1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
package TIE; sub TIESCALAR { my $class = shift; my $code = shift; return bless \$code => $class; } sub DESTROY { ${$_[0]}->(); undef ${$_[0]}; } package BlockExit; sub do { my $var; tie $var, 'TIE', shift; return \$var; } package main; if (1) { my $onExit = BlockExit::do(sub { say "RAUS!"; }); say "DRIN"; } say "DRAUSSEN";
Quote[...]
When the last reference to an object goes away, the object is destroyed. If you only have one reference to an object stored in a lexical scalar, the object is destroyed when that scalar goes out of scope.
[...]