SPUG: Finding name for sub ref

SPUG-list-owner tim at consultix-inc.com
Mon Jul 15 11:05:35 CDT 2002


> I've created a subroutine to time the execution of another subroutine; so I pass in a reference to the subroutine that is to be timed.
> 
> Zub timeThis
> {
>       my ($sub) = shift;
>       my $startTime = time();
>       &{$sub}(@_);
>       my $endTime = time();
>       # if possible, I'd like to get the subroutine name from the reference right here,
>       # then I'd be able to print it out in the following print statement.
>       print " took " . ($endTime - $startTime) . " seconds\n";
> }
> 
> timeThis(\&createTheFiles, $path, $numberOfFiles, $fileSize, $dataType);
> 
> 
> Is there a way to extract the name of the subroutine from the reference to the subroutine?
> 
> Thanks...
> 
> -Keith

I didn't know of any easy way to do this, so I asked "The Damian", while we were
on a break in our Adv. OO Perl class this morning.  And he says, 1) first of all,
if it's an anonymous sub, it won't have a name, and 2) if it is named, you'd
have to walk through the symbol tables for each of your packages looking
for its address, to retrieve the name.

Maybe you should ask the sub's caller to provide the name as an additional arguent!

-Tim

*==============================================================================*
|  Tim Maher, CEO, CONSULTIX  (206) 781-UNIX; (866) DOC-PERL; (866) DOC-LINUX  |
|   tim at consultix-inc.com  teachmeunix.com  teachmeperl.com  teachmelinux.net  |
| 7/29:D'base w/Perl 8/19:UNIX  8/26:Perl Prog  9/18:Int Perl  9/23:Sh & Utils |
*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - *
| NEW Seminar Series!  "DAMIAN CONWAY's Adv. Perl Workshop";   Seattle 7/15-18 |
|          Adv. OOP  *  Adv. Module Techniques  *  Programming Perl 6          |
*==============================================================================*

 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     POST TO: spug-list at pm.org       PROBLEMS: owner-spug-list at pm.org
      Subscriptions; Email to majordomo at pm.org:  ACTION  LIST  EMAIL
  Replace ACTION by subscribe or unsubscribe, EMAIL by your Email-address
 For daily traffic, use spug-list for LIST ;  for weekly, spug-list-digest
     Seattle Perl Users Group (SPUG) Home Page: http://seattleperl.org




More information about the spug-list mailing list