I'd like to detect if my object is being DESTROY
'd as part of global destruction, and print out a warning (as that'd clearly be an error and lead to data loss). The obvious way to do that would seem to be:
sub DESTROY {
my $self = shift;
# ⋮
if (i_am_in_global_destruction()) {
warn "I survived until global destruction";
}
}
but I have been unable to find a good way to detect global destruction (instead of normal refcount hit 0 destruction).
By "good way", I mean not this, which though it works on 5.10.1 and 5.8.8, probably breaks the second someone gives it an odd glance:
sub DESTROY {
$in_gd = 0;
{
local $SIG{__WARN__} = sub { $_[0] =~ /during global destruction\.$/ and $in_gd = 1 };
warn "look, a warning";
}
if ($in_gd) {
warn "I survived until global destruction";
}
}'
Devel::GlobalDestruction
source) that in v5.13.7 there is a${^GLOBAL_PHASE}
variable that suits this purpose. – Chaddie