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

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

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";

    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

    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


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

Site Timeline