use XSAccessor if available
[gitmo/Moo.git] / lib / Class / Tiny / _Utils.pm
1 package Class::Tiny::_Utils;
2
3 use strictures 1;
4 use base qw(Exporter);
5
6 our @EXPORT = qw(_getglob _install_modifier _maybe_load_module);
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));
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);
21   }
22   Class::Method::Modifiers::install_modifier(@_);
23 }
24
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
41 1;