Here's an example of an anonymous sub generator:
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;
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.
$ 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>
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:
main::incrementor(inc.pl:13): cluck('Iterator overuse');
But when using stack traces for debugging (e.g. cluck), it calls it just __ANON__.
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
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.
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
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.