我開始注意到一些關於Scope::Guard
的奇怪事情。DESTROY以意想不到的順序呼叫
- 如果我未定義一個
$guard
變量作爲最後的語句子,守衛的子得到 後來稱爲比我期望的那樣。 - 如果我不民主基金,或者如果我
undef $guard
後做一些 (東西),它被當引用超出範圍 如記錄調用。我想知道爲什麼。
的代碼也被發現here
my $sClass = 'SGuard';
# Uncomment to use Scope::Guard instead:
# use Scope::Guard; $sClass = 'Scope::Guard';
package SGuard;
sub new {
my ($class, $sub) = @_;
return bless { sub => $sub }, $class;
}
sub DESTROY {
my ($self) = @_;
$self->{sub}->();
}
package main;
sub mySub {
my $mySubGuard = $sClass->new(sub {
print "shouldDestroyFirst\n"
});
# Do something - any no-op will do.
undef;
# Comment out this line and it works
undef $mySubGuard;
# Or uncomment the next undef line and it works. Any statement(s) or
# no-ops will do but we test the return value of mySub to make sure it
# doesn't return a reference, so undef...
# undef;
}
{
my $scopeGuard = $sClass->new(sub {
print "shouldDestroyLast\n"
});
# Check that mySub returns undef to ensure the reference *did* go out
# of scope
printf "mySub returned a %sdefined value\n", defined mySub() ? "" : "un";
}
print "done\n";
在代碼中,我做了我自己的可憐人的Scope::Guard
(SGuard
以上) 只是爲了讓這個例子儘可能簡單。您也可以使用Scope::Guard
並獲得完全相同的結果,至少對我來說是意想不到的。
我期待這裏面mySub()
的$mySubGuard
應銷燬第一 和調用mySub()
應銷燬 最後的範圍$scopeGuard
。所以得到的輸出,如:
shouldDestroyFirst
mySub returned a undefined value
shouldDestroyLast
done
我得到上面的輸出,如果我使用mySub undef $mySubGuard
線。 如果我不使用mySub undef $mySubGuard
線,我得到以下的輸出:
mySub returned a undefined value
shouldDestroyLast
shouldDestroyFirst
done
所以,它看起來像局部變量的外部範圍被破壞後$mySubGuard
從mySub()
被破壞 。
爲什麼行爲不同只是因爲我undef一個變量,即將 超出範圍?之後爲什麼要做 ?