Pureperlize
[p5sagit/Devel-GlobalDestruction.git] / lib / Devel / GlobalDestruction.pm
CommitLineData
a91e8a78 1package Devel::GlobalDestruction;
2
3use strict;
4use warnings;
5
aaa7f60f 6our $VERSION = '0.04';
a91e8a78 7
a91e8a78 8use Sub::Exporter -setup => {
f832e240 9 exports => [ qw(in_global_destruction) ],
10 groups => { default => [ -all ] },
a91e8a78 11};
12
eaac10b5 13if (defined ${^GLOBAL_PHASE}) {
3790e928 14 eval 'sub in_global_destruction () { ${^GLOBAL_PHASE} eq q[DESTRUCT] }';
15}
9aaf3646 16elsif (eval {
17 require XSLoader;
3790e928 18 XSLoader::load(__PACKAGE__, $VERSION);
9aaf3646 19 1;
20}) {
21 # the eval already installed everything, nothing to do
3790e928 22}
9aaf3646 23else {
24 eval <<'PP_IGD' or die $@;
3790e928 25
9aaf3646 26my ($in_global_destruction, $before_is_installed);
a91e8a78 27
9aaf3646 28sub in_global_destruction { $in_global_destruction }
29
30END {
31 # SpeedyCGI runs END blocks every cycle but somehow keeps object instances
32 # hence lying about it seems reasonable...ish
33 $in_global_destruction = 1 unless $CGI::SpeedyCGI::i_am_speedy;
34}
35
36# threads do not execute the global ENDs (it would be stupid). However
37# one can register a new END via simple string eval within a thread, and
38# achieve the same result. A logical place to do this would be CLONE, which
39# is claimed to run in the context of the new thread. However this does
40# not really seem to be the case - any END evaled in a CLONE is ignored :(
41# Hence blatantly hooking threads::create
42
43if ($INC{'threads.pm'}) {
44 my $orig_create = threads->can('create');
45 no warnings 'redefine';
46 *threads::create = sub {
47 { local $@; eval 'END { $in_global_destruction = 1 }' };
48 goto $orig_create;
49 };
50 $before_is_installed = 1;
51}
52
53# just in case threads got loaded after us (silly)
54sub CLONE {
55 unless ($before_is_installed) {
56 require Carp;
57 Carp::croak("You must load the 'threads' module before @{[ __PACKAGE__ ]}");
58 }
59}
a91e8a78 60
9aaf3646 611; # keep eval happy
62
63PP_IGD
64
65}
66
671; # keep require happy
68
69
70__END__
a91e8a78 71
72=head1 NAME
73
761f3ee2 74Devel::GlobalDestruction - Expose the flag which marks global
a91e8a78 75destruction.
76
77=head1 SYNOPSIS
78
f832e240 79 package Foo;
80 use Devel::GlobalDestruction;
a91e8a78 81
f832e240 82 use namespace::clean; # to avoid having an "in_global_destruction" method
a91e8a78 83
f832e240 84 sub DESTROY {
85 return if in_global_destruction;
a91e8a78 86
f832e240 87 do_something_a_little_tricky();
88 }
a91e8a78 89
90=head1 DESCRIPTION
91
92Perl's global destruction is a little tricky to deal with WRT finalizers
93because it's not ordered and objects can sometimes disappear.
94
95Writing defensive destructors is hard and annoying, and usually if global
96destruction is happenning you only need the destructors that free up non
97process local resources to actually execute.
98
99For these constructors you can avoid the mess by simply bailing out if global
100destruction is in effect.
101
102=head1 EXPORTS
103
104This module uses L<Sub::Exporter> so the exports may be renamed, aliased, etc.
105
106=over 4
107
108=item in_global_destruction
109
761f3ee2 110Returns true if the interpreter is in global destruction. In perl 5.14+, this
111returns C<${^GLOBAL_PHASE} eq 'DESTRUCT'>, and on earlier perls, it returns the
112current value of C<PL_dirty>.
a91e8a78 113
114=back
115
116=head1 VERSION CONTROL
117
118This module is maintained using Darcs. You can get the latest version from
119L<http://nothingmuch.woobling.org/code>, and use C<darcs send> to commit
120changes.
121
ec94b9e1 122=head1 AUTHORS
a91e8a78 123
124Yuval Kogman E<lt>nothingmuch@woobling.orgE<gt>
125
ec94b9e1 126Florian Ragwitz E<lt>rafl@debian.orgE<gt>
127
aaa7f60f 128Jesse Luehrs E<lt>doy@tozt.netE<gt>
129
9aaf3646 130Peter Rabbitson E<lt>ribasushi@cpan.orgE<gt>
131
a91e8a78 132=head1 COPYRIGHT
133
f832e240 134 Copyright (c) 2008 Yuval Kogman. All rights reserved
135 This program is free software; you can redistribute
136 it and/or modify it under the same terms as Perl itself.
a91e8a78 137
138=cut