Run all of Mouse::Util at BEGIN time
[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 BEGIN {
8     our %dependencies = (
9         'Scalar::Util' => {
10
11 #       VVVVV   CODE TAKEN FROM SCALAR::UTIL   VVVVV
12             'blessed' => do {
13                 *UNIVERSAL::a_sub_not_likely_to_be_here = sub {
14                     my $ref = ref($_[0]);
15
16                     # deviation from Scalar::Util
17                     # XS returns undef, PP returns GLOB.
18                     # let's make that more consistent by having PP return
19                     # undef if it's a GLOB. :/
20
21                     # \*STDOUT would be allowed as an object in PP blessed
22                     # but not XS
23                     return $ref eq 'GLOB' ? undef : $ref;
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             'openhandle' => sub {
73                 my $fh = shift;
74                 my $rt = reftype($fh) || '';
75
76                 return defined(fileno($fh)) ? $fh : undef
77                     if $rt eq 'IO';
78
79                 if (reftype(\$fh) eq 'GLOB') { # handle  openhandle(*DATA)
80                     $fh = \(my $tmp=$fh);
81                 }
82                 elsif ($rt ne 'GLOB') {
83                     return undef;
84                 }
85
86                 (tied(*$fh) or defined(fileno($fh)))
87                     ? $fh : undef;
88             },
89             weaken => {
90                 loaded => \&Scalar::Util::weaken,
91                 not_loaded => sub { die "Scalar::Util required for weak reference support" },
92             },
93 #       ^^^^^   CODE TAKEN FROM SCALAR::UTIL   ^^^^^
94         },
95         'MRO::Compat' => {
96 #       VVVVV   CODE TAKEN FROM MRO::COMPAT   VVVVV
97             'get_linear_isa' => {
98                 loaded     => \&mro::get_linear_isa,
99                 not_loaded => do {
100                     # this recurses so it isn't pretty
101                     my $code;
102                     $code = sub {
103                         no strict 'refs';
104
105                         my $classname = shift;
106
107                         my @lin = ($classname);
108                         my %stored;
109                         foreach my $parent (@{"$classname\::ISA"}) {
110                             my $plin = $code->($parent);
111                             foreach (@$plin) {
112                                 next if exists $stored{$_};
113                                 push(@lin, $_);
114                                 $stored{$_} = 1;
115                             }
116                         }
117                         return \@lin;
118                     }
119                 },
120             },
121 #       ^^^^^   CODE TAKEN FROM MRO::COMPAT   ^^^^^
122         },
123 #       VVVVV   CODE TAKEN FROM TEST::EXCEPTION   VVVVV
124         'Test::Exception' => do {
125
126             my $Tester = Test::Builder->new;
127
128             my $is_exception = sub {
129                 my $exception = shift;
130                 return ref $exception || $exception ne '';
131             };
132
133             my $exception_as_string = sub {
134                 my ( $prefix, $exception ) = @_;
135                 return "$prefix normal exit" unless $is_exception->( $exception );
136                 my $class = ref $exception;
137                 $exception = "$class ($exception)"
138                         if $class && "$exception" !~ m/^\Q$class/;
139                 chomp $exception;
140                 return "$prefix $exception";
141             };
142             my $try_as_caller = sub {
143                 my $coderef = shift;
144                 eval { $coderef->() };
145                 $@;
146             };
147
148             {
149                 'throws_ok' => sub (&$;$) {
150                     my ( $coderef, $expecting, $description ) = @_;
151                     Carp::croak "throws_ok: must pass exception class/object or regex"
152                         unless defined $expecting;
153                     $description = $exception_as_string->( "threw", $expecting )
154                         unless defined $description;
155                     my $exception = $try_as_caller->($coderef);
156
157                     my $regex = $Tester->maybe_regex( $expecting );
158                     my $ok = $regex
159                         ? ( $exception =~ m/$regex/ )
160                         : eval {
161                             $exception->isa( ref $expecting ? ref $expecting : $expecting )
162                         };
163                     $Tester->ok( $ok, $description );
164                     unless ( $ok ) {
165                         $Tester->diag( $exception_as_string->( "expecting:", $expecting ) );
166                         $Tester->diag( $exception_as_string->( "found:", $exception ) );
167                     };
168                     $@ = $exception;
169                     return $ok;
170                 },
171                 'lives_ok' => sub (&;$) {
172                     my ( $coderef, $description ) = @_;
173                     my $exception = $try_as_caller->( $coderef );
174                     my $ok = $Tester->ok( ! $is_exception->( $exception ), $description );
175                     $Tester->diag( $exception_as_string->( "died:", $exception ) ) unless $ok;
176                     $@ = $exception;
177                     return $ok;
178                 },
179             },
180         },
181     );
182
183     our %loaded;
184
185     our @EXPORT_OK = map { keys %$_ } values %dependencies;
186     our %EXPORT_TAGS = (
187         all  => \@EXPORT_OK,
188         test => [qw/throws_ok lives_ok/],
189     );
190
191     for my $module_name (keys %dependencies) {
192         my $loaded = do {
193             local $SIG{__DIE__} = 'DEFAULT';
194             eval "require $module_name; 1";
195         };
196
197         $loaded{$module_name} = $loaded;
198
199         for my $method_name (keys %{ $dependencies{ $module_name } }) {
200             my $producer = $dependencies{$module_name}{$method_name};
201             my $implementation;
202
203             if (ref($producer) eq 'HASH') {
204                 $implementation = $loaded
205                                 ? $producer->{loaded}
206                                 : $producer->{not_loaded};
207             }
208             else {
209                 $implementation = $loaded
210                                 ? $module_name->can($method_name)
211                                 : $producer;
212             }
213
214             no strict 'refs';
215             *{ __PACKAGE__ . '::' . $method_name } = $implementation;
216         }
217     }
218 }
219
220 1;
221
222 __END__
223
224 =head1 NAME
225
226 Mouse::Util - features, with or without their dependencies
227
228 =head1 IMPLEMENTATIONS FOR
229
230 =head2 L<MRO::Compat>
231
232 =head3 get_linear_isa
233
234 =head2 L<Scalar::Util>
235
236 =head3 blessed
237
238 =head3 looks_like_number
239
240 =head3 reftype
241
242 =head3 openhandle
243
244 =head3 weaken
245
246 C<weaken> I<must> be implemented in XS. If the user tries to use C<weaken>
247 without L<Scalar::Util>, an error is thrown.
248
249 =head2 Test::Exception
250
251 =head3 throws_ok
252
253 =head3 lives_ok
254
255 =cut
256