Created
August 8, 2013 09:33
-
-
Save ChinaXing/6183183 to your computer and use it in GitHub Desktop.
Show closure variables of a subroutine
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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