This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
-x should be C<-x>, reported by Gerben Wierda.
[perl5.git] / t / op / universal.t
CommitLineData
44a8e56a 1#!./perl
2#
3# check UNIVERSAL
4#
5
e09f3e01
MG
6BEGIN {
7 chdir 't' if -d 't';
8 @INC = '../lib' if -d '../lib';
9}
10
84902520 11print "1..72\n";
44a8e56a 12
13$a = {};
14bless $a, "Bob";
ff0cee69 15print "not " unless $a->isa("Bob");
16print "ok 1\n";
44a8e56a 17
ff0cee69 18package Human;
19sub eat {}
44a8e56a 20
ff0cee69 21package Female;
22@ISA=qw(Human);
44a8e56a 23
ff0cee69 24package Alice;
25@ISA=qw(Bob Female);
26sub drink {}
27sub new { bless {} }
44a8e56a 28
e09f3e01
MG
29$Alice::VERSION = 2.718;
30
44a8e56a 31package main;
e09f3e01
MG
32
33my $i = 2;
34sub test { print "not " unless shift; print "ok $i\n"; $i++; }
35
ff0cee69 36$a = new Alice;
44a8e56a 37
e09f3e01 38test $a->isa("Alice");
44a8e56a 39
e09f3e01
MG
40test $a->isa("Bob");
41
42test $a->isa("Female");
43
44test $a->isa("Human");
45
46test ! $a->isa("Male");
47
48test $a->can("drink");
49
50test $a->can("eat");
51
52test ! $a->can("sleep");
53
54my $b = 'abc';
55my @refs = qw(SCALAR SCALAR LVALUE GLOB ARRAY HASH CODE);
56my @vals = ( \$b, \3.14, \substr($b,1,1), \*b, [], {}, sub {} );
57for ($p=0; $p < @refs; $p++) {
58 for ($q=0; $q < @vals; $q++) {
59 test UNIVERSAL::isa($vals[$p], $refs[$q]) eq ($p==$q or $p+$q==1);
60 };
61};
62
63test ! UNIVERSAL::can(23, "can");
64
65test $a->can("VERSION");
66
67test $a->can("can");
84902520 68test ! $a->can("export_tags"); # a method in Exporter
e09f3e01
MG
69
70test (eval { $a->VERSION }) == 2.718;
71
72test ! (eval { $a->VERSION(2.719) }) &&
73 $@ =~ /^Alice version 2.719 required--this is only version 2.718 at /;
44a8e56a 74
e09f3e01 75test (eval { $a->VERSION(2.718) }) && ! $@;
ff0cee69 76
e09f3e01 77my $subs = join ' ', sort grep { defined &{"UNIVERSAL::$_"} } keys %UNIVERSAL::;
9d116dd7
JH
78if ('a' lt 'A') {
79 test $subs eq "can isa VERSION";
80} else {
81 test $subs eq "VERSION can isa";
82}
ff0cee69 83
e09f3e01 84test $a->isa("UNIVERSAL");
ff0cee69 85
84902520 86# now use UNIVERSAL.pm and see what changes
e09f3e01 87eval "use UNIVERSAL";
ff0cee69 88
e09f3e01 89test $a->isa("UNIVERSAL");
44a8e56a 90
e09f3e01 91my $sub2 = join ' ', sort grep { defined &{"UNIVERSAL::$_"} } keys %UNIVERSAL::;
84902520 92# XXX import being here is really a bug
9d116dd7
JH
93if ('a' lt 'A') {
94 test $sub2 eq "can import isa VERSION";
95} else {
96 test $sub2 eq "VERSION can import isa";
97}
44a8e56a 98
e09f3e01
MG
99eval 'sub UNIVERSAL::sleep {}';
100test $a->can("sleep");
44a8e56a 101
e09f3e01 102test ! UNIVERSAL::can($b, "can");
84902520
TB
103
104test ! $a->can("export_tags"); # a method in Exporter