refer to object in another package

E

ed

Hey all. This is probably a simple question, but I've looked around
the faq and other parts of the manual and haven't found an answer yet.

I want to use an object from another package.

Example:

--- START Perl CODE -----

$obj = new Object;

# ... DO STUFF in anotherPackage ......


{ package anotherPackage;

# How do I refer to $obj from here?
# I've tried $main::eek:bj and *main::eek:bj
# but neither seem to work

}


--- END Perl CODE ------

tia,
--ed
 
E

ed

try : $newobject = Package::Classname->new();

HTH
Michael


Thanks for the reply. But I would like to use an object that's
already been instantiated in another package.

For now I can do something like this:


package main;

my $obj = new className;
&AnotherPackage::sendObj($obj);


{ package AnotherPackage;

my $obj;

sub sendObj
{ $obj = shift;
}

}


--ed
 
J

Jay Tilton

: Hey all. This is probably a simple question, but I've looked around
: the faq and other parts of the manual and haven't found an answer yet.
:
: I want to use an object from another package.
:
: --- START Perl CODE -----
: $obj = new Object;
:
: # ... DO STUFF in anotherPackage ......
:
: { package anotherPackage;
: # How do I refer to $obj from here?
: # I've tried $main::eek:bj and *main::eek:bj
: # but neither seem to work
: }
: --- END Perl CODE ------

$obj is just a scalar. That it is an object has no impact on its scope
or how to access it from other packages.

Unless $obj is declared with my(), there's no problem with using it as
$main::eek:bj .

If $obj is declared with my(), and the anotherPackage package is in the
same file, as it is in the sample code, there's no problem using it at
all--its scope extends to the end of the file.

If $obj is was declared with my(), and anotherPackage is held in an
unrelated scope, you're boned.
 
E

ed

Unless $obj is declared with my(), there's no problem with using it as
$main::eek:bj .

If $obj is declared with my(), and the anotherPackage package is in the
same file, as it is in the sample code, there's no problem using it at
all--its scope extends to the end of the file.


$obj is delcared with my() in the actual script I'm working with. My
example doesn't refect that. Sorry about that.
And it is being used in the same file, so I don't guess I need to use
any qualified name to access it.

Thanks a lot.

--ed
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top