Hack #76. Peek Inside Closures
Violate closure-based encapsulation when you really need to.
Very few rules in Perl are inviolate—not even the rule that lexicals are inaccessible outside their scopes. For closures to work (and even lexicals in general), Perl has to be able to access them somehow. If you could use the same mechanism, you could read from and write to these variables.
This is very useful for debugging closures and closure-based objects [Hack #43]. It's scary and wrong, but sometimes it's just what you need.
The Hack
Robin Houston's PadWalker
module
helpfully encapsulates the necessary dark magic in a single place that, most importantly, you don't have to understand to use. Suppose you have a misbehaving counter closure:[7]
sub make_counter
{
my ($start, $end, $step) = @_;
return sub
{
return if $start ==
$end;
$start += $step;
};
}
One way to debug this is to throw test case after test case at it [Hack #53] until it fails and you can deduce and reproduce why. An easier approach is to show all of the enclosed values when you have a misbehaving counter.
Once you have a counter, use PadWalker
's closed_over( )
function to retrieve a hash of all closed-over variables, keyed on the name of the variable:
use Data::Dumper; use PadWalker 'closed_over'; my $hundred_by_nines = make_counter( 0, 100, 9 ); while ( my $item = $hundred_by_nines->( ) ) { my $vars = closed_over( $hundred_by_nines ); warn Dumper( $vars ); }
Running the Hack
Running this reveals that $start
, the current value ...
Get Perl Hacks now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.