Run all of Mouse::Util at BEGIN time
[gitmo/Mouse.git] / lib / Mouse / Util.pm
CommitLineData
4093c859 1#!/usr/bin/env perl
2package Mouse::Util;
3use strict;
4use warnings;
5use base 'Exporter';
6
42d7df00 7BEGIN {
8 our %dependencies = (
9 'Scalar::Util' => {
577be390 10
11# VVVVV CODE TAKEN FROM SCALAR::UTIL VVVVV
42d7df00 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 };
8fcbe7fb 25
42d7df00 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 },
8fcbe7fb 32 },
42d7df00 33 'looks_like_number' => sub {
34 local $_ = shift;
58fe9fb7 35
42d7df00 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);
58fe9fb7 41
42d7df00 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 }
d8aea268 69 }
42d7df00 70 : $t
71 },
72 'openhandle' => sub {
73 my $fh = shift;
74 my $rt = reftype($fh) || '';
29312fc3 75
42d7df00 76 return defined(fileno($fh)) ? $fh : undef
77 if $rt eq 'IO';
29312fc3 78
42d7df00 79 if (reftype(\$fh) eq 'GLOB') { # handle openhandle(*DATA)
80 $fh = \(my $tmp=$fh);
81 }
82 elsif ($rt ne 'GLOB') {
83 return undef;
84 }
29312fc3 85
42d7df00 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 },
577be390 93# ^^^^^ CODE TAKEN FROM SCALAR::UTIL ^^^^^
42d7df00 94 },
95 'MRO::Compat' => {
577be390 96# VVVVV CODE TAKEN FROM MRO::COMPAT VVVVV
42d7df00 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 }
4093c859 116 }
42d7df00 117 return \@lin;
4093c859 118 }
42d7df00 119 },
4093c859 120 },
577be390 121# ^^^^^ CODE TAKEN FROM MRO::COMPAT ^^^^^
42d7df00 122 },
5816d47a 123# VVVVV CODE TAKEN FROM TEST::EXCEPTION VVVVV
42d7df00 124 'Test::Exception' => do {
9458ce95 125
42d7df00 126 my $Tester = Test::Builder->new;
9458ce95 127
42d7df00 128 my $is_exception = sub {
129 my $exception = shift;
130 return ref $exception || $exception ne '';
131 };
0a3a19a1 132
42d7df00 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 };
0a3a19a1 147
42d7df00 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 ) );
5816d47a 167 };
42d7df00 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 },
0a3a19a1 179 },
5816d47a 180 },
42d7df00 181 );
4093c859 182
42d7df00 183 our %loaded;
00136d64 184
42d7df00 185 our @EXPORT_OK = map { keys %$_ } values %dependencies;
186 our %EXPORT_TAGS = (
187 all => \@EXPORT_OK,
188 test => [qw/throws_ok lives_ok/],
189 );
4093c859 190
42d7df00 191 for my $module_name (keys %dependencies) {
192 my $loaded = do {
193 local $SIG{__DIE__} = 'DEFAULT';
194 eval "require $module_name; 1";
195 };
4093c859 196
42d7df00 197 $loaded{$module_name} = $loaded;
00136d64 198
42d7df00 199 for my $method_name (keys %{ $dependencies{ $module_name } }) {
200 my $producer = $dependencies{$module_name}{$method_name};
201 my $implementation;
4093c859 202
42d7df00 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 }
4093c859 213
42d7df00 214 no strict 'refs';
215 *{ __PACKAGE__ . '::' . $method_name } = $implementation;
216 }
4093c859 217 }
218}
219
2201;
221
f38ce2d0 222__END__
223
224=head1 NAME
225
226Mouse::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
246C<weaken> I<must> be implemented in XS. If the user tries to use C<weaken>
247without L<Scalar::Util>, an error is thrown.
248
ea0b9e39 249=head2 Test::Exception
250
251=head3 throws_ok
252
253=head3 lives_ok
254
f38ce2d0 255=cut
256