[CMI.PM] get function name?

Mike Rosulek mike at mikero.com
Thu Apr 21 08:56:51 PDT 2005


The CMI.PM list liiiiiiives ;)

Try 'caller' -- it can look down the call stack to any depth (including
depth 0, which gives the current stack frame) and tell you various
useful thingies about the frames.

  sub myfunc {
     my $fname = (caller 0)[3];
     print "$fname\n"; # will print "main::myfunc"
  }


Cheers,
Mike




On Thu, 21 Apr 2005 10:41:35 -0500, "Jay A. Kreibich" <jak at uiuc.edu>
said:
> 
>   Anyone know if there is a way to get the current function name?  For
>   example:
> 
> ----------------------------------------------------------------------
> 
> package myPkg;
> use strict;
> 
> sub myFunc
> {
> 	my( $f ) = ?????;
> 
> 	# want $f equal to "MyFunc" or "myPkg::myFunc"
> }
> 
> 1;
> 
> ----------------------------------------------------------------------
> 
>   With '?????' being some contant var reference or symbol (e.g. a
>   built-in var or something like '__PACKAGE__'.
> 
>   With an autoloaded function this information is avaliable in
>   $AUTOLOAD, but I was wondering if anyone knows how to extract this
>   in the context of a normal package function and/or method.
> 
>    -j
> 
> -- 
>                      Jay A. Kreibich | CommTech, Emrg Net Tech Svcs
>                         jak at uiuc.edu | Campus IT & Edu Svcs
>           <http://www.uiuc.edu/~jak> | University of Illinois at U/C
> _______________________________________________
> Champaign-Urbana mailing list
> Champaign-Urbana at pm.org
> http://mail.pm.org/mailman/listinfo/champaign-urbana


More information about the Champaign-Urbana mailing list