#!/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"; } } }
In reply to Re: Closure Over Scalar?
by BillKSmith
in thread Closure Over Scalar?
by QM
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |