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 $a->can("VERSION");
115 ok ! $a->can("export_tags"); # a method in Exporter
117 cmp_ok eval { $a->VERSION }, '==', 2.718;
119 ok ! (eval { $a->VERSION(2.719) });
120 like $@, qr/^Alice version 2.719 required--this is only version 2.718 at /;
122 ok (eval { $a->VERSION(2.718) });
125 my $subs = join ' ', sort grep { defined &{"UNIVERSAL::$_"} } keys %UNIVERSAL::;
126 ## The test for import here is *not* because we want to ensure that UNIVERSAL
127 ## can always import; it is an historical accident that UNIVERSAL can import.
129 is $subs, "can import isa DOES VERSION";
131 is $subs, "DOES VERSION can import isa";
134 ok $a->isa("UNIVERSAL");
136 ok ! UNIVERSAL::isa([], "UNIVERSAL");
138 ok ! UNIVERSAL::can({}, "can");
140 ok UNIVERSAL::isa(Alice => "UNIVERSAL");
142 cmp_ok UNIVERSAL::can(Alice => "can"), '==', \&UNIVERSAL::can;
144 # now use UNIVERSAL.pm and see what changes
145 eval "use UNIVERSAL";
147 ok $a->isa("UNIVERSAL");
149 my $sub2 = join ' ', sort grep { defined &{"UNIVERSAL::$_"} } keys %UNIVERSAL::;
150 # XXX import being here is really a bug
152 is $sub2, "can import isa DOES VERSION";
154 is $sub2, "DOES VERSION can import isa";
157 eval 'sub UNIVERSAL::sleep {}';
160 ok ! UNIVERSAL::can($b, "can");
162 ok ! $a->can("export_tags"); # a method in Exporter
164 ok ! UNIVERSAL::isa("\xff\xff\xff\0", 'HASH');
168 use UNIVERSAL qw( isa can VERSION );
170 ::ok isa "Pickup", UNIVERSAL;
171 ::cmp_ok can( "Pickup", "can" ), '==', \&UNIVERSAL::can;
172 ::ok VERSION "UNIVERSAL" ;
176 # test isa() and can() on magic variables
181 sub TIESCALAR { bless {} }
182 sub FETCH { "Human" }
183 tie my($x), "HumanTie";
184 ::ok $x->isa("Human");
189 # a second call to isa('UNIVERSAL') when @ISA is null failed due to caching
192 my $x = {}; bless $x, 'X';
193 ok $x->isa('UNIVERSAL');
194 ok $x->isa('UNIVERSAL');
197 # Check that the "historical accident" of UNIVERSAL having an import()
198 # method doesn't effect anyone else.
199 eval { Some::Package->import("bar") };
203 # This segfaulted in a blead.
204 fresh_perl_is('package Foo; Foo->VERSION; print "ok"', 'ok');
207 fresh_perl_is('$:; UNIVERSAL::isa(":","Unicode::String");print "ok"','ok');
220 ok( Foo->DOES( 'bar' ), 'DOES() should call DOES() on class' );
221 ok( Bar->DOES( 'Bar' ), '... and should fall back to isa()' );
222 ok( Bar->DOES( 'Foo' ), '... even when inherited' );
223 ok( Baz->DOES( 'Baz' ), '... even without inheriting any other DOES()' );
224 ok( ! Baz->DOES( 'Foo' ), '... returning true or false appropriately' );
226 ok( ! "T"->DOES( "T\0" ), 'DOES() is nul-clean' );
227 ok( ! Baz->DOES( "Baz\0Boy howdy" ), 'DOES() is nul-clean' );
232 *isa = \&UNIVERSAL::isa;
233 eval { isa({}, 'HASH') };
234 ::is($@, '', "*isa correctly found");
237 eval { UNIVERSAL::DOES([], "foo") };
238 like( $@, qr/Can't call method "DOES" on unblessed reference/,
239 'DOES call error message says DOES, not isa' );
241 # Tests for can seem to be split between here and method.t
242 # Add the verbatim perl code mentioned in the comments of
243 # http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/2001-05/msg01710.html
244 # but never actually tested.
245 is(UNIVERSAL->can("NoSuchPackage::foo"), undef);
247 @splatt::ISA = 'zlopp';
248 ok (splatt->isa('zlopp'));
249 ok (!splatt->isa('plop'));
251 # This should reset the ->isa lookup cache
252 @splatt::ISA = 'plop';
253 # And here is the new truth.
254 ok (!splatt->isa('zlopp'));
255 ok (splatt->isa('plop'));
257 use warnings "deprecated";
260 local $SIG{__WARN__} = sub { $m = $_[0] };
261 eval "use UNIVERSAL 'can'";
262 like($m, qr/^UNIVERSAL->import is deprecated/,
263 "deprecation warning for UNIVERSAL->import('can')");
266 eval "use UNIVERSAL";
268 "no deprecation warning for UNIVERSAL->import");
271 # Test: [perl #66112]: change @ISA inside sub isa
279 @ISA = qw/RT66112::A/;
280 return $self->SUPER::isa(@_);
289 @RT66112::E::ISA = qw/RT66112::A/;
290 return $self->SUPER::isa(@_);
297 @RT66112::B::ISA = qw//;
298 @RT66112::C::ISA = qw/RT66112::B/;
299 @RT66112::T1::ISA = qw/RT66112::C/;
300 ok(RT66112::T1->isa('RT66112::C'), "modify \@ISA in isa (RT66112::T1 isa RT66112::C)");
302 @RT66112::B::ISA = qw//;
303 @RT66112::C::ISA = qw/RT66112::B/;
304 @RT66112::T2::ISA = qw/RT66112::C/;
305 ok(RT66112::T2->isa('RT66112::B'), "modify \@ISA in isa (RT66112::T2 isa RT66112::B)");
307 @RT66112::B::ISA = qw//;
308 @RT66112::C::ISA = qw/RT66112::B/;
309 @RT66112::T3::ISA = qw/RT66112::C/;
310 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')}";
312 @RT66112::E::ISA = qw/RT66112::D/;
313 @RT66112::T4::ISA = qw/RT66112::E/;
314 ok(RT66112::T4->isa('RT66112::E'), "modify \@ISA in isa (RT66112::T4 isa RT66112::E)");
316 @RT66112::E::ISA = qw/RT66112::D/;
317 @RT66112::T5::ISA = qw/RT66112::E/;
318 ok(! RT66112::T5->isa('RT66112::D'), "modify \@ISA in isa (RT66112::T5 not isa RT66112::D)");
320 @RT66112::E::ISA = qw/RT66112::D/;
321 @RT66112::T6::ISA = qw/RT66112::E/;
322 ok(RT66112::T6->isa('RT66112::A'), "modify \@ISA in isa (RT66112::T6 isa RT66112::A)");