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

I need to create a debug log for large mess of code I inherited.

What is the most efficient way to determine the name of the currently executing subroutine?

Replies are listed 'Best First'.
Re: name of executing sub?
by Mr. Muskrat (Canon) on Jan 10, 2014 at 18:58 UTC

    Caller works well enough except for AUTOLOAD.

    #!/usr/bin/env perl use strict; use warnings; sub foo { my $name = (caller(0))[3]; my $caller = (caller(1))[3]; if ( defined $caller ) { print "My name is $name and I was called by $caller.\n"; } else { print "My name is $name.\n"; } } sub bar { my $name = (caller(0))[3]; my $caller = (caller(1))[3]; if ( defined $caller ) { print "My name is $name and I was called by $caller.\n"; } else { print "My name is $name.\n"; } foo(); baz(); } sub AUTOLOAD { my $name = (caller(0))[3]; my $called = our $AUTOLOAD; my $caller = (caller(1))[3]; if ( defined $caller ) { print "My name is $name and I was called as $called by $caller +.\n"; } else { print "My name is $name and I was called as $called.\n"; } } foo(); bar(); baz(); __END__ My name is main::foo. My name is main::bar. My name is main::foo and I was called by main::bar. My name is main::AUTOLOAD and I was called as main::baz by main::bar. My name is main::AUTOLOAD and I was called as main::baz.

    Updated to show who called (if applicable).

      This is why I think that the best way of implementing AUTOLOAD is something like this:

      use Sub::Name; sub AUTOLOAD { my ($name) = (our $AUTOLOAD =~ /(\w+)$/); my $func = subname $name => sub { ...; # this is where the work happens }; no strict 'refs'; *$name = $func; goto $func; }

      What this means is that the first time an autoloaded function is called, AUTOLOAD creates the function as a coderef, and then installs that into the symbol table, so that the next time the same named function is called, no AUTOLOAD needs to happen.

      And it fixes caller, and stuff that relies on caller like Carp::confess.

      use Moops; class Cow :rw { has name => (default => 'Ermintrude') }; say Cow->new->name
      Outstanding!

      The AUTOLOAD part is beyond my current understanding but the examples enlightened me to exactly what I needed!
Re: name of executing sub?
by roboticus (Chancellor) on Jan 10, 2014 at 18:53 UTC

    arley_ge:

    In vi, you could use something like: ?^sub

    On a more serious note, when you're writing your code, you know what the name of your subroutine is that you're writing, so I presume you mean to determine the name of the subroutine that's calling the one you're writing. In that case, you could use the caller function to check the stack frame.

    If you're meaning that you want the subroutine running at a specific point in time, I guess you could use a timer in conjunction with a signal handler to interrupt your program at a particular point in time. I've never tried it, but if you do so, I'm guessing you could use caller to determine what routine you were in when the signal fired.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

      Yes, calling subroutine is what I meant. Doh!

      Thanks for your kind reply.

Re: name of executing sub?
by jellisii2 (Hermit) on Jan 10, 2014 at 19:13 UTC
    If you're working on logs, Might I recommend Log::Log4perl, assuming that you're not running super high workloads? It has quite a bit of information at it's fingertips, including the one you're looking for.
Re: name of executing sub?
by Anonymous Monk on Jan 10, 2014 at 18:48 UTC

    What is the most efficient way to determine the name of the currently executing subroutine?

    Its too early to worry about efficiency , start with one way, and it will be good enough

    Devel::Trace, caller...

      OK, let me correct my quetion...

      What is a way to determine the name of the currently executing subroutine?

      Or, what is the easiest way to determine the name of the currently executing subroutine?
Re: name of executing sub?
by karlgoethebier (Abbot) on Jan 10, 2014 at 18:54 UTC
    "...code I inherited"

    Can't you show the code you inherited?

    Karl

    «The Crux of the Biscuit is the Apostrophe»