36a1feb73bb33f4bd076cbe9ea68523b77bb877f
[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 Devel::GlobalDestruction::XS;
23   *in_global_destruction = \&Devel::GlobalDestruction::XS::in_global_destruction;
24   1;
25 }) {
26   # the eval already installed everything, nothing to do
27 }
28 else {
29   # internally, PL_main_start is nulled immediately before entering global destruction
30   # and we can use B to detect that.  It will also be null before the main runloop starts,
31   # so we check install a CHECK if needed to detect that.
32   require B;
33   my $started = !B::main_start()->isa(q[B::NULL]);
34   unless ($started) {
35     eval 'CHECK { $started = 1 }; 1'
36       or die $@;
37   }
38   eval 'sub in_global_destruction () { $started && B::main_start()->isa(q[B::NULL]) }; 1'
39     or die $@;
40 }
41
42 1;  # keep require happy
43
44
45 __END__
46
47 =head1 NAME
48
49 Devel::GlobalDestruction - Expose the flag which marks global
50 destruction.
51
52 =head1 SYNOPSIS
53
54     package Foo;
55     use Devel::GlobalDestruction;
56
57     use namespace::clean; # to avoid having an "in_global_destruction" method
58
59     sub DESTROY {
60         return if in_global_destruction;
61
62         do_something_a_little_tricky();
63     }
64
65 =head1 DESCRIPTION
66
67 Perl's global destruction is a little tricky to deal with WRT finalizers
68 because it's not ordered and objects can sometimes disappear.
69
70 Writing defensive destructors is hard and annoying, and usually if global
71 destruction is happenning you only need the destructors that free up non
72 process local resources to actually execute.
73
74 For these constructors you can avoid the mess by simply bailing out if global
75 destruction is in effect.
76
77 =head1 EXPORTS
78
79 This module uses L<Sub::Exporter::Progressive> so the exports may be renamed,
80 aliased, etc. if L<Sub::Exporter> is present.
81
82 =over 4
83
84 =item in_global_destruction
85
86 Returns true if the interpreter is in global destruction. In perl 5.14+, this
87 returns C<${^GLOBAL_PHASE} eq 'DESTRUCT'>, and on earlier perls, detects it using
88 the value of C<PL_main_start> or C<PL_dirty>.
89
90 =back
91
92 =head1 AUTHORS
93
94 Yuval Kogman E<lt>nothingmuch@woobling.orgE<gt>
95
96 Florian Ragwitz E<lt>rafl@debian.orgE<gt>
97
98 Jesse Luehrs E<lt>doy@tozt.netE<gt>
99
100 Peter Rabbitson E<lt>ribasushi@cpan.orgE<gt>
101
102 Arthur Axel 'fREW' Schmidt E<lt>frioux@gmail.comE<gt>
103
104 Elizabeth Mattijsen E<lt>liz@dijkmat.nlE<gt>
105
106 Greham Knop E<lt>haarg@haarg.orgE<gt>
107
108 =head1 COPYRIGHT
109
110     Copyright (c) 2008 Yuval Kogman. All rights reserved
111     This program is free software; you can redistribute
112     it and/or modify it under the same terms as Perl itself.
113
114 =cut