Commit | Line | Data |
b1eebd55 |
1 | package Moo::_Utils; |
6c74d087 |
2 | |
3 | use strictures 1; |
4 | use base qw(Exporter); |
5 | |
daa05b62 |
6 | our @EXPORT = qw(_getglob _install_modifier _maybe_load_module); |
6c74d087 |
7 | |
8 | sub _getglob { no strict 'refs'; \*{$_[0]} } |
9 | |
10 | sub _install_modifier { |
11 | require Class::Method::Modifiers; |
12 | my ($into, $type, $name, $code) = @_; |
13 | my $ref = ref(my $to_modify = $into->can($name)); |
a165a07f |
14 | |
15 | # if it isn't CODE, then either we're about to die, or it's a blessed |
16 | # coderef - if it's a blessed coderef it might be deferred, and the |
17 | # user's already doing something clever so a minor speed hit is meh. |
18 | |
19 | if ($ref && $ref ne 'CODE') { |
20 | require Sub::Defer; Sub::Defer::undefer_sub($to_modify); |
6c74d087 |
21 | } |
22 | Class::Method::Modifiers::install_modifier(@_); |
23 | } |
24 | |
daa05b62 |
25 | our %MAYBE_LOADED; |
26 | |
27 | sub _maybe_load_module { |
28 | return $MAYBE_LOADED{$_[0]} if exists $MAYBE_LOADED{$_[0]}; |
29 | (my $proto = $_[0]) =~ s/::/\//g; |
30 | if (eval { require "${proto}.pm"; 1 }) { |
31 | $MAYBE_LOADED{$_[0]} = 1; |
32 | } else { |
33 | if (exists $INC{"${proto}.pm"}) { |
34 | warn "$_[0] exists but failed to load with error: $@"; |
35 | } |
36 | $MAYBE_LOADED{$_[0]} = 0; |
37 | } |
38 | return $MAYBE_LOADED{$_[0]}; |
39 | } |
40 | |
6c74d087 |
41 | 1; |