Skip to content

Instantly share code, notes, and snippets.

@ChinaXing
Created August 8, 2013 09:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ChinaXing/6183183 to your computer and use it in GitHub Desktop.
Save ChinaXing/6183183 to your computer and use it in GitHub Desktop.
Show closure variables of a subroutine
# PadWalker
use warnings;
use strict;
use PadWalker;
my $a = "A";
my $b = "B";
my $sub1 = do {
my $c = "C";
my $d = "D";
sub {
my $e = "E";
print "SUB1", $a," AND ",$c;
}
};
my $sub2 = sub {
my $f = "F";
print "SUB2", $a, " AND ", $f;
};
sub sub3 {
my $g = "G";
print "SUB3", $a, " AND ", $g;
}
my $hash_1 = PadWalker::closed_over($sub1);
my $hash_2 = PadWalker::closed_over($sub2);
my $hash_3 = PadWalker::closed_over(\&sub3);
require Data::Dumper;
$Data::Dumper::Terse = 1;
print "SUB1 : \n", Data::Dumper::Dumper($hash_1);
print "SUB2 : \n", Data::Dumper::Dumper($hash_2);
print "SUB3 : \n", Data::Dumper::Dumper($hash_3);
__END__
# ---------------------- result --------------------------- #
=begin result
SUB1 :
{
'$c' => \'C',
'$a' => \'A'
}
SUB2 :
{
'$a' => \'A'
}
SUB3 :
{
'$a' => \'A'
}
=end result
=cut
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment