sub lambda {
my $f = $_[1];
for my $x ( $_[0] ) {
return sub {
return localize( $x, $_[0], $f );
}
}
}
sub localize {
my( $lexical, $val, $f )= @_;
local( $_[0] )= $val;
$f->();
}
(Updated order of arguments for no particularly good reason.)
Update: This, however, doesn't work (despite not generating an error). This is because (I believe) local localizes $_[0] by replacing the alias with a new SV that gets set to the new value. The prior value is restored by restoring the alias back into that spot in the array. Thus, the alias is never modified and thus the value of the lexical is never changed.