16 print "not " unless $a->isa("Bob");
28 sub drink { return "drinking " . $_[1] }
31 $Alice::VERSION = 2.718;
50 print "not " unless $_[0];
52 print " # at ", (caller)[1], ", line ", (caller)[2] unless $_[0];
59 test $a->isa("Alice");
60 test $a->isa("main::Alice"); # check that alternate class names work
62 test(("main::Alice"->new)->isa("Alice"));
65 test $a->isa("main::Bob");
67 test $a->isa("Female");
69 test $a->isa("Human");
71 test ! $a->isa("Male");
73 test ! $a->isa('Programmer');
78 test ! $a->can("sleep");
79 test my $ref = $a->can("drink"); # returns a coderef
80 test $a->$ref("tea") eq "drinking tea"; # ... which works
81 test $ref = $a->can("sing");
83 test $@; # ... but not if no actual subroutine
85 test (!Cedric->isa('Programmer'));
87 test (Cedric->isa('Human'));
89 push(@Cedric::ISA,'Programmer');
91 test (Cedric->isa('Programmer'));
95 base::->import('Programmer');
98 test $a->isa('Programmer');
99 test $a->isa("Female");
101 @Cedric::ISA = qw(Bob);
103 test (!Cedric->isa('Programmer'));
106 my @refs = qw(SCALAR SCALAR LVALUE GLOB ARRAY HASH CODE);
107 my @vals = ( \$b, \3.14, \substr($b,1,1), \*b, [], {}, sub {} );
108 for ($p=0; $p < @refs; $p++) {
109 for ($q=0; $q < @vals; $q++) {
110 test UNIVERSAL::isa($vals[$p], $refs[$q]) eq ($p==$q or $p+$q==1);
114 test ! UNIVERSAL::can(23, "can");
116 test $a->can("VERSION");
119 test ! $a->can("export_tags"); # a method in Exporter
121 test (eval { $a->VERSION }) == 2.718;
123 test ! (eval { $a->VERSION(2.719) }) &&
124 $@ =~ /^Alice version 2.71(?:9|8999\d+) required--this is only version 2.718 at /;
126 test (eval { $a->VERSION(2.718) }) && ! $@;
128 my $subs = join ' ', sort grep { defined &{"UNIVERSAL::$_"} } keys %UNIVERSAL::;
129 ## The test for import here is *not* because we want to ensure that UNIVERSAL
130 ## can always import; it is an historical accident that UNIVERSAL can import.
132 test $subs eq "can import isa VERSION";
134 test $subs eq "VERSION can import isa";
137 test $a->isa("UNIVERSAL");
139 test ! UNIVERSAL::isa([], "UNIVERSAL");
141 test ! UNIVERSAL::can({}, "can");
143 test UNIVERSAL::isa(Alice => "UNIVERSAL");
145 test UNIVERSAL::can(Alice => "can") == \&UNIVERSAL::can;
147 # now use UNIVERSAL.pm and see what changes
148 eval "use UNIVERSAL";
150 test $a->isa("UNIVERSAL");
152 my $sub2 = join ' ', sort grep { defined &{"UNIVERSAL::$_"} } keys %UNIVERSAL::;
153 # XXX import being here is really a bug
155 test $sub2 eq "can import isa VERSION";
157 test $sub2 eq "VERSION can import isa";
160 eval 'sub UNIVERSAL::sleep {}';
161 test $a->can("sleep");
163 test ! UNIVERSAL::can($b, "can");
165 test ! $a->can("export_tags"); # a method in Exporter
167 test ! UNIVERSAL::isa("\xff\xff\xff\0", 'HASH');
171 use UNIVERSAL qw( isa can VERSION );
173 main::test isa "Pickup", UNIVERSAL;
174 main::test can( "Pickup", "can" ) == \&UNIVERSAL::can;
175 main::test VERSION "UNIVERSAL" ;
179 # test isa() and can() on magic variables
181 test $1->isa("Human");
184 sub TIESCALAR { bless {} }
185 sub FETCH { "Human" }
186 tie my($x), "HumanTie";
187 ::test $x->isa("Human");
188 ::test $x->can("eat");