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

In reply to Re: How to know current Package by bart
in thread How to know current Package by perladdict

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.