Running:
$t = 3;
{
tie $t, 'Yep';
} # Expect $t to become untied here.
print $t;
package Yep;
sub TIESCALAR {
bless {}, 'Yep';
}
sub UNTIE {
print "UNTIE\n";
}
sub DESTROY {
print "DESTROY\n";
}
The output is:
Can't locate object method "FETCH" via package "Yep" at a.pl line 5.
DESTROY
The EXPECTED output is:
DESTROY
3
I want to tie
variable $t only for the duration of the scope in which tie
is located. Out of the scope it must behave same as before tie. So I wrap tie
into the block and expect that untie
will be called when the end of block is reached (like 'local' where the value is restored at the end of block, but for tied variable I expect behaviour is restored (untie $t
) ). Notice the $t
is not out of scope yet.
As for the completely new question,
Changes to variables aren't automatically undone when the scope in which those changes are made
my $t = 3;
{
$t = 4;
}
print "$t\n"; # 4, not 3.
Same goes when the change is to add tie magic. You can use untie
to remove the magic, but it's best if you simply use a new variable.
my $t = 3;
{
tie my $t, 'Yep';
} # Tied variable destroyed here.
print "$t\n"; # 3.