bichonfrise74 has asked for the wisdom of the Perl Monks concerning the following question:
I got this code from this article perl.com.
Can someone further dissect or rewrite this code to show what is exactly happening?
I tried to read the article on the explanation of the code above but I could not figure out how it is able to 'retain/remember' the old values.sub make_counter { my $start = shift; return sub { $start++ } } my $from_ten = make_counter(10); my $from_three = make_counter(3); print $from_ten->() . "\n"; print $from_ten->() . "\n"; print $from_three->() . "\n";
Can someone further dissect or rewrite this code to show what is exactly happening?
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Closure Explanation
by AnomalousMonk (Archbishop) on Apr 30, 2010 at 02:56 UTC | |
Re: Closure Explanation
by almut (Canon) on Apr 30, 2010 at 06:22 UTC | |
Re: Closure Explanation
by Anonymous Monk on Apr 30, 2010 at 02:16 UTC | |
Re: Closure Explanation
by Anonymous Monk on Apr 30, 2010 at 18:35 UTC | |
by runrig (Abbot) on Apr 30, 2010 at 20:37 UTC | |
by TGI (Parson) on Apr 30, 2010 at 19:06 UTC | |
by Anonymous Monk on Apr 30, 2010 at 19:33 UTC |
Back to
Seekers of Perl Wisdom