Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

how to get a subroutine name?

by Hossein (Acolyte)
on Jun 26, 2013 at 10:58 UTC ( [id://1040748]=perlquestion: print w/replies, xml ) Need Help??

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

Hi,

I need to know which subroutine is running in order to display error message.

How do I get hold on subroutin name in a variable when the subroutine is running?

I prefare ??? to contain the running subroutine name.

print "$CFG{ERROR_NOFILE} [ ??? ]\n";

Replies are listed 'Best First'.
Re: how to get a subroutine name?
by LanX (Saint) on Jun 26, 2013 at 11:00 UTC
    see caller

    DB<111> sub tst { print ( (caller(0))[3] ) } DB<112> tst() main::tst

    Cheers Rolf

    ( addicted to the Perl Programming Language)

Re: how to get a subroutine name?
by hdb (Monsignor) on Jun 26, 2013 at 11:10 UTC

    For historic reasons, I would propose to define a sub called __FUNC__ that returns the name of the subroutine in which it was called, see the following example:

    use strict; use warnings; sub __FUNC__ { (caller 1)[3] } sub func { print "We are now in ", __FUNC__, "\n"; } func();

    UPDATE: Removed a duplicated "print".

      > For historic reasons,

      Sorry, which historic reasons?

      If you don't like caller's interface - where I sympathize - I'd rather propose something more flexible like returning a hashref:

      DB<116> sub __CALLER__ { my $level = shift; my %hash; @hash{qw/package filename line subroutine hasargs wantarray + evaltext is_require hints bitmask hinthash/} = caller($level+1); return \%hash } DB<130> sub tst { print __CALLER__->{subroutine} } DB<131> tst() main::tst
      or
      DB<136> use Data::Dump qw/dd/ DB<137> sub tst { dd __CALLER__ } DB<138> tst() { bitmask => "\0\0\0\0\0\0\0\0\0\0\0\0", evaltext => undef, filename => "(eval 108)[multi_perl5db.pl:644]", hasargs => 1, hinthash => undef, hints => 256, is_require => undef, line => 2, "package" => "main", subroutine => "main::tst", "wantarray" => 1, }
      Though not sure about using this underscore __NAMESPACE__...

      And I'm pretty sure about reinventing the wheel ...

      ... CPAN is so big and I'm so lazy ;-)

      Cheers Rolf

      ( addicted to the Perl Programming Language)

        I had to think about __LINE__ and __FILE__ etc from the good old times...

        Correction: I was thinking about good old __LINE__ and __FILE__...

      5.16 added __SUB__ for this purpose.

      My bad! It returns a coderef.

        However, __SUB__ returns a reference to the currently running subroutine according to perlsub. Not sure how you will get its name?

Log In?
Username:
Password:

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

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

    No recent polls found