Bring back 5.8.1 support
[gitmo/Role-Tiny.git] / lib / Moo / _Utils.pm
1 package Moo::_Utils;
2
3 sub _getglob { \*{$_[0]} }
4 sub _getstash { \%{"$_[0]::"} }
5
6 BEGIN {
7   *lt_5_8_3 = $] < 5.008003
8     ? sub () { 1 }
9     : sub () { 0 }
10   ;
11 }
12
13 use strictures 1;
14 use base qw(Exporter);
15
16 our @EXPORT = qw(_getglob _install_modifier _load_module _maybe_load_module);
17
18 sub _install_modifier {
19   my ($into, $type, $name, $code) = @_;
20
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   }
25
26   Class::Method::Modifiers::install_modifier(@_);
27 }
28
29 our %MAYBE_LOADED;
30
31 sub _load_module {
32   (my $proto = $_[0]) =~ s/::/\//g;
33   return 1 if $INC{"${proto}.pm"};
34   # can't just ->can('can') because a sub-package Foo::Bar::Baz
35   # creates a 'Baz::' key in Foo::Bar's symbol table
36   return 1 if grep !/::$/, keys %{_getstash($_[0])||{}};
37   require "${proto}.pm";
38   return 1;
39 }
40
41 sub _maybe_load_module {
42   return $MAYBE_LOADED{$_[0]} if exists $MAYBE_LOADED{$_[0]};
43   (my $proto = $_[0]) =~ s/::/\//g;
44   if (eval { require "${proto}.pm"; 1 }) {
45     $MAYBE_LOADED{$_[0]} = 1;
46   } else {
47     if (exists $INC{"${proto}.pm"}) {
48       warn "$_[0] exists but failed to load with error: $@";
49     }
50     $MAYBE_LOADED{$_[0]} = 0;
51   }
52   return $MAYBE_LOADED{$_[0]};
53 }
54
55 1;