.gitignore
[gitmo/Role-Tiny.git] / lib / Moo / _Utils.pm
CommitLineData
b1eebd55 1package Moo::_Utils;
6c74d087 2
119014a7 3sub _getglob { \*{$_[0]} }
4
6c74d087 5use strictures 1;
6use base qw(Exporter);
7
fb5074f6 8our @EXPORT = qw(_getglob _install_modifier _load_module _maybe_load_module);
6c74d087 9
6c74d087 10sub _install_modifier {
6c74d087 11 my ($into, $type, $name, $code) = @_;
a165a07f 12
dccea57d 13 if (my $to_modify = $into->can($name)) { # CMM will throw for us if not
14 require Sub::Defer;
15 Sub::Defer::undefer_sub($to_modify);
16 }
a165a07f 17
6c74d087 18 Class::Method::Modifiers::install_modifier(@_);
19}
20
daa05b62 21our %MAYBE_LOADED;
22
fb5074f6 23sub _load_module {
24 return 1 if $_[0]->can('can');
25 (my $proto = $_[0]) =~ s/::/\//g;
26 require "${proto}.pm";
27 return 1;
28}
29
daa05b62 30sub _maybe_load_module {
31 return $MAYBE_LOADED{$_[0]} if exists $MAYBE_LOADED{$_[0]};
32 (my $proto = $_[0]) =~ s/::/\//g;
33 if (eval { require "${proto}.pm"; 1 }) {
34 $MAYBE_LOADED{$_[0]} = 1;
35 } else {
36 if (exists $INC{"${proto}.pm"}) {
37 warn "$_[0] exists but failed to load with error: $@";
38 }
39 $MAYBE_LOADED{$_[0]} = 0;
40 }
41 return $MAYBE_LOADED{$_[0]};
42}
43
6c74d087 441;