FAQ 7.26 How can I find out my current or calling package?

P

PerlFAQ Server

This is an excerpt from the latest version perlfaq7.pod, which
comes with the standard Perl distribution. These postings aim to
reduce the number of repeated questions as well as allow the community
to review and update the answers. The latest version of the complete
perlfaq is at http://faq.perl.org .

--------------------------------------------------------------------

7.26: How can I find out my current or calling package?

(contributed by brian d foy)

To find the package you are currently in, use the special literal
"__PACKAGE__", as documented in perldata. You can only use the special
literals as separate tokens, so you can't interpolate them into strings
like you can with variables:

my $current_package = __PACKAGE__;
print "I am in package $current_package\n";

If you want to find the package calling your code, perhaps to give
better diagnostics as "Carp" does, use the "caller" built-in:

sub foo {
my @args = ...;
my( $package, $filename, $line ) = caller;

print "I was called from package $package\n";
);

By default, your program starts in package "main", so you should always
be in some package unless someone uses the "package" built-in with no
namespace. See the "package" entry in perlfunc for the details of empty
packages.

This is different from finding out the package an object is blessed
into, which might not be the current package. For that, use "blessed"
from "Scalar::Util", part of the Standard Library since Perl 5.8:

use Scalar::Util qw(blessed);
my $object_package = blessed( $object );

Most of the time, you shouldn't care what package an object is blessed
into, however, as long as it claims to inherit from that class:

my $is_right_class = eval { $object->isa( $package ) }; # true or false

And, with Perl 5.10 and later, you don't have to check for an
inheritance to see if the object can handle a role. For that, you can
use "DOES", which comes from "UNIVERSAL":

my $class_does_it = eval { $object->DOES( $role ) }; # true or false

You can safely replace "isa" with "DOES" (although the converse is not
true).



--------------------------------------------------------------------

The perlfaq-workers, a group of volunteers, maintain the perlfaq. They
are not necessarily experts in every domain where Perl might show up,
so please include as much information as possible and relevant in any
corrections. The perlfaq-workers also don't have access to every
operating system or platform, so please include relevant details for
corrections to examples that do not work on particular platforms.
Working code is greatly appreciated.

If you'd like to help maintain the perlfaq, see the details in
perlfaq.pod.
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top