9 set_up_inc(qw '../lib ../dist/base/lib');
29 sub drink { return "drinking " . $_[1] }
32 $Alice::VERSION = 2.718;
54 ok $a->isa("main::Alice"); # check that alternate class names work
56 ok(("main::Alice"->new)->isa("Alice"));
59 ok $a->isa("main::Bob");
63 ok ! $a->isa("Female\0NOT REALLY!"), "->isa is nul-clean.";
69 ok ! $a->isa('Programmer');
74 ok ! $a->can("eat\0Except not!"), "->can is nul-clean.";
75 ok ! $a->can("sleep");
76 ok my $ref = $a->can("drink"); # returns a coderef
77 is $a->$ref("tea"), "drinking tea"; # ... which works
78 ok $ref = $a->can("sing");
80 ok $@; # ... but not if no actual subroutine
82 ok (!Cedric->isa('Programmer'));
84 ok (Cedric->isa('Human'));
86 push(@Cedric::ISA,'Programmer');
88 ok (Cedric->isa('Programmer'));
92 base::->import('Programmer');
95 ok $a->isa('Programmer');
98 @Cedric::ISA = qw(Bob);
100 ok (!Cedric->isa('Programmer'));
103 my @refs = qw(SCALAR SCALAR LVALUE GLOB ARRAY HASH CODE);
104 my @vals = ( \$b, \3.14, \substr($b,1,1), \*b, [], {}, sub {} );
105 for ($p=0; $p < @refs; $p++) {
106 for ($q=0; $q < @vals; $q++) {
107 is UNIVERSAL::isa($vals[$p], $refs[$q]), ($p==$q or $p+$q==1);
111 ok UNIVERSAL::can(23, "can");
113 ok UNIVERSAL::can("23", "can"), '"23" can can when the pack exists';
114 ok UNIVERSAL::can(23, "can"), '23 can can when the pack exists';
115 sub IO::Handle::turn {}
116 ok UNIVERSAL::can(*STDOUT, 'turn'), 'globs with IOs can';
117 ok UNIVERSAL::can(\*STDOUT, 'turn'), 'globrefs with IOs can';
118 ok UNIVERSAL::can("STDOUT", 'turn'), 'IO barewords can';
120 ok $a->can("VERSION");
123 ok ! $a->can("export_tags"); # a method in Exporter
125 cmp_ok eval { $a->VERSION }, '==', 2.718;
127 ok ! (eval { $a->VERSION(2.719) });
128 like $@, qr/^Alice version 2.719 required--this is only version 2.718 at /;
130 ok (eval { $a->VERSION(2.718) });
133 ok ! (eval { $a->VERSION("version") });
134 like $@, qr/^Invalid version format/;
136 $aversion::VERSION = "version";
137 ok ! (eval { aversion->VERSION(2.719) });
138 like $@, qr/^Invalid version format/;
140 my $subs = join ' ', sort grep { defined &{"UNIVERSAL::$_"} } keys %UNIVERSAL::;
142 is $subs, "can isa DOES VERSION";
144 is $subs, "DOES VERSION can isa";
147 ok $a->isa("UNIVERSAL");
149 ok ! UNIVERSAL::isa([], "UNIVERSAL");
151 ok ! UNIVERSAL::can({}, "can");
153 ok UNIVERSAL::isa(Alice => "UNIVERSAL");
155 cmp_ok UNIVERSAL::can(Alice => "can"), '==', \&UNIVERSAL::can;
157 # now use UNIVERSAL.pm and see what changes
158 eval "use UNIVERSAL";
160 ok $a->isa("UNIVERSAL");
162 my $sub2 = join ' ', sort grep { defined &{"UNIVERSAL::$_"} } keys %UNIVERSAL::;
163 # XXX import being here is really a bug
165 is $sub2, "can import isa DOES VERSION";
167 is $sub2, "DOES VERSION can import isa";
170 eval 'sub UNIVERSAL::sleep {}';
173 ok UNIVERSAL::can($b, "can");
175 ok ! $a->can("export_tags"); # a method in Exporter
177 ok ! UNIVERSAL::isa("\xff\xff\xff\0", 'HASH');
180 # test isa() and can() on magic variables
185 sub TIESCALAR { bless {} }
186 sub FETCH { "Human" }
187 tie my($x), "HumanTie";
188 ::ok $x->isa("Human");
193 # a second call to isa('UNIVERSAL') when @ISA is null failed due to caching
196 my $x = {}; bless $x, 'X';
197 ok $x->isa('UNIVERSAL');
198 ok $x->isa('UNIVERSAL');
201 # Check that the "historical accident" of UNIVERSAL having an import()
202 # method doesn't effect anyone else.
203 eval { Some::Package->import("bar") };
207 # This segfaulted in a blead.
208 fresh_perl_is('package Foo; Foo->VERSION; print "ok"', 'ok');
211 fresh_perl_is('$:; UNIVERSAL::isa(":","Unicode::String");print "ok"','ok');
224 ok( Foo->DOES( 'bar' ), 'DOES() should call DOES() on class' );
225 ok( Bar->DOES( 'Bar' ), '... and should fall back to isa()' );
226 ok( Bar->DOES( 'Foo' ), '... even when inherited' );
227 ok( Baz->DOES( 'Baz' ), '... even without inheriting any other DOES()' );
228 ok( ! Baz->DOES( 'Foo' ), '... returning true or false appropriately' );
230 ok( ! "T"->DOES( "T\0" ), 'DOES() is nul-clean' );
231 ok( ! Baz->DOES( "Baz\0Boy howdy" ), 'DOES() is nul-clean' );
236 *isa = \&UNIVERSAL::isa;
237 eval { isa({}, 'HASH') };
238 ::is($@, '', "*isa correctly found");
241 eval { UNIVERSAL::DOES([], "foo") };
242 like( $@, qr/Can't call method "DOES" on unblessed reference/,
243 'DOES call error message says DOES, not isa' );
245 # Tests for can seem to be split between here and method.t
246 # Add the verbatim perl code mentioned in the comments of
247 # Message-ID: E14ufZD-0007kD-00@libra.cus.cam.ac.uk
248 # https://www.nntp.perl.org/group/perl.perl5.porters/2001/05/msg35327.html
249 # but never actually tested.
250 is(UNIVERSAL->can("NoSuchPackage::foo"), undef);
252 @splatt::ISA = 'zlopp';
253 ok (splatt->isa('zlopp'));
254 ok (!splatt->isa('plop'));
256 # This should reset the ->isa lookup cache
257 @splatt::ISA = 'plop';
258 # And here is the new truth.
259 ok (!splatt->isa('zlopp'));
260 ok (splatt->isa('plop'));
262 use warnings "deprecated";
265 local $SIG{__WARN__} = sub { $m = $_[0] };
266 eval "use UNIVERSAL 'can'";
267 like($@, qr/^UNIVERSAL does not export anything\b/,
268 "error for UNIVERSAL->import('can')");
270 "no deprecation warning for UNIVERSAL->import('can')");
273 eval "use UNIVERSAL";
275 "no error for UNIVERSAL->import");
277 "no deprecation warning for UNIVERSAL->import");
280 # Test: [perl #66112]: change @ISA inside sub isa
288 @ISA = qw/RT66112::A/;
289 return $self->SUPER::isa(@_);
298 @RT66112::E::ISA = qw/RT66112::A/;
299 return $self->SUPER::isa(@_);
306 @RT66112::B::ISA = qw//;
307 @RT66112::C::ISA = qw/RT66112::B/;
308 @RT66112::T1::ISA = qw/RT66112::C/;
309 ok(RT66112::T1->isa('RT66112::C'), "modify \@ISA in isa (RT66112::T1 isa RT66112::C)");
311 @RT66112::B::ISA = qw//;
312 @RT66112::C::ISA = qw/RT66112::B/;
313 @RT66112::T2::ISA = qw/RT66112::C/;
314 ok(RT66112::T2->isa('RT66112::B'), "modify \@ISA in isa (RT66112::T2 isa RT66112::B)");
316 @RT66112::B::ISA = qw//;
317 @RT66112::C::ISA = qw/RT66112::B/;
318 @RT66112::T3::ISA = qw/RT66112::C/;
319 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')}";
321 @RT66112::E::ISA = qw/RT66112::D/;
322 @RT66112::T4::ISA = qw/RT66112::E/;
323 ok(RT66112::T4->isa('RT66112::E'), "modify \@ISA in isa (RT66112::T4 isa RT66112::E)");
325 @RT66112::E::ISA = qw/RT66112::D/;
326 @RT66112::T5::ISA = qw/RT66112::E/;
327 ok(! RT66112::T5->isa('RT66112::D'), "modify \@ISA in isa (RT66112::T5 not isa RT66112::D)");
329 @RT66112::E::ISA = qw/RT66112::D/;
330 @RT66112::T6::ISA = qw/RT66112::E/;
331 ok(RT66112::T6->isa('RT66112::A'), "modify \@ISA in isa (RT66112::T6 isa RT66112::A)");
334 ok(Undeclared->can("can"));
335 sub Undeclared::foo { }
336 ok(Undeclared->can("foo"));
337 ok(!Undeclared->can("something_else"));
339 ok(Undeclared->isa("UNIVERSAL"));
341 # keep this at the end to avoid messing up earlier tests, since it modifies
343 @UNIVERSAL::ISA = ('UniversalParent');
344 { package UniversalIsaTest1; }
345 ok(UniversalIsaTest1->isa('UniversalParent'));
346 ok(UniversalIsaTest2->isa('UniversalParent'));