reftype (which is used by openhandle but not Mouse directly)
[gitmo/Mouse.git] / lib / Mouse / Util.pm
1 #!/usr/bin/env perl
2 package Mouse::Util;
3 use strict;
4 use warnings;
5 use base 'Exporter';
6
7 our %dependencies = (
8     'Scalar::Util' => {
9         'blessed' => do {
10             do {
11                 no strict 'refs';
12                 *UNIVERSAL::a_sub_not_likely_to_be_here = sub {
13                     my $ref = ref($_[0]);
14
15                     # deviation from Scalar::Util
16                     # XS returns undef, PP returns GLOB.
17                     # let's make that more consistent by having PP return
18                     # undef if it's a GLOB. :/
19
20                     # \*STDOUT would be allowed as an object in PP blessed
21                     # but not XS
22                     return $ref eq 'GLOB' ? undef : $ref;
23                 };
24             };
25
26             sub {
27                 local($@, $SIG{__DIE__}, $SIG{__WARN__});
28                 length(ref($_[0]))
29                     ? eval { $_[0]->a_sub_not_likely_to_be_here }
30                     : undef;
31             },
32         },
33         'looks_like_number' => sub {
34             local $_ = shift;
35
36             # checks from perlfaq4
37             return 0 if !defined($_) or ref($_);
38             return 1 if (/^[+-]?\d+$/); # is a +/- integer
39             return 1 if (/^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/); # a C float
40             return 1 if ($] >= 5.008 and /^(Inf(inity)?|NaN)$/i) or ($] >= 5.006001 and /^Inf$/i);
41
42             0;
43         },
44         'reftype' => sub {
45             local($@, $SIG{__DIE__}, $SIG{__WARN__});
46             my $r = shift;
47             my $t;
48
49             length($t = ref($r)) or return undef;
50
51             # This eval will fail if the reference is not blessed
52             eval { $r->a_sub_not_likely_to_be_here; 1 }
53             ? do {
54                 $t = eval {
55                     # we have a GLOB or an IO. Stringify a GLOB gives it's name
56                     my $q = *$r;
57                     $q =~ /^\*/ ? "GLOB" : "IO";
58                 }
59                 or do {
60                     # OK, if we don't have a GLOB what parts of
61                     # a glob will it populate.
62                     # NOTE: A glob always has a SCALAR
63                     local *glob = $r;
64                     defined *glob{ARRAY} && "ARRAY"
65                         or defined *glob{HASH} && "HASH"
66                         or defined *glob{CODE} && "CODE"
67                         or length(ref(${$r})) ? "REF" : "SCALAR";
68                 }
69             }
70             : $t
71         },
72     },
73     'MRO::Compat' => {
74         'get_linear_isa' => {
75             loaded     => \&mro::get_linear_isa,
76             not_loaded => do {
77                 # this recurses so it isn't pretty
78                 my $code;
79                 $code = sub {
80                     no strict 'refs';
81
82                     my $classname = shift;
83
84                     my @lin = ($classname);
85                     my %stored;
86                     foreach my $parent (@{"$classname\::ISA"}) {
87                         my $plin = $code->($parent);
88                         foreach (@$plin) {
89                             next if exists $stored{$_};
90                             push(@lin, $_);
91                             $stored{$_} = 1;
92                         }
93                     }
94                     return \@lin;
95                 }
96             },
97         },
98     },
99 );
100
101 our @EXPORT_OK = map { keys %$_ } values %dependencies;
102
103 for my $module_name (keys %dependencies) {
104     (my $file = "$module_name.pm") =~ s{::}{/}g;
105
106     my $loaded = do {
107         local $SIG{__DIE__} = 'DEFAULT';
108         eval "require '$file'; 1";
109     };
110
111     for my $method_name (keys %{ $dependencies{ $module_name } }) {
112         my $producer = $dependencies{$module_name}{$method_name};
113         my $implementation;
114
115         if (ref($producer) eq 'HASH') {
116             $implementation = $loaded
117                             ? $producer->{loaded}
118                             : $producer->{not_loaded};
119         }
120         else {
121             $implementation = $loaded
122                             ? $module_name->can($method_name)
123                             : $producer;
124         }
125
126         no strict 'refs';
127         *{ __PACKAGE__ . '::' . $method_name } = $implementation;
128     }
129 }
130
131 1;
132