I would like to determine if my object DESTROY'd is part of a global destruction, and print a warning (as this will be clearly an error and lead to data loss). The obvious way to do this would be:
sub DESTROY {
my $self = shift;
if (i_am_in_global_destruction()) {
warn "I survived until global destruction";
}
}
but I could not find a good way to detect global destruction (instead of the usual destruction of 0).
By “good way” I do not mean this, which, although it works on 5.10.1 and 5.8.8, probably breaks the second, someone gives it a strange look:
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";
}
}'
source
share