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
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.)
|
---|
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 |