Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

How to display name of current package

by sylph001 (Sexton)
on Aug 22, 2016 at 10:16 UTC ( [id://1170155]=perlquestion: print w/replies, xml ) Need Help??

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

Dear monks,

Recently I tried to find a way to display the name of the perl package that the program is in, however didn't get any clue about this.

Could you let me know if there is any special varaible or any other method that I can get the package name?

Thanks a lot in advance

For example, I'm writing a package in "MyModule.pm", and I need to display the package name when the program is executing this package, like in below:

package MyPackage; use warnings; my $pkg_name = <how to know the package name?>; print "I'm currently in the $pkg_name package!"; exit 0;

Replies are listed 'Best First'.
Re: How to display name of current package
by LanX (Saint) on Aug 22, 2016 at 10:19 UTC
Re: How to display name of current package
by Discipulus (Canon) on Aug 22, 2016 at 10:38 UTC
    just for seek of completness (and because they are sparsed in index-functions with misregard of the underscore..), you can find all special literals in the dedicated section in perldata.

    They are all special tokens Pay attention to __SUB__ that is available only on request. They are:__FILE__  __LINE__ __PACKAGE__ __SUB__ __END__ __DATA__

    L*

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

      Worth noting, I think, that __SUB__ is availabe >= v5.16. While it is ludicrous, quite a few of us are stuck on v5.10 or even v5.8. :\

        Or even 5.6.1 .... YIKES!

        As Occam said: Entia non sunt multiplicanda praeter necessitatem.

Re: How to display name of current package
by Athanasius (Archbishop) on Aug 22, 2016 at 12:25 UTC

    Hello sylph001,

    Your question is not entirely clear to me. So, although it seems likely that you’re looking for the __PACKAGE__ literal, as described by LanX and Discipulus, it’s just possible that you’re really looking for the functionality of Perl’s built-in caller function. The following snippet should make the difference clear:

    #! perl use strict; use warnings; package Foo; sub f { my ($package, $filename, $line) = caller; printf "Function '%s::f()' called from package '%s'\n", __PACKAGE_ +_, $package; } package Bar; Foo::f();

    Output:

    22:24 >perl 1686_SoPW.pl Function 'Foo::f()' called from package 'Bar' 22:24 >

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1170155]
Approved by Ratazong
Front-paged by kcott
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (2)
As of 2024-04-19 01:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found