For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > July 2005 > A little Object Question









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author A little Object Question
Tom Allison

2005-07-24, 8:29 pm

I have two objects.

One object is in ~/ and starts like:

package IMAP;

@ISA=qw(Mail::IMAPClient);
use strict;
use warnings;

use Mail::IMAPClient;

---

I can use it in my imap.t code with a simple
use IMAP;
and all the methods are visible. I call these methods as Object methods.

I have another object in a lib path that looks like:

package HIP;

use strict;
use warnings;

---

my test module has

use lib '/home/tallison/lib';
use strict;
use HIP;

and none of the package functions are visible. I call these methods as
functions and not as Objects.


What is the difference between these two that breaks the second one?
Why?
Jeff 'japhy' Pinyan

2005-07-24, 8:29 pm

On Jul 21, Tom Allison said:

> I have two objects.

[snip]
> I have another object in a lib path that looks like:
>
> package HIP;
>
> use strict;
> use warnings;


> use lib '/home/tallison/lib';
> use strict;
> use HIP;
>
> and none of the package functions are visible. I call these methods as
> functions and not as Objects.


Hang on. An "object" is a reference that has been blessed into a specific
package (called, in the OO world, a "class"). A "module" is a Perl source
file ending with ".pm" that typically defines a package.

The HIP module has functions that you're trying to access, right? But
when you call them, you get errors like "function XYZ not defined", right?
This is because they exist in HIP's namespace, not main's (or whatever
package you're calling them from). There are two general solutions here.

The first is to use the functions' full names:

HIP::function_name(...);

instead of just

function_name();

The second is to equip the HIP module with Exporter, which defines a
simple system for exporting symbols (such as functions or variables) to
the package using the module. In this way, you could do:

use HIP qw( function_a $something );

print function_a();
print $something;

which would be identical to:

use HIP (); # import absolutely nothing from HIP
print HIP::function_a();
print $HIP::something;

To find out about Exporter, read its documentation: perldoc Exporter.

>
>
> What is the difference between these two that breaks the second one?
> Why?
>
>


--
Jeff "japhy" Pinyan % How can we ever be the sold short or
RPI Acacia Brother #734 % the cheated, we who for every service
http://japhy.perlmonk.org/ % have long ago been overpaid?
http://www.perlmonks.org/ % -- Meister Eckhart
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com