28 sub drink { return "drinking " . $_[1] }
31 $Alice::VERSION = 2.718;
53 ok $a->isa("main::Alice"); # check that alternate class names work
55 ok(("main::Alice"->new)->isa("Alice"));
58 ok $a->isa("main::Bob");
62 ok ! $a->isa("Female\0NOT REALLY!"), "->isa is nul-clean.";
68 ok ! $a->isa('Programmer');
73 ok ! $a->can("eat\0Except not!"), "->can is nul-clean.";
74 ok ! $a->can("sleep");
75 ok my $ref = $a->can("drink"); # returns a coderef
76 is $a->$ref("tea"), "drinking tea"; # ... which works
77 ok $ref = $a->can("sing");
79 ok $@; # ... but not if no actual subroutine
81 ok (!Cedric->isa('Programmer'));
83 ok (Cedric->isa('Human'));
85 push(@Cedric::ISA,'Programmer');
87 ok (Cedric->isa('Programmer'));
91 base::->import('Programmer');
94 ok $a->isa('Programmer');
97 @Cedric::ISA = qw(Bob);
99 ok (!Cedric->isa('Programmer'));
102 my @refs = qw(SCALAR SCALAR LVALUE GLOB ARRAY HASH CODE);
103 my @vals = ( \$b, \3.14, \substr($b,1,1), \*b, [], {}, sub {} );
104 for ($p=0; $p < @refs; $p++) {
105 for ($q=0; $q < @vals; $q++) {
106 is UNIVERSAL::isa($vals[$p], $refs[$q]), ($p==$q or $p+$q==1);
110 ok UNIVERSAL::can(23, "can");
112 ok UNIVERSAL::can("23", "can"), '"23" can can when the pack exists';
113 ok UNIVERSAL::can(23, "can"), '23 can can when the pack exists';
115 local $::TODO = '[perl #113932]';
116 ok UNIVERSAL::can(*STDOUT, 'print'), 'globs with IOs can can';
117 ok UNIVERSAL::can(\*STDOUT, 'print'), 'globrefs with IOs can can';
118 sub IO::Handle::turn {}
119 # Should this pass? Or is the existing behaviour correct?
120 ok UNIVERSAL::can("STDOUT", 'turn'), 'IO barewords can';
123 ok $a->can("VERSION");
126 ok ! $a->can("export_tags"); # a method in Exporter
128 cmp_ok eval { $a->VERSION }, '==', 2.718;
130 ok ! (eval { $a->VERSION(2.719) });
131 like $@, qr/^Alice version 2.719 required--this is only version 2.718 at /;
133 ok (eval { $a->VERSION(2.718) });
136 ok ! (eval { $a->VERSION("version") });
137 like $@, qr/^Invalid version format/;
139 $aversion::VERSION = "version";
140 ok ! (eval { aversion->VERSION(2.719) });
141 like $@, qr/^Invalid version format/;
143 my $subs = join ' ', sort grep { defined &{"UNIVERSAL::$_"} } keys %UNIVERSAL::;
144 ## The test for import here is *not* because we want to ensure that UNIVERSAL
145 ## can always import; it is an historical accident that UNIVERSAL can import.
147 is $subs, "can import isa DOES VERSION";
149 is $subs, "DOES VERSION can import isa";
152 ok $a->isa("UNIVERSAL");
154 ok ! UNIVERSAL::isa([], "UNIVERSAL");
156 ok ! UNIVERSAL::can({}, "can");
158 ok UNIVERSAL::isa(Alice => "UNIVERSAL");
160 cmp_ok UNIVERSAL::can(Alice => "can"), '==', \&UNIVERSAL::can;
162 # now use UNIVERSAL.pm and see what changes
163 eval "use UNIVERSAL";
165 ok $a->isa("UNIVERSAL");
167 my $sub2 = join ' ', sort grep { defined &{"UNIVERSAL::$_"} } keys %UNIVERSAL::;
168 # XXX import being here is really a bug
170 is $sub2, "can import isa DOES VERSION";
172 is $sub2, "DOES VERSION can import isa";
175 eval 'sub UNIVERSAL::sleep {}';
178 ok UNIVERSAL::can($b, "can");
180 ok ! $a->can("export_tags"); # a method in Exporter
182 ok ! UNIVERSAL::isa("\xff\xff\xff\0", 'HASH');
186 no warnings "deprecated";
187 use UNIVERSAL qw( isa can VERSION );
189 ::ok isa "Pickup", UNIVERSAL;
190 ::cmp_ok can( "Pickup", "can" ), '==', \&UNIVERSAL::can;
191 ::ok VERSION "UNIVERSAL" ;
195 # test isa() and can() on magic variables
200 sub TIESCALAR { bless {} }
201 sub FETCH { "Human" }
202 tie my($x), "HumanTie";
203 ::ok $x->isa("Human");
208 # a second call to isa('UNIVERSAL') when @ISA is null failed due to caching
211 my $x = {}; bless $x, 'X';
212 ok $x->isa('UNIVERSAL');
213 ok $x->isa('UNIVERSAL');
216 # Check that the "historical accident" of UNIVERSAL having an import()
217 # method doesn't effect anyone else.
218 eval { Some::Package->import("bar") };
222 # This segfaulted in a blead.
223 fresh_perl_is('package Foo; Foo->VERSION; print "ok"', 'ok');
226 fresh_perl_is('$:; UNIVERSAL::isa(":","Unicode::String");print "ok"','ok');
239 ok( Foo->DOES( 'bar' ), 'DOES() should call DOES() on class' );
240 ok( Bar->DOES( 'Bar' ), '... and should fall back to isa()' );
241 ok( Bar->DOES( 'Foo' ), '... even when inherited' );
242 ok( Baz->DOES( 'Baz' ), '... even without inheriting any other DOES()' );
243 ok( ! Baz->DOES( 'Foo' ), '... returning true or false appropriately' );
245 ok( ! "T"->DOES( "T\0" ), 'DOES() is nul-clean' );
246 ok( ! Baz->DOES( "Baz\0Boy howdy" ), 'DOES() is nul-clean' );
251 *isa = \&UNIVERSAL::isa;
252 eval { isa({}, 'HASH') };
253 ::is($@, '', "*isa correctly found");
256 eval { UNIVERSAL::DOES([], "foo") };
257 like( $@, qr/Can't call method "DOES" on unblessed reference/,
258 'DOES call error message says DOES, not isa' );
260 # Tests for can seem to be split between here and method.t
261 # Add the verbatim perl code mentioned in the comments of
262 # http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/2001-05/msg01710.html
263 # but never actually tested.
264 is(UNIVERSAL->can("NoSuchPackage::foo"), undef);
266 @splatt::ISA = 'zlopp';
267 ok (splatt->isa('zlopp'));
268 ok (!splatt->isa('plop'));
270 # This should reset the ->isa lookup cache
271 @splatt::ISA = 'plop';
272 # And here is the new truth.
273 ok (!splatt->isa('zlopp'));
274 ok (splatt->isa('plop'));
276 use warnings "deprecated";
279 local $SIG{__WARN__} = sub { $m = $_[0] };
280 eval "use UNIVERSAL 'can'";
281 like($m, qr/^UNIVERSAL->import is deprecated/,
282 "deprecation warning for UNIVERSAL->import('can')");
285 eval "use UNIVERSAL";
287 "no deprecation warning for UNIVERSAL->import");
290 # Test: [perl #66112]: change @ISA inside sub isa
298 @ISA = qw/RT66112::A/;
299 return $self->SUPER::isa(@_);
308 @RT66112::E::ISA = qw/RT66112::A/;
309 return $self->SUPER::isa(@_);
316 @RT66112::B::ISA = qw//;
317 @RT66112::C::ISA = qw/RT66112::B/;
318 @RT66112::T1::ISA = qw/RT66112::C/;
319 ok(RT66112::T1->isa('RT66112::C'), "modify \@ISA in isa (RT66112::T1 isa RT66112::C)");
321 @RT66112::B::ISA = qw//;
322 @RT66112::C::ISA = qw/RT66112::B/;
323 @RT66112::T2::ISA = qw/RT66112::C/;
324 ok(RT66112::T2->isa('RT66112::B'), "modify \@ISA in isa (RT66112::T2 isa RT66112::B)");
326 @RT66112::B::ISA = qw//;
327 @RT66112::C::ISA = qw/RT66112::B/;
328 @RT66112::T3::ISA = qw/RT66112::C/;
329 ok(RT66112::T3->isa('RT66112::A'), "modify \@ISA in isa (RT66112::T3 isa RT66112::A)") or require mro, diag "@{mro::get_linear_isa('RT66112::T3')}";
331 @RT66112::E::ISA = qw/RT66112::D/;
332 @RT66112::T4::ISA = qw/RT66112::E/;
333 ok(RT66112::T4->isa('RT66112::E'), "modify \@ISA in isa (RT66112::T4 isa RT66112::E)");
335 @RT66112::E::ISA = qw/RT66112::D/;
336 @RT66112::T5::ISA = qw/RT66112::E/;
337 ok(! RT66112::T5->isa('RT66112::D'), "modify \@ISA in isa (RT66112::T5 not isa RT66112::D)");
339 @RT66112::E::ISA = qw/RT66112::D/;
340 @RT66112::T6::ISA = qw/RT66112::E/;
341 ok(RT66112::T6->isa('RT66112::A'), "modify \@ISA in isa (RT66112::T6 isa RT66112::A)");
344 ok(Undeclared->can("can"));
345 sub Undeclared::foo { }
346 ok(Undeclared->can("foo"));
347 ok(!Undeclared->can("something_else"));
349 ok(Undeclared->isa("UNIVERSAL"));