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.
Why is UNTIE not called when tied variable goes out of scope?
Asking why UNTIE isn't called when the variable goes out of scope is the same thing as asking why UNTIE isn't called whenever DESTROY is called. Well, that would be useless. What is useful is a function that's called when untie is called, and that's what UNTIE is.
If you want common code to be called when untie is called and when the object is destroyed, nothing's stopping you.
sub UNTIE { shift->_destructor(@_) }
sub DESTROY { shift->_destructor(@_) }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With