Commit | Line | Data |
4093c859 |
1 | #!/usr/bin/env perl |
2 | package Mouse::Util; |
3 | use strict; |
4 | use warnings; |
5 | use base 'Exporter'; |
6 | |
7 | our %dependencies = ( |
8fcbe7fb |
8 | 'Scalar::Util' => { |
577be390 |
9 | |
10 | # VVVVV CODE TAKEN FROM SCALAR::UTIL VVVVV |
8fcbe7fb |
11 | 'blessed' => do { |
7fe1b68f |
12 | *UNIVERSAL::a_sub_not_likely_to_be_here = sub { |
13 | my $ref = ref($_[0]); |
8fcbe7fb |
14 | |
7fe1b68f |
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. :/ |
8fcbe7fb |
19 | |
7fe1b68f |
20 | # \*STDOUT would be allowed as an object in PP blessed |
21 | # but not XS |
22 | return $ref eq 'GLOB' ? undef : $ref; |
8fcbe7fb |
23 | }; |
24 | |
25 | sub { |
26 | local($@, $SIG{__DIE__}, $SIG{__WARN__}); |
27 | length(ref($_[0])) |
28 | ? eval { $_[0]->a_sub_not_likely_to_be_here } |
29 | : undef; |
30 | }, |
31 | }, |
58fe9fb7 |
32 | 'looks_like_number' => sub { |
33 | local $_ = shift; |
34 | |
35 | # checks from perlfaq4 |
36 | return 0 if !defined($_) or ref($_); |
37 | return 1 if (/^[+-]?\d+$/); # is a +/- integer |
38 | return 1 if (/^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/); # a C float |
39 | return 1 if ($] >= 5.008 and /^(Inf(inity)?|NaN)$/i) or ($] >= 5.006001 and /^Inf$/i); |
40 | |
41 | 0; |
42 | }, |
d8aea268 |
43 | 'reftype' => sub { |
44 | local($@, $SIG{__DIE__}, $SIG{__WARN__}); |
45 | my $r = shift; |
46 | my $t; |
47 | |
48 | length($t = ref($r)) or return undef; |
49 | |
50 | # This eval will fail if the reference is not blessed |
51 | eval { $r->a_sub_not_likely_to_be_here; 1 } |
52 | ? do { |
53 | $t = eval { |
54 | # we have a GLOB or an IO. Stringify a GLOB gives it's name |
55 | my $q = *$r; |
56 | $q =~ /^\*/ ? "GLOB" : "IO"; |
57 | } |
58 | or do { |
59 | # OK, if we don't have a GLOB what parts of |
60 | # a glob will it populate. |
61 | # NOTE: A glob always has a SCALAR |
62 | local *glob = $r; |
63 | defined *glob{ARRAY} && "ARRAY" |
64 | or defined *glob{HASH} && "HASH" |
65 | or defined *glob{CODE} && "CODE" |
66 | or length(ref(${$r})) ? "REF" : "SCALAR"; |
67 | } |
68 | } |
69 | : $t |
70 | }, |
29312fc3 |
71 | 'openhandle' => sub { |
72 | my $fh = shift; |
73 | my $rt = reftype($fh) || ''; |
74 | |
75 | return defined(fileno($fh)) ? $fh : undef |
76 | if $rt eq 'IO'; |
77 | |
78 | if (reftype(\$fh) eq 'GLOB') { # handle openhandle(*DATA) |
79 | $fh = \(my $tmp=$fh); |
80 | } |
81 | elsif ($rt ne 'GLOB') { |
82 | return undef; |
83 | } |
84 | |
85 | (tied(*$fh) or defined(fileno($fh))) |
86 | ? $fh : undef; |
87 | }, |
577be390 |
88 | weaken => { |
89 | loaded => \&Scalar::Util::weaken, |
90 | not_loaded => sub { die "Scalar::Util required for weak reference support" }, |
91 | }, |
92 | # ^^^^^ CODE TAKEN FROM SCALAR::UTIL ^^^^^ |
8fcbe7fb |
93 | }, |
4093c859 |
94 | 'MRO::Compat' => { |
577be390 |
95 | # VVVVV CODE TAKEN FROM MRO::COMPAT VVVVV |
4093c859 |
96 | 'get_linear_isa' => { |
97 | loaded => \&mro::get_linear_isa, |
98 | not_loaded => do { |
99 | # this recurses so it isn't pretty |
100 | my $code; |
101 | $code = sub { |
102 | no strict 'refs'; |
103 | |
104 | my $classname = shift; |
105 | |
106 | my @lin = ($classname); |
107 | my %stored; |
108 | foreach my $parent (@{"$classname\::ISA"}) { |
109 | my $plin = $code->($parent); |
110 | foreach (@$plin) { |
111 | next if exists $stored{$_}; |
112 | push(@lin, $_); |
113 | $stored{$_} = 1; |
114 | } |
115 | } |
116 | return \@lin; |
117 | } |
118 | }, |
119 | }, |
577be390 |
120 | # ^^^^^ CODE TAKEN FROM MRO::COMPAT ^^^^^ |
4093c859 |
121 | }, |
5816d47a |
122 | # VVVVV CODE TAKEN FROM TEST::EXCEPTION VVVVV |
0a3a19a1 |
123 | 'Test::Exception' => do { |
124 | my $is_exception = sub { |
125 | my $exception = shift; |
126 | return ref $exception || $exception ne ''; |
127 | }; |
128 | |
129 | my $exception_as_string = sub { |
130 | my ( $prefix, $exception ) = @_; |
131 | return "$prefix normal exit" unless $is_exception->( $exception ); |
132 | my $class = ref $exception; |
133 | $exception = "$class ($exception)" |
134 | if $class && "$exception" !~ m/^\Q$class/; |
135 | chomp $exception; |
136 | return "$prefix $exception"; |
137 | }; |
138 | my $try_as_caller = sub { |
139 | my $coderef = shift; |
140 | eval { $coderef->() }; |
141 | $@; |
142 | }; |
143 | |
144 | { |
145 | 'throws_ok' => sub (&$;$) { |
5816d47a |
146 | my ( $coderef, $expecting, $description ) = @_; |
147 | Carp::croak "throws_ok: must pass exception class/object or regex" |
148 | unless defined $expecting; |
149 | $description = $exception_as_string->( "threw", $expecting ) |
150 | unless defined $description; |
0a3a19a1 |
151 | my $exception = $try_as_caller->($coderef); |
5816d47a |
152 | |
153 | my $regex = $Test::Builder::Tester->maybe_regex( $expecting ); |
154 | my $ok = $regex |
155 | ? ( $exception =~ m/$regex/ ) |
156 | : eval { |
157 | $exception->isa( ref $expecting ? ref $expecting : $expecting ) |
158 | }; |
159 | $Test::Builder::Tester->ok( $ok, $description ); |
160 | unless ( $ok ) { |
161 | $Test::Builder::Tester->diag( $exception_as_string->( "expecting:", $expecting ) ); |
162 | $Test::Builder::Tester->diag( $exception_as_string->( "found:", $exception ) ); |
163 | }; |
164 | $@ = $exception; |
165 | return $ok; |
166 | }, |
0a3a19a1 |
167 | 'lives_ok' => sub (&;$) { |
168 | my ( $coderef, $description ) = @_; |
169 | my $exception = $try_as_caller->( $coderef ); |
170 | my $ok = $Test::Builder::Tester->ok( ! $is_exception->( $exception ), $description ); |
171 | $Test::Builder::Tester->diag( $exception_as_string->( "died:", $exception ) ) unless $ok; |
172 | $@ = $exception; |
173 | return $ok; |
174 | }, |
5816d47a |
175 | }, |
176 | }, |
4093c859 |
177 | ); |
178 | |
179 | our @EXPORT_OK = map { keys %$_ } values %dependencies; |
0a3a19a1 |
180 | our %EXPORT_TAGS = ( |
181 | all => \@EXPORT_OK, |
182 | test => [qw/throws_ok lives_ok/], |
183 | ); |
4093c859 |
184 | |
185 | for my $module_name (keys %dependencies) { |
4093c859 |
186 | my $loaded = do { |
187 | local $SIG{__DIE__} = 'DEFAULT'; |
4d02abfd |
188 | eval "require $module_name; 1"; |
4093c859 |
189 | }; |
190 | |
191 | for my $method_name (keys %{ $dependencies{ $module_name } }) { |
192 | my $producer = $dependencies{$module_name}{$method_name}; |
193 | my $implementation; |
194 | |
195 | if (ref($producer) eq 'HASH') { |
196 | $implementation = $loaded |
197 | ? $producer->{loaded} |
198 | : $producer->{not_loaded}; |
199 | } |
200 | else { |
201 | $implementation = $loaded |
202 | ? $module_name->can($method_name) |
203 | : $producer; |
204 | } |
205 | |
206 | no strict 'refs'; |
207 | *{ __PACKAGE__ . '::' . $method_name } = $implementation; |
208 | } |
209 | } |
210 | |
211 | 1; |
212 | |
f38ce2d0 |
213 | __END__ |
214 | |
215 | =head1 NAME |
216 | |
217 | Mouse::Util - features, with or without their dependencies |
218 | |
219 | =head1 IMPLEMENTATIONS FOR |
220 | |
221 | =head2 L<MRO::Compat> |
222 | |
223 | =head3 get_linear_isa |
224 | |
225 | =head2 L<Scalar::Util> |
226 | |
227 | =head3 blessed |
228 | |
229 | =head3 looks_like_number |
230 | |
231 | =head3 reftype |
232 | |
233 | =head3 openhandle |
234 | |
235 | =head3 weaken |
236 | |
237 | C<weaken> I<must> be implemented in XS. If the user tries to use C<weaken> |
238 | without L<Scalar::Util>, an error is thrown. |
239 | |
240 | =cut |
241 | |