transfer changes to _load_module to Role::Tiny's inlined version and document that...
[gitmo/Role-Tiny.git] / lib / Moo / _Utils.pm
CommitLineData
b1eebd55 1package Moo::_Utils;
6c74d087 2
119014a7 3sub _getglob { \*{$_[0]} }
5ed7d68a 4sub _getstash { \%{"$_[0]::"} }
119014a7 5
2215d4b9 6BEGIN {
7 *lt_5_8_3 = $] < 5.008003
8 ? sub () { 1 }
9 : sub () { 0 }
10 ;
11}
12
6c74d087 13use strictures 1;
14use base qw(Exporter);
15
fb5074f6 16our @EXPORT = qw(_getglob _install_modifier _load_module _maybe_load_module);
6c74d087 17
6c74d087 18sub _install_modifier {
6c74d087 19 my ($into, $type, $name, $code) = @_;
a165a07f 20
dccea57d 21 if (my $to_modify = $into->can($name)) { # CMM will throw for us if not
22 require Sub::Defer;
23 Sub::Defer::undefer_sub($to_modify);
24 }
a165a07f 25
6c74d087 26 Class::Method::Modifiers::install_modifier(@_);
27}
28
daa05b62 29our %MAYBE_LOADED;
30
5e03b55c 31# _load_module is inlined in Role::Tiny - make sure to copy if you update it.
32
fb5074f6 33sub _load_module {
fb5074f6 34 (my $proto = $_[0]) =~ s/::/\//g;
5ed7d68a 35 return 1 if $INC{"${proto}.pm"};
36 # can't just ->can('can') because a sub-package Foo::Bar::Baz
37 # creates a 'Baz::' key in Foo::Bar's symbol table
38 return 1 if grep !/::$/, keys %{_getstash($_[0])||{}};
fb5074f6 39 require "${proto}.pm";
40 return 1;
41}
42
daa05b62 43sub _maybe_load_module {
44 return $MAYBE_LOADED{$_[0]} if exists $MAYBE_LOADED{$_[0]};
45 (my $proto = $_[0]) =~ s/::/\//g;
46 if (eval { require "${proto}.pm"; 1 }) {
47 $MAYBE_LOADED{$_[0]} = 1;
48 } else {
49 if (exists $INC{"${proto}.pm"}) {
50 warn "$_[0] exists but failed to load with error: $@";
51 }
52 $MAYBE_LOADED{$_[0]} = 0;
53 }
54 return $MAYBE_LOADED{$_[0]};
55}
56
6c74d087 571;