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");
66 ok ! $a->isa('Programmer');
71 ok ! $a->can("sleep");
72 ok my $ref = $a->can("drink"); # returns a coderef
73 is $a->$ref("tea"), "drinking tea"; # ... which works
74 ok $ref = $a->can("sing");
76 ok $@; # ... but not if no actual subroutine
78 ok (!Cedric->isa('Programmer'));
80 ok (Cedric->isa('Human'));
82 push(@Cedric::ISA,'Programmer');
84 ok (Cedric->isa('Programmer'));
88 base::->import('Programmer');
91 ok $a->isa('Programmer');
94 @Cedric::ISA = qw(Bob);
96 ok (!Cedric->isa('Programmer'));
99 my @refs = qw(SCALAR SCALAR LVALUE GLOB ARRAY HASH CODE);
100 my @vals = ( \$b, \3.14, \substr($b,1,1), \*b, [], {}, sub {} );
101 for ($p=0; $p < @refs; $p++) {
102 for ($q=0; $q < @vals; $q++) {
103 is UNIVERSAL::isa($vals[$p], $refs[$q]), ($p==$q or $p+$q==1);
107 ok ! UNIVERSAL::can(23, "can");
109 ok $a->can("VERSION");
112 ok ! $a->can("export_tags"); # a method in Exporter
114 cmp_ok eval { $a->VERSION }, '==', 2.718;
116 ok ! (eval { $a->VERSION(2.719) });
117 like $@, qr/^Alice version 2.719 required--this is only version 2.718 at /;
119 ok (eval { $a->VERSION(2.718) });
122 my $subs = join ' ', sort grep { defined &{"UNIVERSAL::$_"} } keys %UNIVERSAL::;
123 ## The test for import here is *not* because we want to ensure that UNIVERSAL
124 ## can always import; it is an historical accident that UNIVERSAL can import.
126 is $subs, "can import isa DOES VERSION";
128 is $subs, "DOES VERSION can import isa";
131 ok $a->isa("UNIVERSAL");
133 ok ! UNIVERSAL::isa([], "UNIVERSAL");
135 ok ! UNIVERSAL::can({}, "can");
137 ok UNIVERSAL::isa(Alice => "UNIVERSAL");
139 cmp_ok UNIVERSAL::can(Alice => "can"), '==', \&UNIVERSAL::can;
141 # now use UNIVERSAL.pm and see what changes
142 eval "use UNIVERSAL";
144 ok $a->isa("UNIVERSAL");
146 my $sub2 = join ' ', sort grep { defined &{"UNIVERSAL::$_"} } keys %UNIVERSAL::;
147 # XXX import being here is really a bug
149 is $sub2, "can import isa DOES VERSION";
151 is $sub2, "DOES VERSION can import isa";
154 eval 'sub UNIVERSAL::sleep {}';
157 ok ! UNIVERSAL::can($b, "can");
159 ok ! $a->can("export_tags"); # a method in Exporter
161 ok ! UNIVERSAL::isa("\xff\xff\xff\0", 'HASH');
165 use UNIVERSAL qw( isa can VERSION );
167 ::ok isa "Pickup", UNIVERSAL;
168 ::cmp_ok can( "Pickup", "can" ), '==', \&UNIVERSAL::can;
169 ::ok VERSION "UNIVERSAL" ;
173 # test isa() and can() on magic variables
178 sub TIESCALAR { bless {} }
179 sub FETCH { "Human" }
180 tie my($x), "HumanTie";
181 ::ok $x->isa("Human");
186 # a second call to isa('UNIVERSAL') when @ISA is null failed due to caching
189 my $x = {}; bless $x, 'X';
190 ok $x->isa('UNIVERSAL');
191 ok $x->isa('UNIVERSAL');
194 # Check that the "historical accident" of UNIVERSAL having an import()
195 # method doesn't effect anyone else.
196 eval { Some::Package->import("bar") };
200 # This segfaulted in a blead.
201 fresh_perl_is('package Foo; Foo->VERSION; print "ok"', 'ok');
214 ok( Foo->DOES( 'bar' ), 'DOES() should call DOES() on class' );
215 ok( Bar->DOES( 'Bar' ), '... and should fall back to isa()' );
216 ok( Bar->DOES( 'Foo' ), '... even when inherited' );
217 ok( Baz->DOES( 'Baz' ), '... even without inheriting any other DOES()' );
218 ok( ! Baz->DOES( 'Foo' ), '... returning true or false appropriately' );
223 *isa = \&UNIVERSAL::isa;
224 eval { isa({}, 'HASH') };
225 ::is($@, '', "*isa correctly found");
228 eval { UNIVERSAL::DOES([], "foo") };
229 like( $@, qr/Can't call method "DOES" on unblessed reference/,
230 'DOES call error message says DOES, not isa' );