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 _unimport_coderefs _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 no warnings 'redefine';
69 *{_getglob($_[0])} = _name_coderef(@_);
73 shift if @_ > 2; # three args is (target, name, sub)
74 can_haz_subname ? Sub::Name::subname(@_) : $_[1];
77 sub _unimport_coderefs {
78 my ($target, $info) = @_;
79 return unless $info and my $exports = $info->{exports};
80 my %rev = reverse %$exports;
81 my $stash = _getstash($target);
82 foreach my $name (keys %$exports) {
83 if ($stash->{$name} and defined(&{$stash->{$name}})) {
84 if ($rev{$target->can($name)}) {
85 delete $stash->{$name};
92 sub STANDARD_DESTROY {
99 $self->DEMOLISHALL(_in_global_destruction);
105 die $e if $e; # rethrow
108 if (eval { require_module('Devel::GlobalDestruction') }) {
109 *_in_global_destruction = \&Devel::GlobalDestruction::in_global_destruction;
110 } elsif (defined ${^GLOBAL_PHASE}) {
111 eval 'sub _in_global_destruction () { ${^GLOBAL_PHASE} eq q[DESTRUCT] }';
113 eval <<'PP_IGD' or die $@;
115 my ($in_global_destruction, $before_is_installed);
117 sub _in_global_destruction () { $in_global_destruction }
120 # SpeedyCGI runs END blocks every cycle but somehow keeps object instances
121 # hence lying about it seems reasonable...ish
122 $in_global_destruction = 1 unless $CGI::SpeedyCGI::i_am_speedy;
125 # threads do not execute the global ENDs (it would be stupid). However
126 # one can register a new END via simple string eval within a thread, and
127 # achieve the same result. A logical place to do this would be CLONE, which
128 # is claimed to run in the context of the new thread. However this does
129 # not really seem to be the case - any END evaled in a CLONE is ignored :(
130 # Hence blatantly hooking threads::create
132 if ($INC{'threads.pm'}) {
133 my $orig_create = threads->can('create');
134 no warnings 'redefine';
135 *threads::create = sub {
136 { local $@; eval 'END { $in_global_destruction = 1 }' };
139 $before_is_installed = 1;
142 # just in case threads got loaded after us (silly)
144 unless ($before_is_installed) {
146 Carp::croak("You must load the 'threads' module before @{[ __PACKAGE__ ]}");