Commit | Line | Data |
b1eebd55 |
1 | package Moo::_Utils; |
6c74d087 |
2 | |
3 | use strictures 1; |
4 | use base qw(Exporter); |
5 | |
fb5074f6 |
6 | our @EXPORT = qw(_getglob _install_modifier _load_module _maybe_load_module); |
6c74d087 |
7 | |
8 | sub _getglob { no strict 'refs'; \*{$_[0]} } |
9 | |
10 | sub _install_modifier { |
6c74d087 |
11 | my ($into, $type, $name, $code) = @_; |
12 | my $ref = ref(my $to_modify = $into->can($name)); |
a165a07f |
13 | |
9187b862 |
14 | require Sub::Defer; |
15 | Sub::Defer::undefer_sub($to_modify); |
a165a07f |
16 | |
9187b862 |
17 | require Class::Method::Modifiers; |
6c74d087 |
18 | Class::Method::Modifiers::install_modifier(@_); |
19 | } |
20 | |
daa05b62 |
21 | our %MAYBE_LOADED; |
22 | |
fb5074f6 |
23 | sub _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 |
30 | sub _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 |
44 | 1; |