in reply to Local for lexicals
Here is a working example that eschews all external dependencies and just "works", but requiring a less concise invocation:
#!/usr/bin/perl -w use strict; my $x = 1; my $f = sub { $x }; { my $scope= tempSet( \$x, 2 ); print $f->(), "\n"; # => 2 } print $f->(), "\n"; # => 1 sub TempSet::DESTROY { shift(@_)->() } sub tempSet { my( $sv, $new )= @_; my $prior= $$sv; my $restore= bless sub { $$sv= $prior }, 'TempSet'; $$sv= $new; return $restore; }
- tye
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Local for lexicals (KISS)
by JadeNB (Chaplain) on Aug 10, 2009 at 21:02 UTC | |
by tye (Sage) on Aug 10, 2009 at 21:10 UTC | |
by JadeNB (Chaplain) on Aug 10, 2009 at 21:29 UTC | |
by tye (Sage) on Aug 10, 2009 at 22:20 UTC | |
by JadeNB (Chaplain) on Aug 10, 2009 at 23:17 UTC | |
by JadeNB (Chaplain) on Aug 10, 2009 at 22:52 UTC | |
| |
|
Re^2: Local for lexicals (KISS)
by Roy Johnson (Monsignor) on Aug 13, 2009 at 15:25 UTC |