check B::main_start for old perl fallback
[p5sagit/Devel-GlobalDestruction.git] / lib / Devel / GlobalDestruction.pm
1 package Devel::GlobalDestruction;
2
3 use strict;
4 use warnings;
5
6 our $VERSION = '0.09';
7
8 use Sub::Exporter::Progressive -setup => {
9   exports => [ qw(in_global_destruction) ],
10   groups  => { default => [ -all ] },
11 };
12
13 # we run 5.14+ - everything is in core
14 #
15 if (defined ${^GLOBAL_PHASE}) {
16   eval 'sub in_global_destruction () { ${^GLOBAL_PHASE} eq q[DESTRUCT] }; 1'
17     or die $@;
18 }
19 # try to load the xs version if it was compiled
20 #
21 elsif (eval {
22   require XSLoader;
23   XSLoader::load(__PACKAGE__, $VERSION);
24   1;
25 }) {
26   # the eval already installed everything, nothing to do
27 }
28 else {
29   require B;
30   eval 'sub in_global_destruction () { B::main_start()->isa(q[B::NULL]) }; 1'
31     or die $@;
32 }
33
34 1;  # keep require happy
35
36
37 __END__
38
39 =head1 NAME
40
41 Devel::GlobalDestruction - Expose the flag which marks global
42 destruction.
43
44 =head1 SYNOPSIS
45
46     package Foo;
47     use Devel::GlobalDestruction;
48
49     use namespace::clean; # to avoid having an "in_global_destruction" method
50
51     sub DESTROY {
52         return if in_global_destruction;
53
54         do_something_a_little_tricky();
55     }
56
57 =head1 DESCRIPTION
58
59 Perl's global destruction is a little tricky to deal with WRT finalizers
60 because it's not ordered and objects can sometimes disappear.
61
62 Writing defensive destructors is hard and annoying, and usually if global
63 destruction is happenning you only need the destructors that free up non
64 process local resources to actually execute.
65
66 For these constructors you can avoid the mess by simply bailing out if global
67 destruction is in effect.
68
69 =head1 EXPORTS
70
71 This module uses L<Sub::Exporter::Progressive> so the exports may be renamed,
72 aliased, etc. if L<Sub::Exporter> is present.
73
74 =over 4
75
76 =item in_global_destruction
77
78 Returns true if the interpreter is in global destruction. In perl 5.14+, this
79 returns C<${^GLOBAL_PHASE} eq 'DESTRUCT'>, and on earlier perls, it returns the
80 current value of C<PL_dirty>.
81
82 =back
83
84 =head1 AUTHORS
85
86 Yuval Kogman E<lt>nothingmuch@woobling.orgE<gt>
87
88 Florian Ragwitz E<lt>rafl@debian.orgE<gt>
89
90 Jesse Luehrs E<lt>doy@tozt.netE<gt>
91
92 Peter Rabbitson E<lt>ribasushi@cpan.orgE<gt>
93
94 Arthur Axel 'fREW' Schmidt E<lt>frioux@gmail.comE<gt>
95
96 Elizabeth Mattijsen E<lt>liz@dijkmat.nlE<gt>
97
98 =head1 COPYRIGHT
99
100     Copyright (c) 2008 Yuval Kogman. All rights reserved
101     This program is free software; you can redistribute
102     it and/or modify it under the same terms as Perl itself.
103
104 =cut