Here's an example of an anonymous sub generator:
When you are debugging it, the debugger shows the anonymous sub as a
stringified code ref, which can be less than helpful if you are
stepping through the code and have a number of different kinds of
anonymous subs.
Fortunately, there is a way to tell the debugger a name to show for an
anonymous sub. Adding local $DB::sub = 'iterator' at the
top of the anonymous sub (also having another mention somewhere of
$DB::sub to prevent a warning when not run with -d) gives the debugger
a name for it:
But when using stack traces for debugging (e.g. cluck), it calls it just __ANON__.
A little exploration of perl internals shows an undocumented feature.
When you create an anonymous sub actually creates an *__ANON__ glob
in the package and uses that for the name. This means you can add
local *__ANON__ = "incrementor" in the sub {} to give
it a real name while it is running.
Online Fortune Cookie Search
Office Space merchandise
use strict; use warnings; use Carp 'cluck'; sub make_incrementor { my ($initial_value, $reset_value) = @_; my $i = $initial_value; sub { if ($i == $reset_value) { cluck('Iterator overuse'); $i = $initial_value; } return $i++; } } sub test_incrementor { my $foo_incrementor = make_incrementor(0,2); print "call $_: got ", $foo_incrementor->(), "\n" for 1..3; } test_incrementor;
$ perl -d inc.pl Loading DB routines from perl5db.pl version 1.19 Editor support available. Enter h or `h h' for help, or `man perldebug' for more help. main::(inc.pl:23): test_incrementor; DB<1> c 11 call 1: got 0 call 2: got 1 main::CODE(0x10326b1c)(inc.pl:11): cluck('Iterator overuse'); DB<2>
main::incrementor(inc.pl:13): cluck('Iterator overuse');
call 1: got 0 call 2: got 1 Iterator overuse at inc.pl line 13 main::__ANON__() called at inc.pl line 22 main::test_incrementor() called at inc.pl line 25 call 3: got 0
What does that buy you, since it
already says "at inc.pl line 22"? Well, if the sub{} is in a string
eval, you won't have an actual code line. Also, you can include extra
information for closures like
local *__ANON__ = "incrementor_init${initial_value}_reset$reset_value"
-- Note that this is an example of a harmless use of an undocumented feature. You use it for debugging only, and if perl stops working this way, the *__ANON__ setting does nothing harmful.
(Given time, I will post more later about my attempts to attach a different *__ANON__ to each sub {} at compile time, which led to this question. For those who experiment, I will note now that the sub doesn't hold a refcnt on the *__ANON__ glob, so you have to arrange some other way to keep it allocated.)
Online Fortune Cookie Search
Office Space merchandise
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Named anonymous subs
by tilly (Archbishop) on Nov 06, 2003 at 00:57 UTC | |
Re: Named anonymous subs
by Ovid (Cardinal) on Nov 05, 2003 at 22:43 UTC | |
Re: Named anonymous subs
by blokhead (Monsignor) on Nov 05, 2003 at 23:39 UTC | |
by fluffy (Scribe) on Oct 02, 2004 at 18:41 UTC | |
by stvn (Monsignor) on Mar 08, 2006 at 03:22 UTC | |
Re: Named anonymous subs
by Julian Mehnle (Initiate) on Mar 08, 2006 at 02:35 UTC | |
Re: Named anonymous subs
by ysth (Canon) on Nov 06, 2013 at 22:38 UTC |
Back to
Meditations