in reply to sub calling name

Is is possible to determine the "name" by which name a reference to sub is called?

There are two ways I can think of, neither of which are (IMHO) elegant and both of which are kludges since you're not really getting the calling info from the reference, but rather by how you defined them.

The first uses wrappers (anonymous or otherwise)...

$divesub = sub { somesub('divesub') }; $raisesub = sub { somesub('raisesub') }; sub somesub { if ( $_[0] eq "divesub") { &dive; } else { &surface; } }

... while the second would be to rename your sub to the magical AUTOLOAD and point each reference to a non-existant sub so that it got ran instead ...

$divesub = \&divesub; $raisesub = \&raisesub; sub AUTOLOAD { (my $sub = $AUTOLOAD) =~ s/.*:://; if ( $sub eq "divesub" ) { &dive; } else { &surface; } }

    --k.


Replies are listed 'Best First'.
Re: Re: sub calling name
by Dogma (Pilgrim) on Apr 11, 2002 at 19:53 UTC
    AUTOLOAD would be AUTOOVERKILL for this type of thing. I never thought of using a sub to trap an argument list to a function call. Thats pretty slick! For what I was thinking of doing that will work. There is a downside though - if you take a reference on that reference the arguments don't change.

    Cheers,
    -Dogma