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 Devel::GlobalDestruction ();
14 use base qw(Exporter);
18 _getglob _install_modifier _load_module _maybe_load_module
19 _get_linear_isa _getstash _install_coderef _name_coderef
20 _unimport_coderefs _in_global_destruction
23 sub _in_global_destruction ();
24 *_in_global_destruction = \&Devel::GlobalDestruction::in_global_destruction;
26 sub _install_modifier {
27 my ($into, $type, $name, $code) = @_;
29 if (my $to_modify = $into->can($name)) { # CMM will throw for us if not
31 Sub::Defer::undefer_sub($to_modify);
34 Class::Method::Modifiers::install_modifier(@_);
40 (my $proto = $_[0]) =~ s/::/\//g;
41 return 1 if $INC{"${proto}.pm"};
42 # can't just ->can('can') because a sub-package Foo::Bar::Baz
43 # creates a 'Baz::' key in Foo::Bar's symbol table
44 my $stash = _getstash($_[0])||{};
45 return 1 if grep +(!ref($_) and *$_{CODE}), values %$stash;
46 require_module($_[0]);
50 sub _maybe_load_module {
51 return $MAYBE_LOADED{$_[0]} if exists $MAYBE_LOADED{$_[0]};
52 (my $proto = $_[0]) =~ s/::/\//g;
54 if (eval { require "${proto}.pm"; 1 }) {
55 $MAYBE_LOADED{$_[0]} = 1;
57 if (exists $INC{"${proto}.pm"}) {
58 warn "$_[0] exists but failed to load with error: $@";
60 $MAYBE_LOADED{$_[0]} = 0;
62 return $MAYBE_LOADED{$_[0]};
66 return mro::get_linear_isa($_[0]);
69 sub _install_coderef {
70 no warnings 'redefine';
71 *{_getglob($_[0])} = _name_coderef(@_);
75 shift if @_ > 2; # three args is (target, name, sub)
76 can_haz_subname ? Sub::Name::subname(@_) : $_[1];
79 sub _unimport_coderefs {
80 my ($target, $info) = @_;
81 return unless $info and my $exports = $info->{exports};
82 my %rev = reverse %$exports;
83 my $stash = _getstash($target);
84 foreach my $name (keys %$exports) {
85 if ($stash->{$name} and defined(&{$stash->{$name}})) {
86 if ($rev{$target->can($name)}) {
87 my $old = delete $stash->{$name};
88 my $full_name = join('::',$target,$name);
89 # Copy everything except the code slot back into place (e.g. $has)
90 foreach my $type (qw(SCALAR HASH ARRAY IO)) {
91 next unless defined(*{$old}{$type});
93 *$full_name = *{$old}{$type};
100 sub STANDARD_DESTROY {
107 $self->DEMOLISHALL(_in_global_destruction);
113 die $e if $e; # rethrow