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
22 require Devel::GlobalDestruction::XS;
24 *in_global_destruction = \&Devel::GlobalDestruction::XS::in_global_destruction;
27 # the eval already installed everything, nothing to do
30 # internally, PL_main_cv is set to Nullcv immediately before entering
31 # global destruction and we can use B to detect that. B::main_cv will
32 # only ever be a B::CV or a B::SPECIAL that is a reference to 0
34 eval 'sub in_global_destruction () { ${B::main_cv()} == 0 }; 1'
38 1; # keep require happy
45 Devel::GlobalDestruction - Provides function returning the equivalent of
46 C<${^GLOBAL_PHASE} eq 'DESTRUCT'> for older perls.
51 use Devel::GlobalDestruction;
53 use namespace::clean; # to avoid having an "in_global_destruction" method
56 return if in_global_destruction;
58 do_something_a_little_tricky();
63 Perl's global destruction is a little tricky to deal with WRT finalizers
64 because it's not ordered and objects can sometimes disappear.
66 Writing defensive destructors is hard and annoying, and usually if global
67 destruction is happening you only need the destructors that free up non
68 process local resources to actually execute.
70 For these constructors you can avoid the mess by simply bailing out if global
71 destruction is in effect.
75 This module uses L<Sub::Exporter::Progressive> so the exports may be renamed,
76 aliased, etc. if L<Sub::Exporter> is present.
80 =item in_global_destruction
82 Returns true if the interpreter is in global destruction. In perl 5.14+, this
83 returns C<${^GLOBAL_PHASE} eq 'DESTRUCT'>, and on earlier perls, detects it using
84 the value of C<PL_main_cv> or C<PL_dirty>.
90 Yuval Kogman E<lt>nothingmuch@woobling.orgE<gt>
92 Florian Ragwitz E<lt>rafl@debian.orgE<gt>
94 Jesse Luehrs E<lt>doy@tozt.netE<gt>
96 Peter Rabbitson E<lt>ribasushi@cpan.orgE<gt>
98 Arthur Axel 'fREW' Schmidt E<lt>frioux@gmail.comE<gt>
100 Elizabeth Mattijsen E<lt>liz@dijkmat.nlE<gt>
102 Greham Knop E<lt>haarg@haarg.orgE<gt>
106 Copyright (c) 2008 Yuval Kogman. All rights reserved
107 This program is free software; you can redistribute
108 it and/or modify it under the same terms as Perl itself.