10 # VVVVV CODE TAKEN FROM SCALAR::UTIL VVVVV
12 *UNIVERSAL::a_sub_not_likely_to_be_here = sub {
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. :/
20 # \*STDOUT would be allowed as an object in PP blessed
22 return $ref eq 'GLOB' ? undef : $ref;
26 local($@, $SIG{__DIE__}, $SIG{__WARN__});
28 ? eval { $_[0]->a_sub_not_likely_to_be_here }
32 'looks_like_number' => sub {
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);
44 local($@, $SIG{__DIE__}, $SIG{__WARN__});
48 length($t = ref($r)) or return undef;
50 # This eval will fail if the reference is not blessed
51 eval { $r->a_sub_not_likely_to_be_here; 1 }
54 # we have a GLOB or an IO. Stringify a GLOB gives it's name
56 $q =~ /^\*/ ? "GLOB" : "IO";
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
63 defined *glob{ARRAY} && "ARRAY"
64 or defined *glob{HASH} && "HASH"
65 or defined *glob{CODE} && "CODE"
66 or length(ref(${$r})) ? "REF" : "SCALAR";
73 my $rt = reftype($fh) || '';
75 return defined(fileno($fh)) ? $fh : undef
78 if (reftype(\$fh) eq 'GLOB') { # handle openhandle(*DATA)
81 elsif ($rt ne 'GLOB') {
85 (tied(*$fh) or defined(fileno($fh)))
89 loaded => \&Scalar::Util::weaken,
90 not_loaded => sub { die "Scalar::Util required for weak reference support" },
92 # ^^^^^ CODE TAKEN FROM SCALAR::UTIL ^^^^^
95 # VVVVV CODE TAKEN FROM MRO::COMPAT VVVVV
97 loaded => \&mro::get_linear_isa,
99 # this recurses so it isn't pretty
104 my $classname = shift;
106 my @lin = ($classname);
108 foreach my $parent (@{"$classname\::ISA"}) {
109 my $plin = $code->($parent);
111 next if exists $stored{$_};
120 # ^^^^^ CODE TAKEN FROM MRO::COMPAT ^^^^^
122 # VVVVV CODE TAKEN FROM TEST::EXCEPTION VVVVV
123 'Test::Exception' => do {
125 my $Tester = Test::Builder->new;
127 my $is_exception = sub {
128 my $exception = shift;
129 return ref $exception || $exception ne '';
132 my $exception_as_string = sub {
133 my ( $prefix, $exception ) = @_;
134 return "$prefix normal exit" unless $is_exception->( $exception );
135 my $class = ref $exception;
136 $exception = "$class ($exception)"
137 if $class && "$exception" !~ m/^\Q$class/;
139 return "$prefix $exception";
141 my $try_as_caller = sub {
143 eval { $coderef->() };
148 'throws_ok' => sub (&$;$) {
149 my ( $coderef, $expecting, $description ) = @_;
150 Carp::croak "throws_ok: must pass exception class/object or regex"
151 unless defined $expecting;
152 $description = $exception_as_string->( "threw", $expecting )
153 unless defined $description;
154 my $exception = $try_as_caller->($coderef);
156 my $regex = $Tester->maybe_regex( $expecting );
158 ? ( $exception =~ m/$regex/ )
160 $exception->isa( ref $expecting ? ref $expecting : $expecting )
162 $Tester->ok( $ok, $description );
164 $Tester->diag( $exception_as_string->( "expecting:", $expecting ) );
165 $Tester->diag( $exception_as_string->( "found:", $exception ) );
170 'lives_ok' => sub (&;$) {
171 my ( $coderef, $description ) = @_;
172 my $exception = $try_as_caller->( $coderef );
173 my $ok = $Tester->ok( ! $is_exception->( $exception ), $description );
174 $Tester->diag( $exception_as_string->( "died:", $exception ) ) unless $ok;
182 our @EXPORT_OK = map { keys %$_ } values %dependencies;
185 test => [qw/throws_ok lives_ok/],
188 for my $module_name (keys %dependencies) {
190 local $SIG{__DIE__} = 'DEFAULT';
191 eval "require $module_name; 1";
194 for my $method_name (keys %{ $dependencies{ $module_name } }) {
195 my $producer = $dependencies{$module_name}{$method_name};
198 if (ref($producer) eq 'HASH') {
199 $implementation = $loaded
200 ? $producer->{loaded}
201 : $producer->{not_loaded};
204 $implementation = $loaded
205 ? $module_name->can($method_name)
210 *{ __PACKAGE__ . '::' . $method_name } = $implementation;
220 Mouse::Util - features, with or without their dependencies
222 =head1 IMPLEMENTATIONS FOR
224 =head2 L<MRO::Compat>
226 =head3 get_linear_isa
228 =head2 L<Scalar::Util>
232 =head3 looks_like_number
240 C<weaken> I<must> be implemented in XS. If the user tries to use C<weaken>
241 without L<Scalar::Util>, an error is thrown.