perladdict has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, I am running a script and wants to know the script is running under which package.i.e in which Package I am in?? Also,I want to know "how to access the package" by name or by it's associated variables. Also, I am having a script and having some modules included there.Is there any command to know what modules we used in our script without seeing in to the script Pls suggest Monks......

Replies are listed 'Best First'.
Re: How to know current Package
by davorg (Chancellor) on Jun 05, 2006 at 07:38 UTC

    I'm not entirely sure what you're asking, but the current package is always in __PACKAGE__. This is from perldoc perldata:

    Special Literals

    The special literals __FILE__, __LINE__, and __PACKAGE__ represent the current filename, line number, and package name at that point in your program. They may be used only as separate tokens; they will not be interpolated into strings. If there is no current package (due to an empty package; directive), __PACKAGE__ is the undefined value.

    --
    <http://dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

Re: How to know current Package
by bart (Canon) on Jun 05, 2006 at 08:23 UTC
    It looks like __PACKAGE__ is what you're asking for, like davorg said...

    But I'd like to point you towards caller too, where you can find out what package a sub has been called from.

    sub test { my($pkg) = caller; print "I've been called from package $pkg\n"; } package Foo; main::test(); __END__ I've been called from package Foo

    Note that you can get more extensive information, as well as finding out about deeper calling levels, if you use an integer parameter to caller. For this application, caller() and caller(0) would yield the same results.

    Also,I want to know "how to access the package" by name or by it's associated variables.
    You mean, how to access variables from a different package? Like this:
    $Foo::x # scalar @Foo::y # array %Foo::z # hash $Foo:z{bar} # hash item # etc.
    If you want to access variables of which you just found out the package name, there are several ways, a stash (from "Symbol Table hASH", which is what it is) being the low level (but hard) approach — in particular, deeper level package names like Foo::Bar are hard to access. There's an excellent chapter in the 1st edition of the O'Reilly book "Advanced Perl Programming" about stashes.

    But the easy approach is to use symbolic references. That's how Exporter does it. Note that you'll temporarily have to disable use strict;.

    my $pkg = "Foo"; my $var = "x"; $Foo::x = 123; { no strict 'refs'; print ${$pkg . '::' . $var}; } __END__ prints: "123";
Re: How to know current Package
by ambrus (Abbot) on Jun 05, 2006 at 09:44 UTC

    You are in the package main, also called package ::.

Re: How to know current Package
by Joost (Canon) on Jun 05, 2006 at 10:44 UTC