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