Version 0.04
[p5sagit/Devel-GlobalDestruction.git] / lib / Devel / GlobalDestruction.pm
1 #!/usr/bin/perl
2
3 package Devel::GlobalDestruction;
4
5 use strict;
6 use warnings;
7
8 use XSLoader;
9
10 our $VERSION = '0.04';
11
12 use Sub::Exporter -setup => {
13         exports => [ qw(in_global_destruction) ],
14         groups  => { default => [ -all ] },
15 };
16
17 if (defined ${^GLOBAL_PHASE}) {
18     eval 'sub in_global_destruction () { ${^GLOBAL_PHASE} eq q[DESTRUCT] }';
19 }
20 else {
21     XSLoader::load(__PACKAGE__, $VERSION);
22 }
23
24 __PACKAGE__
25
26 __END__
27
28 =pod
29
30 =head1 NAME
31
32 Devel::GlobalDestruction - Expose the flag which marks global
33 destruction.
34
35 =head1 SYNOPSIS
36
37         package Foo;
38         use Devel::GlobalDestruction;
39
40         use namespace::clean; # to avoid having an "in_global_destruction" method
41
42         sub DESTROY {
43                 return if in_global_destruction;
44
45                 do_something_a_little_tricky();
46         }
47
48 =head1 DESCRIPTION
49
50 Perl's global destruction is a little tricky to deal with WRT finalizers
51 because it's not ordered and objects can sometimes disappear.
52
53 Writing defensive destructors is hard and annoying, and usually if global
54 destruction is happenning you only need the destructors that free up non
55 process local resources to actually execute.
56
57 For these constructors you can avoid the mess by simply bailing out if global
58 destruction is in effect.
59
60 =head1 EXPORTS
61
62 This module uses L<Sub::Exporter> so the exports may be renamed, aliased, etc.
63
64 =over 4
65
66 =item in_global_destruction
67
68 Returns true if the interpreter is in global destruction. In perl 5.14+, this
69 returns C<${^GLOBAL_PHASE} eq 'DESTRUCT'>, and on earlier perls, it returns the
70 current value of C<PL_dirty>.
71
72 =back
73
74 =head1 VERSION CONTROL
75
76 This module is maintained using Darcs. You can get the latest version from
77 L<http://nothingmuch.woobling.org/code>, and use C<darcs send> to commit
78 changes.
79
80 =head1 AUTHORS
81
82 Yuval Kogman E<lt>nothingmuch@woobling.orgE<gt>
83
84 Florian Ragwitz E<lt>rafl@debian.orgE<gt>
85
86 Jesse Luehrs E<lt>doy@tozt.netE<gt>
87
88 =head1 COPYRIGHT
89
90         Copyright (c) 2008 Yuval Kogman. All rights reserved
91         This program is free software; you can redistribute
92         it and/or modify it under the same terms as Perl itself.
93
94 =cut
95
96