Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Special variable to return current sub name?

by spudnic (Acolyte)
on Aug 12, 2003 at 20:59 UTC ( #283353=perlquestion: print w/replies, xml ) Need Help??

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

Just as we have $$ for the pid or $ENV{'TERM'} to read an environmental variable, is there any special variable that will equal the name of the subroutine that is currently being executed?

ex:
#!/usr/bin/perl print "Hello\n"; &ThisSub; exit(0); sub ThisSub { print "I am now in sub $#@_#($&\n"; return(1); } output: Hello I am now in sub ThisSub

Replies are listed 'Best First'.
Re: Special variable to return current sub name?
by Mr. Muskrat (Canon) on Aug 12, 2003 at 21:09 UTC

    Nope but you can still get that info.

    #!/usr/bin/perl print "Hello\n"; &ThisSub; exit(0); sub ThisSub { my $sub = (caller(0))[3]; $sub =~ s/.*:://; # remove the package name, comment out of you wan +t it too print "I am now in sub $sub\n"; return 1; # or simply 1; }

Re: Special variable to return current sub name?
by liz (Monsignor) on Aug 12, 2003 at 21:10 UTC
•Re: Special variable to return current sub name?
by merlyn (Sage) on Aug 12, 2003 at 23:36 UTC
    The current sub might not have a name (anon sub) or it might have many names (thanks to aliases). I agree with a previous comment... why do you need to know? Sounds like a bad design.

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

Re: Special variable to return current sub name?
by The Mad Hatter (Priest) on Aug 12, 2003 at 21:10 UTC
    It seems to me that having to know the name of the current sub indicates bad design in some way. I'm curious as to why you need/want this?

    (And no, I don't think there is such a variable.)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2022-06-30 05:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My most frequent journeys are powered by:









    Results (97 votes). Check out past polls.

    Notices?