3 no warnings 'once'; # guard against -w
5 sub _getglob { \*{$_[0]} }
6 sub _getstash { \%{"$_[0]::"} }
8 use constant lt_5_8_3 => ( $] < 5.008003 ) ? 1 : 0;
9 use constant can_haz_subname => eval { require Sub::Name };
12 use Module::Runtime qw(require_module);
13 use base qw(Exporter);
17 _getglob _install_modifier _load_module _maybe_load_module
18 _get_linear_isa _getstash _install_coderef _name_coderef
19 _in_global_destruction
22 sub _in_global_destruction ();
24 sub _install_modifier {
25 my ($into, $type, $name, $code) = @_;
27 if (my $to_modify = $into->can($name)) { # CMM will throw for us if not
29 Sub::Defer::undefer_sub($to_modify);
32 Class::Method::Modifiers::install_modifier(@_);
38 (my $proto = $_[0]) =~ s/::/\//g;
39 return 1 if $INC{"${proto}.pm"};
40 # can't just ->can('can') because a sub-package Foo::Bar::Baz
41 # creates a 'Baz::' key in Foo::Bar's symbol table
42 my $stash = _getstash($_[0])||{};
43 return 1 if grep +(!ref($_) and *$_{CODE}), values %$stash;
44 require_module($_[0]);
48 sub _maybe_load_module {
49 return $MAYBE_LOADED{$_[0]} if exists $MAYBE_LOADED{$_[0]};
50 (my $proto = $_[0]) =~ s/::/\//g;
52 if (eval { require "${proto}.pm"; 1 }) {
53 $MAYBE_LOADED{$_[0]} = 1;
55 if (exists $INC{"${proto}.pm"}) {
56 warn "$_[0] exists but failed to load with error: $@";
58 $MAYBE_LOADED{$_[0]} = 0;
60 return $MAYBE_LOADED{$_[0]};
64 return mro::get_linear_isa($_[0]);
67 sub _install_coderef {
68 *{_getglob($_[0])} = _name_coderef(@_);
72 shift if @_ > 2; # three args is (target, name, sub)
73 can_haz_subname ? Sub::Name::subname(@_) : $_[1];
76 sub STANDARD_DESTROY {
83 $self->DEMOLISHALL(_in_global_destruction);
89 die $e if $e; # rethrow
92 if (eval { require_module('Devel::GlobalDestruction') }) {
93 *_in_global_destruction = \&Devel::GlobalDestruction::in_global_destruction;
94 } elsif (defined ${^GLOBAL_PHASE}) {
95 eval 'sub _in_global_destruction () { ${^GLOBAL_PHASE} eq q[DESTRUCT] }';
97 eval <<'PP_IGD' or die $@;
99 my ($in_global_destruction, $before_is_installed);
101 sub _in_global_destruction () { $in_global_destruction }
104 # SpeedyCGI runs END blocks every cycle but somehow keeps object instances
105 # hence lying about it seems reasonable...ish
106 $in_global_destruction = 1 unless $CGI::SpeedyCGI::i_am_speedy;
109 # threads do not execute the global ENDs (it would be stupid). However
110 # one can register a new END via simple string eval within a thread, and
111 # achieve the same result. A logical place to do this would be CLONE, which
112 # is claimed to run in the context of the new thread. However this does
113 # not really seem to be the case - any END evaled in a CLONE is ignored :(
114 # Hence blatantly hooking threads::create
116 if ($INC{'threads.pm'}) {
117 my $orig_create = threads->can('create');
118 no warnings 'redefine';
119 *threads::create = sub {
120 { local $@; eval 'END { $in_global_destruction = 1 }' };
123 $before_is_installed = 1;
126 # just in case threads got loaded after us (silly)
128 unless ($before_is_installed) {
130 Carp::croak("You must load the 'threads' module before @{[ __PACKAGE__ ]}");