1 package Devel::GlobalDestruction;
8 use Sub::Exporter::Progressive -setup => {
9 exports => [ qw(in_global_destruction) ],
10 groups => { default => [ -all ] },
13 # we run 5.14+ - everything is in core
15 if (defined ${^GLOBAL_PHASE}) {
16 eval 'sub in_global_destruction () { ${^GLOBAL_PHASE} eq q[DESTRUCT] }; 1'
19 # try to load the xs version if it was compiled
23 XSLoader::load(__PACKAGE__, $VERSION);
26 # the eval already installed everything, nothing to do
29 # internally, PL_main_start is nulled immediately before entering global destruction
30 # and we can use B to detect that
32 eval 'sub in_global_destruction () { B::main_start()->isa(q[B::NULL]) }; 1'
36 1; # keep require happy
43 Devel::GlobalDestruction - Expose the flag which marks global
49 use Devel::GlobalDestruction;
51 use namespace::clean; # to avoid having an "in_global_destruction" method
54 return if in_global_destruction;
56 do_something_a_little_tricky();
61 Perl's global destruction is a little tricky to deal with WRT finalizers
62 because it's not ordered and objects can sometimes disappear.
64 Writing defensive destructors is hard and annoying, and usually if global
65 destruction is happenning you only need the destructors that free up non
66 process local resources to actually execute.
68 For these constructors you can avoid the mess by simply bailing out if global
69 destruction is in effect.
73 This module uses L<Sub::Exporter::Progressive> so the exports may be renamed,
74 aliased, etc. if L<Sub::Exporter> is present.
78 =item in_global_destruction
80 Returns true if the interpreter is in global destruction. In perl 5.14+, this
81 returns C<${^GLOBAL_PHASE} eq 'DESTRUCT'>, and on earlier perls, it returns the
82 current value of C<PL_dirty>.
88 Yuval Kogman E<lt>nothingmuch@woobling.orgE<gt>
90 Florian Ragwitz E<lt>rafl@debian.orgE<gt>
92 Jesse Luehrs E<lt>doy@tozt.netE<gt>
94 Peter Rabbitson E<lt>ribasushi@cpan.orgE<gt>
96 Arthur Axel 'fREW' Schmidt E<lt>frioux@gmail.comE<gt>
98 Elizabeth Mattijsen E<lt>liz@dijkmat.nlE<gt>
102 Copyright (c) 2008 Yuval Kogman. All rights reserved
103 This program is free software; you can redistribute
104 it and/or modify it under the same terms as Perl itself.