From: PerlFAQ Server on
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 false

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
packges.



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

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.
From: Ben Morrow on

Quoth Xho Jingleheimerschmidt <xhoster(a)gmail.com>:
> Ben Morrow wrote:
> > Quoth PerlFAQ Server <brian(a)theperlreview.com>:
> >> 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
> >
> > Should this answer mention ->DOES at this point?
>
> Where is that documented? I've heard of ->can, but never of ->DOES.

UNIVERSAL.

> Is this a 5.10 thing?

Yes. The idea is that ->DOES asks 'does this object implement this
interface' rather than 'does this object inherit from this class'. I'm
still a little unclear as to why it's *wrong* for ->isa to return true
in that case, especially given that Perl has many different object
systems, not all based on @ISA.

> And shouldn't there be an entry for "can" in perldoc -f? I know it
> isn't strictly a function, but it is closer to one that "use" or "my".

What, just an entry like

can
This is a method. See UNIVERSAL.

? I would be loath to see anything which encouraged people to call
UNIVERSAL::can as a function.

Ben