|
|
| The stupid question is the question not asked | |
| PerlMonks |
Named anonymous subsby ysth (Canon) |
| on Nov 05, 2003 at 22:06 UTC ( #304883=perlmeditation: print w/ replies, xml ) | Need Help?? |
|
Here's an example of an anonymous sub generator:
$ 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
in the sub {} to give it a real name while it is running. 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
Back to
Meditations
|
|
||||||||||||||||||||||||||||||||