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';
114 sub IO::Handle::turn {}
115 ok UNIVERSAL::can(*STDOUT, 'turn'), 'globs with IOs can';
116 ok UNIVERSAL::can(\*STDOUT, 'turn'), 'globrefs with IOs can';
117 ok UNIVERSAL::can("STDOUT", 'turn'), 'IO barewords can';
119 ok $a->can("VERSION");
122 ok ! $a->can("export_tags"); # a method in Exporter
124 cmp_ok eval { $a->VERSION }, '==', 2.718;
126 ok ! (eval { $a->VERSION(2.719) });
127 like $@, qr/^Alice version 2.719 required--this is only version 2.718 at /;
129 ok (eval { $a->VERSION(2.718) });
132 ok ! (eval { $a->VERSION("version") });
133 like $@, qr/^Invalid version format/;
135 $aversion::VERSION = "version";
136 ok ! (eval { aversion->VERSION(2.719) });
137 like $@, qr/^Invalid version format/;
139 my $subs = join ' ', sort grep { defined &{"UNIVERSAL::$_"} } keys %UNIVERSAL::;
141 is $subs, "can isa DOES VERSION";
143 is $subs, "DOES VERSION can isa";
146 ok $a->isa("UNIVERSAL");
148 ok ! UNIVERSAL::isa([], "UNIVERSAL");
150 ok ! UNIVERSAL::can({}, "can");
152 ok UNIVERSAL::isa(Alice => "UNIVERSAL");
154 cmp_ok UNIVERSAL::can(Alice => "can"), '==', \&UNIVERSAL::can;
156 # now use UNIVERSAL.pm and see what changes
157 eval "use UNIVERSAL";
159 ok $a->isa("UNIVERSAL");
161 my $sub2 = join ' ', sort grep { defined &{"UNIVERSAL::$_"} } keys %UNIVERSAL::;
162 # XXX import being here is really a bug
164 is $sub2, "can import isa DOES VERSION";
166 is $sub2, "DOES VERSION can import isa";
169 eval 'sub UNIVERSAL::sleep {}';
172 ok UNIVERSAL::can($b, "can");
174 ok ! $a->can("export_tags"); # a method in Exporter
176 ok ! UNIVERSAL::isa("\xff\xff\xff\0", 'HASH');
179 # test isa() and can() on magic variables
184 sub TIESCALAR { bless {} }
185 sub FETCH { "Human" }
186 tie my($x), "HumanTie";
187 ::ok $x->isa("Human");
192 # a second call to isa('UNIVERSAL') when @ISA is null failed due to caching
195 my $x = {}; bless $x, 'X';
196 ok $x->isa('UNIVERSAL');
197 ok $x->isa('UNIVERSAL');
200 # Check that the "historical accident" of UNIVERSAL having an import()
201 # method doesn't effect anyone else.
202 eval { Some::Package->import("bar") };
206 # This segfaulted in a blead.
207 fresh_perl_is('package Foo; Foo->VERSION; print "ok"', 'ok');
210 fresh_perl_is('$:; UNIVERSAL::isa(":","Unicode::String");print "ok"','ok');
223 ok( Foo->DOES( 'bar' ), 'DOES() should call DOES() on class' );
224 ok( Bar->DOES( 'Bar' ), '... and should fall back to isa()' );
225 ok( Bar->DOES( 'Foo' ), '... even when inherited' );
226 ok( Baz->DOES( 'Baz' ), '... even without inheriting any other DOES()' );
227 ok( ! Baz->DOES( 'Foo' ), '... returning true or false appropriately' );
229 ok( ! "T"->DOES( "T\0" ), 'DOES() is nul-clean' );
230 ok( ! Baz->DOES( "Baz\0Boy howdy" ), 'DOES() is nul-clean' );
235 *isa = \&UNIVERSAL::isa;
236 eval { isa({}, 'HASH') };
237 ::is($@, '', "*isa correctly found");
240 eval { UNIVERSAL::DOES([], "foo") };
241 like( $@, qr/Can't call method "DOES" on unblessed reference/,
242 'DOES call error message says DOES, not isa' );
244 # Tests for can seem to be split between here and method.t
245 # Add the verbatim perl code mentioned in the comments of
246 # http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/2001-05/msg01710.html
247 # but never actually tested.
248 is(UNIVERSAL->can("NoSuchPackage::foo"), undef);
250 @splatt::ISA = 'zlopp';
251 ok (splatt->isa('zlopp'));
252 ok (!splatt->isa('plop'));
254 # This should reset the ->isa lookup cache
255 @splatt::ISA = 'plop';
256 # And here is the new truth.
257 ok (!splatt->isa('zlopp'));
258 ok (splatt->isa('plop'));
260 use warnings "deprecated";
263 local $SIG{__WARN__} = sub { $m = $_[0] };
264 eval "use UNIVERSAL 'can'";
265 like($@, qr/^UNIVERSAL does not export anything\b/,
266 "error for UNIVERSAL->import('can')");
268 "no deprecation warning for UNIVERSAL->import('can')");
271 eval "use UNIVERSAL";
273 "no error for UNIVERSAL->import");
275 "no deprecation warning for UNIVERSAL->import");
278 # Test: [perl #66112]: change @ISA inside sub isa
286 @ISA = qw/RT66112::A/;
287 return $self->SUPER::isa(@_);
296 @RT66112::E::ISA = qw/RT66112::A/;
297 return $self->SUPER::isa(@_);
304 @RT66112::B::ISA = qw//;
305 @RT66112::C::ISA = qw/RT66112::B/;
306 @RT66112::T1::ISA = qw/RT66112::C/;
307 ok(RT66112::T1->isa('RT66112::C'), "modify \@ISA in isa (RT66112::T1 isa RT66112::C)");
309 @RT66112::B::ISA = qw//;
310 @RT66112::C::ISA = qw/RT66112::B/;
311 @RT66112::T2::ISA = qw/RT66112::C/;
312 ok(RT66112::T2->isa('RT66112::B'), "modify \@ISA in isa (RT66112::T2 isa RT66112::B)");
314 @RT66112::B::ISA = qw//;
315 @RT66112::C::ISA = qw/RT66112::B/;
316 @RT66112::T3::ISA = qw/RT66112::C/;
317 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')}";
319 @RT66112::E::ISA = qw/RT66112::D/;
320 @RT66112::T4::ISA = qw/RT66112::E/;
321 ok(RT66112::T4->isa('RT66112::E'), "modify \@ISA in isa (RT66112::T4 isa RT66112::E)");
323 @RT66112::E::ISA = qw/RT66112::D/;
324 @RT66112::T5::ISA = qw/RT66112::E/;
325 ok(! RT66112::T5->isa('RT66112::D'), "modify \@ISA in isa (RT66112::T5 not isa RT66112::D)");
327 @RT66112::E::ISA = qw/RT66112::D/;
328 @RT66112::T6::ISA = qw/RT66112::E/;
329 ok(RT66112::T6->isa('RT66112::A'), "modify \@ISA in isa (RT66112::T6 isa RT66112::A)");
332 ok(Undeclared->can("can"));
333 sub Undeclared::foo { }
334 ok(Undeclared->can("foo"));
335 ok(!Undeclared->can("something_else"));
337 ok(Undeclared->isa("UNIVERSAL"));
339 # keep this at the end to avoid messing up earlier tests, since it modifies
341 @UNIVERSAL::ISA = ('UniversalParent');
342 { package UniversalIsaTest1; }
343 ok(UniversalIsaTest1->isa('UniversalParent'));
344 ok(UniversalIsaTest2->isa('UniversalParent'));