25b895881e2cd4f7a12ce3e62f89283c53ea7962
[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   # internally, PL_main_start is nulled immediately before entering global destruction
30   # and we can use B to detect that
31   require B;
32   eval 'sub in_global_destruction () { B::main_start()->isa(q[B::NULL]) }; 1'
33     or die $@;
34 }
35
36 1;  # keep require happy
37
38
39 __END__
40
41 =head1 NAME
42
43 Devel::GlobalDestruction - Expose the flag which marks global
44 destruction.
45
46 =head1 SYNOPSIS
47
48     package Foo;
49     use Devel::GlobalDestruction;
50
51     use namespace::clean; # to avoid having an "in_global_destruction" method
52
53     sub DESTROY {
54         return if in_global_destruction;
55
56         do_something_a_little_tricky();
57     }
58
59 =head1 DESCRIPTION
60
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.
63
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.
67
68 For these constructors you can avoid the mess by simply bailing out if global
69 destruction is in effect.
70
71 =head1 EXPORTS
72
73 This module uses L<Sub::Exporter::Progressive> so the exports may be renamed,
74 aliased, etc. if L<Sub::Exporter> is present.
75
76 =over 4
77
78 =item in_global_destruction
79
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>.
83
84 =back
85
86 =head1 AUTHORS
87
88 Yuval Kogman E<lt>nothingmuch@woobling.orgE<gt>
89
90 Florian Ragwitz E<lt>rafl@debian.orgE<gt>
91
92 Jesse Luehrs E<lt>doy@tozt.netE<gt>
93
94 Peter Rabbitson E<lt>ribasushi@cpan.orgE<gt>
95
96 Arthur Axel 'fREW' Schmidt E<lt>frioux@gmail.comE<gt>
97
98 Elizabeth Mattijsen E<lt>liz@dijkmat.nlE<gt>
99
100 =head1 COPYRIGHT
101
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.
105
106 =cut