just a simple XSLoader for now
[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 use Carp;
7
8 BEGIN {
9     our %dependencies = (
10         'Scalar::Util' => {
11
12 #       VVVVV   CODE TAKEN FROM SCALAR::UTIL   VVVVV
13             'blessed' => do {
14                 *UNIVERSAL::a_sub_not_likely_to_be_here = sub {
15                     my $ref = ref($_[0]);
16
17                     # deviation from Scalar::Util
18                     # XS returns undef, PP returns GLOB.
19                     # let's make that more consistent by having PP return
20                     # undef if it's a GLOB. :/
21
22                     # \*STDOUT would be allowed as an object in PP blessed
23                     # but not XS
24                     return $ref eq 'GLOB' ? undef : $ref;
25                 };
26
27                 sub {
28                     local($@, $SIG{__DIE__}, $SIG{__WARN__});
29                     length(ref($_[0]))
30                         ? eval { $_[0]->a_sub_not_likely_to_be_here }
31                         : undef;
32                 },
33             },
34             'looks_like_number' => sub {
35                 local $_ = shift;
36
37                 # checks from perlfaq4
38                 return 0 if !defined($_) or ref($_);
39                 return 1 if (/^[+-]?\d+$/); # is a +/- integer
40                 return 1 if (/^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/); # a C float
41                 return 1 if ($] >= 5.008 and /^(Inf(inity)?|NaN)$/i) or ($] >= 5.006001 and /^Inf$/i);
42
43                 0;
44             },
45             'reftype' => sub {
46                 local($@, $SIG{__DIE__}, $SIG{__WARN__});
47                 my $r = shift;
48                 my $t;
49
50                 length($t = ref($r)) or return undef;
51
52                 # This eval will fail if the reference is not blessed
53                 eval { $r->a_sub_not_likely_to_be_here; 1 }
54                 ? do {
55                     $t = eval {
56                         # we have a GLOB or an IO. Stringify a GLOB gives it's name
57                         my $q = *$r;
58                         $q =~ /^\*/ ? "GLOB" : "IO";
59                     }
60                     or do {
61                         # OK, if we don't have a GLOB what parts of
62                         # a glob will it populate.
63                         # NOTE: A glob always has a SCALAR
64                         local *glob = $r;
65                         defined *glob{ARRAY} && "ARRAY"
66                             or defined *glob{HASH} && "HASH"
67                             or defined *glob{CODE} && "CODE"
68                             or length(ref(${$r})) ? "REF" : "SCALAR";
69                     }
70                 }
71                 : $t
72             },
73             'openhandle' => sub {
74                 my $fh = shift;
75                 my $rt = reftype($fh) || '';
76
77                 return defined(fileno($fh)) ? $fh : undef
78                     if $rt eq 'IO';
79
80                 if (reftype(\$fh) eq 'GLOB') { # handle  openhandle(*DATA)
81                     $fh = \(my $tmp=$fh);
82                 }
83                 elsif ($rt ne 'GLOB') {
84                     return undef;
85                 }
86
87                 (tied(*$fh) or defined(fileno($fh)))
88                     ? $fh : undef;
89             },
90             weaken => {
91                 loaded => \&Scalar::Util::weaken,
92                 not_loaded => sub { die "Scalar::Util required for weak reference support" },
93             },
94 #       ^^^^^   CODE TAKEN FROM SCALAR::UTIL   ^^^^^
95         },
96         'MRO::Compat' => {
97 #       VVVVV   CODE TAKEN FROM MRO::COMPAT   VVVVV
98             'get_linear_isa' => {
99                 loaded     => \&mro::get_linear_isa,
100                 not_loaded => do {
101                     # this recurses so it isn't pretty
102                     my $code;
103                     $code = sub {
104                         no strict 'refs';
105
106                         my $classname = shift;
107
108                         my @lin = ($classname);
109                         my %stored;
110                         foreach my $parent (@{"$classname\::ISA"}) {
111                             my $plin = $code->($parent);
112                             foreach (@$plin) {
113                                 next if exists $stored{$_};
114                                 push(@lin, $_);
115                                 $stored{$_} = 1;
116                             }
117                         }
118                         return \@lin;
119                     }
120                 },
121             },
122 #       ^^^^^   CODE TAKEN FROM MRO::COMPAT   ^^^^^
123         },
124     );
125
126     our %loaded;
127
128     our @EXPORT_OK = map { keys %$_ } values %dependencies;
129     our %EXPORT_TAGS = (
130         all  => \@EXPORT_OK,
131     );
132
133     for my $module_name (keys %dependencies) {
134         my $loaded = do {
135             local $SIG{__DIE__} = 'DEFAULT';
136             eval "require $module_name; 1";
137         };
138
139         $loaded{$module_name} = $loaded;
140
141         for my $method_name (keys %{ $dependencies{ $module_name } }) {
142             my $producer = $dependencies{$module_name}{$method_name};
143             my $implementation;
144
145             if (ref($producer) eq 'HASH') {
146                 $implementation = $loaded
147                                 ? $producer->{loaded}
148                                 : $producer->{not_loaded};
149             }
150             else {
151                 $implementation = $loaded
152                                 ? $module_name->can($method_name)
153                                 : $producer;
154             }
155
156             no strict 'refs';
157             *{ __PACKAGE__ . '::' . $method_name } = $implementation;
158         }
159     }
160 }
161
162 sub apply_all_roles {
163     my $meta = Mouse::Meta::Class->initialize(shift);
164     my $role  = shift;
165     confess "Mouse::Util only supports 'apply_all_roles' on individual roles at a time" if @_;
166
167     Mouse::load_class($role);
168     $role->meta->apply($meta);
169 }
170
171 1;
172
173 __END__
174
175 =head1 NAME
176
177 Mouse::Util - features, with or without their dependencies
178
179 =head1 IMPLEMENTATIONS FOR
180
181 =head2 L<MRO::Compat>
182
183 =head3 get_linear_isa
184
185 =head2 L<Scalar::Util>
186
187 =head3 blessed
188
189 =head3 looks_like_number
190
191 =head3 reftype
192
193 =head3 openhandle
194
195 =head3 weaken
196
197 C<weaken> I<must> be implemented in XS. If the user tries to use C<weaken>
198 without L<Scalar::Util>, an error is thrown.
199
200 =head2 Test::Exception
201
202 =head3 throws_ok
203
204 =head3 lives_ok
205
206 =cut
207