Global Destruction Detection in Perl

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";
    }
}'
+5
source share
2 answers

Devel:: GlobalDestruction, XS, .

:, perl 5.14.0 ${^GLOBAL_PHASE}, ​​ "DESTRUCT" . Devel:: GlobalDestruction, perls 5.6. perl ${^GLOBAL_PHASE} C.

+10

, , - END.

package Whatever;
our $_IN_GLOBAL_DESTRUCTION = 0;
END {
    $_IN_GLOBAL_DESTRUCTION = 1;
}
+8

All Articles