#!/usr/bin/perl -w use strict; # my $x = 1; tie my $x, 'Tie', 1; my $f = sub { $x }; print $f->(), "\n"; # => 1 { 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; } sub Tie::TIESCALAR { return bless \$_[1] => $_[0] } sub Tie::FETCH { return "${$_[0]} is still tied at line " . (caller())[2] } sub Tie::STORE { ( ${$_[0]} = $_[1] ) =~ s/ is still tied at line \d+$// } #### 1 is still tied at line 7 2 is still tied at line 10 1 is still tied at line 12