http://www.perlmonks.org?node_id=666724

perl45036 has asked for the wisdom of the Perl Monks concerning the following question:

#!/usr/bin/perl use strict; my @firstlist = qw(1 2 3 4 5); my @secondlist = qw(A B C D E); process_list(\@firstlist); process_list(\@secondlist); sub process_list { my($list_ref) = @_; sub get_list { return @$list_ref; } print_list(); } sub print_list { foreach my $item (get_list()) { print "LIST ITEM: $item\n"; } }
Output:
LIST ITEM: 1 LIST ITEM: 2 LIST ITEM: 3 LIST ITEM: 4 LIST ITEM: 5 LIST ITEM: 1 LIST ITEM: 2 LIST ITEM: 3 LIST ITEM: 4 LIST ITEM: 5
Why does the closure "get_list" not get updated to use the reference to @secondlist? I'm assuming there is a scope issue with the way "get_list" is declared. I understand there is a better way to write this with the use of a function that returns an anonymous sub but I'm not sure how to explain the issue with the code above. Better solution:
#!/usr/bin/perl use strict; my @firstlist = qw(1 2 3 4 5); my @secondlist = qw(A B C D E); process_list(\@firstlist); process_list(\@secondlist); my $get_list_sub; sub make_list_closure { my ($ref) = @_; return sub { return @$ref; }; } sub process_list { my($list_ref) = @_; $get_list_sub = make_list_closure($list_ref); print_list(); } sub print_list { foreach my $item ($get_list_sub->()) { print "LIST ITEM: $item\n"; } }