20 # We only have to do this nastiness if we haven't loaded XS version of
21 # Mouse.pm, so check if we're running under PurePerl or not
23 if ($Mouse::PurePerl) {
26 # VVVVV CODE TAKEN FROM SCALAR::UTIL VVVVV
28 *UNIVERSAL::a_sub_not_likely_to_be_here = sub {
31 # deviation from Scalar::Util
32 # XS returns undef, PP returns GLOB.
33 # let's make that more consistent by having PP return
34 # undef if it's a GLOB. :/
36 # \*STDOUT would be allowed as an object in PP blessed
38 return $ref eq 'GLOB' ? undef : $ref;
42 local($@, $SIG{__DIE__}, $SIG{__WARN__});
44 ? eval { $_[0]->a_sub_not_likely_to_be_here }
48 'looks_like_number' => sub {
51 # checks from perlfaq4
52 return 0 if !defined($_) or ref($_);
53 return 1 if (/^[+-]?\d+$/); # is a +/- integer
54 return 1 if (/^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/); # a C float
55 return 1 if ($] >= 5.008 and /^(Inf(inity)?|NaN)$/i) or ($] >= 5.006001 and /^Inf$/i);
60 local($@, $SIG{__DIE__}, $SIG{__WARN__});
64 length($t = ref($r)) or return undef;
66 # This eval will fail if the reference is not blessed
67 eval { $r->a_sub_not_likely_to_be_here; 1 }
70 # we have a GLOB or an IO. Stringify a GLOB gives it's name
72 $q =~ /^\*/ ? "GLOB" : "IO";
75 # OK, if we don't have a GLOB what parts of
76 # a glob will it populate.
77 # NOTE: A glob always has a SCALAR
79 defined *glob{ARRAY} && "ARRAY"
80 or defined *glob{HASH} && "HASH"
81 or defined *glob{CODE} && "CODE"
82 or length(ref(${$r})) ? "REF" : "SCALAR";
89 my $rt = reftype($fh) || '';
91 return defined(fileno($fh)) ? $fh : undef
94 if (reftype(\$fh) eq 'GLOB') { # handle openhandle(*DATA)
97 elsif ($rt ne 'GLOB') {
101 (tied(*$fh) or defined(fileno($fh)))
105 loaded => \&Scalar::Util::weaken,
106 not_loaded => sub { die "Scalar::Util required for weak reference support" },
108 # ^^^^^ CODE TAKEN FROM SCALAR::UTIL ^^^^^
111 # VVVVV CODE TAKEN FROM MRO::COMPAT VVVVV
112 'get_linear_isa' => {
113 loaded => \&mro::get_linear_isa,
115 # this recurses so it isn't pretty
120 my $classname = shift;
122 my @lin = ($classname);
124 foreach my $parent (@{"$classname\::ISA"}) {
125 my $plin = $code->($parent);
127 next if exists $stored{$_};
136 # ^^^^^ CODE TAKEN FROM MRO::COMPAT ^^^^^
141 for my $module_name (keys %dependencies) {
143 local $SIG{__DIE__} = 'DEFAULT';
144 eval "require $module_name; 1";
147 $loaded{$module_name} = $loaded;
149 for my $method_name (keys %{ $dependencies{ $module_name } }) {
150 my $producer = $dependencies{$module_name}{$method_name};
153 if (ref($producer) eq 'HASH') {
154 $implementation = $loaded
155 ? $producer->{loaded}
156 : $producer->{not_loaded};
159 $implementation = $loaded
160 ? $module_name->can($method_name)
165 *{ __PACKAGE__ . '::' . $method_name } = $implementation;
169 # If we're running under XS, we can provide
174 # other functions need to be loaded from our respective sources
176 if (defined &Scalar::Util::openhandle || eval { require Scalar::Util; 1 }) {
177 *openhandle = \&Scalar::Util::openhandle;
179 # XXX - room for improvement
182 my $rt = reftype($fh) || '';
184 return defined(fileno($fh)) ? $fh : undef
187 if (reftype(\$fh) eq 'GLOB') { # handle openhandle(*DATA)
188 $fh = \(my $tmp=$fh);
190 elsif ($rt ne 'GLOB') {
194 (tied(*$fh) or defined(fileno($fh)))
199 if (defined &mro::get_linear_isa || eval { require MRO::Compat; 1; }) {
200 *get_linear_isa = \&mro::get_linear_isa;
202 # this recurses so it isn't pretty
204 *get_linear_isa = $code = sub {
207 my $classname = shift;
209 my @lin = ($classname);
211 foreach my $parent (@{"$classname\::ISA"}) {
212 my $plin = $code->($parent);
214 next if exists $stored{$_};
225 sub apply_all_roles {
226 my $meta = Mouse::Meta::Class->initialize(shift);
228 confess "Mouse::Util only supports 'apply_all_roles' on individual roles at a time" if @_;
230 Mouse::load_class($role);
231 $role->meta->apply($meta);
240 Mouse::Util - features, with or without their dependencies
242 =head1 IMPLEMENTATIONS FOR
244 =head2 L<MRO::Compat>
246 =head3 get_linear_isa
248 =head2 L<Scalar::Util>
252 =head3 looks_like_number
260 C<weaken> I<must> be implemented in XS. If the user tries to use C<weaken>
261 without L<Scalar::Util>, an error is thrown.
263 =head2 Test::Exception