fix silly bug in _load_module
[gitmo/Moo.git] / lib / Moo / _Utils.pm
CommitLineData
b1eebd55 1package Moo::_Utils;
6c74d087 2
0fe2ad8c 3no warnings 'once'; # guard against -w
4
119014a7 5sub _getglob { \*{$_[0]} }
5ed7d68a 6sub _getstash { \%{"$_[0]::"} }
119014a7 7
2215d4b9 8BEGIN {
9 *lt_5_8_3 = $] < 5.008003
10 ? sub () { 1 }
11 : sub () { 0 }
12 ;
13}
14
6c74d087 15use strictures 1;
cf62c989 16use Module::Runtime qw(require_module);
6c74d087 17use base qw(Exporter);
3c739397 18use Moo::_mro;
6c74d087 19
3c739397 20our @EXPORT = qw(
21 _getglob _install_modifier _load_module _maybe_load_module
3362e41c 22 _get_linear_isa _getstash
3c739397 23);
6c74d087 24
6c74d087 25sub _install_modifier {
6c74d087 26 my ($into, $type, $name, $code) = @_;
a165a07f 27
dccea57d 28 if (my $to_modify = $into->can($name)) { # CMM will throw for us if not
7568ba55 29 require Sub::Defer;
dccea57d 30 Sub::Defer::undefer_sub($to_modify);
31 }
a165a07f 32
6c74d087 33 Class::Method::Modifiers::install_modifier(@_);
34}
35
daa05b62 36our %MAYBE_LOADED;
37
fb5074f6 38sub _load_module {
fb5074f6 39 (my $proto = $_[0]) =~ s/::/\//g;
5ed7d68a 40 return 1 if $INC{"${proto}.pm"};
41 # can't just ->can('can') because a sub-package Foo::Bar::Baz
42 # creates a 'Baz::' key in Foo::Bar's symbol table
2a577e53 43 my $stash = _getstash($_[0])||{};
44 return 1 if grep +(!ref($_) and *$_{CODE}), values %$stash;
cf62c989 45 require_module($_[0]);
fb5074f6 46 return 1;
47}
48
daa05b62 49sub _maybe_load_module {
50 return $MAYBE_LOADED{$_[0]} if exists $MAYBE_LOADED{$_[0]};
51 (my $proto = $_[0]) =~ s/::/\//g;
59812c87 52 local $@;
daa05b62 53 if (eval { require "${proto}.pm"; 1 }) {
54 $MAYBE_LOADED{$_[0]} = 1;
55 } else {
56 if (exists $INC{"${proto}.pm"}) {
57 warn "$_[0] exists but failed to load with error: $@";
58 }
59 $MAYBE_LOADED{$_[0]} = 0;
60 }
61 return $MAYBE_LOADED{$_[0]};
62}
63
3c739397 64sub _get_linear_isa {
65 return mro::get_linear_isa($_[0]);
66}
67
68our $_in_global_destruction = 0;
69END { $_in_global_destruction = 1 }
70
59812c87 71sub STANDARD_DESTROY {
72 my $self = shift;
73
74 my $e = do {
75 local $?;
76 local $@;
77 eval {
78 $self->DEMOLISHALL($_in_global_destruction);
79 };
80 $@;
81 };
82
83 no warnings 'misc';
84 die $e if $e; # rethrow
85}
86
6c74d087 871;