http://qs321.pair.com?node_id=11128540


in reply to Closure Over Scalar?

This seems to be what use feature 'state' is made for.
#!/usr/bin/env perl # demo of closure around sub, with persistent string use strict; use warnings; use feature 'state'; for my $q ('a'..'g') { do_something($q); print "Done computing\n" } sub do_something { state $FIXED_STRING = 'fixed_string'; state %persistent; my $x = $_[0]; $persistent{$x}{$FIXED_STRING} = rand; END { for my $k (keys %persistent) { print "$k: $persistent{$k}{$FIXED_STRING}\n"; } } }
Bill