<pre name="code" class="html">12.5.3 UNIVERSAL:最终的祖先类: 你可以把 UNIVERSAL 看作最终的祖先,所 有类都隐含地从它衍生而来。 INVOCANT->isa(CLASS) 如果 INVOCANT 的类是 CLASS 或者任何从 CLASS 继承来的,isa 方法返回真。 除了包名字以外,CLASS还 可以是一个内建的类型,比如 "HASH" 或者 "ARRAY"。 (准确地检查某种类型在封装和多态性机制中并不能很好 地工作。你应该依赖重 载分检给你正确的方法。) [root@wx03 test]# cat t9.pl unshift(@INC,"/root/test"); use Horse;; if (Horse->isa("Critter")) { print "true. "; } [root@wx03 test]# perl t9.pl true. [root@wx03 test]# cat t9.pl unshift(@INC,"/root/test"); use Horse;; if (Horse->isa("Horse")) { print "true. "; } [root@wx03 test]# perl t9.pl true. INVOCANT->isa(CLASS) 对应的if (Horse->isa("Horse")) 如果INVOCANT 的类是CLASS或者任何从CLASS继承来的,isa方法返回真。除了包名字以外,CLASS还可以是一个内建的类型 比如HASH 或者ARRAY [root@wx03 test]# cat t10.pl unshift(@INC,"/root/test"); use Horse;; if (Horse->isa("Critter")) { print "true. "; } [root@wx03 test]# perl t10.pl true. 判断Horse类是否继承("Critter")) 类 isa(CLASS) 如果调用 "isa" 的对象是隶属于 "CLASS" 或者它的子类, 那么 "isa" 返回 *真值*。 你也可以用传递两个参数的办法直接调用 "UNIVERSAL::isa":第一个参数是 一个对象(甚至是普通的引用),这个办法可以用来检查一个对象是不是属于 指定的类型。例如: if(UNIVERSAL::isa($ref, 'ARRAY')) { #... } [root@wx03 test]# cat Horse.pm package Horse; our @ISA = "Critter"; sub new { my $invocant = shift; my $class = ref($invocant) || $invocant; my $self = { color => "bay", legs => 4, owner => undef, @_, # 覆盖以前的属性 }; return bless $self, $class; }; sub sum1 { $self=shift; my $a=shift; my $b=shift; return $a + $b + 7; }; our @arr=qw/1 2 3 4 5 6 7/; our %h1=(1,2,3,4,5,6,7,8); 1; unshift(@INC,"/root/test"); use Horse;; $ua=Horse->new(); if (UNIVERSAL::isa($ua,'HASH')) { print "true. "; [root@wx03 test]# perl t10.pl true. 判断 $ua是不是HASH 判断一个引用是不是被bless过的: [root@wx03 test]# perl t10.pl It's an object [root@wx03 test]# [root@wx03 test]# [root@wx03 test]# cat t10.pl unshift(@INC,"/root/test"); use Horse;; $ua=Horse->new(); print "It's an object " if UNIVERSAL::isa($ua, 'UNIVERSAL'); [root@wx03 test]# perl t10.pl It's an object 注释掉bless [root@wx03 test]# cat Horse.pm package Horse; our @ISA = "Critter"; sub new { my $invocant = shift; my $class = ref($invocant) || $invocant; my $self = { color => "bay", legs => 4, owner => undef, @_, # 覆盖以前的属性 }; #return bless $self, $class; return $self; }; sub sum1 { $self=shift; my $a=shift; my $b=shift; return $a + $b + 7; }; our @arr=qw/1 2 3 4 5 6 7/; our %h1=(1,2,3,4,5,6,7,8); 1; [root@wx03 test]# perl t10.pl [root@wx03 test]# 没有返回 [root@wx03 test]# cat t10.pl unshift(@INC,"/root/test"); use Horse;; $ua=Horse->new(); #print "It's an object " if UNIVERSAL::isa($ua, 'UNIVERSAL'); $code=Horse->can(sum1); use Data::Dumper; $str=Dumper($code); print "$str is $str "; [root@wx03 test]# perl t10.pl $str is $VAR1 = sub { "DUMMY" }; 如果 INVOCANT 中有 METHOD,那么 can 方法就返回一个可以调用的该子过程的 引用。如果没有定义这样的 子过程,can 返回 undef。 12.5.3 UNIVERSAL:最终的祖先类: 在 UNIVERSAL 类里面有下面的预定义的方法可以使用,因此所有类中都可以用它们。而且 不管它们是被当作类 方法还是对象方法调用的都能运行。 INVOCANT->isa(CLASS) 如果 INVOCANT 的类是 CLASS 或者任何从 CLASS 继承来的,isa 方法返回真。 [root@wx03 test]# cat t12.pl unshift(@INC,"/root/test"); use Horse;; if (Horse->isa("Critter")) { print "true. "; } [root@wx03 test]# perl t12.pl true. [root@wx03 test]# cat t12.pl unshift(@INC,"/root/test"); use Horse;; if (Horse->isa("Horse")) { print "true. "; } [root@wx03 test]# perl t12.pl true. [root@wx03 test]# cat t12.pl unshift(@INC,"/root/test"); use Horse;; if (Horse->isa("Horse1")) { print "true. "; } [root@wx03 test]# perl t12.pl [root@wx03 test]# [root@wx03 test]# cat t13.pl unshift(@INC,"/root/test"); use Horse;; $ua=Horse->new(); if (UNIVERSAL::isa($ua,'HASH')) { print "true. "; }; [root@wx03 test]# perl t13.pl true. 检测$ua是否是hash [root@wx03 test]# cat t13.pl unshift(@INC,"/root/test"); use Horse;; $ua=Horse->new(); if (Horse->can("sum1")) { print "Our invocant can copy. "; } [root@wx03 test]# perl t13.pl Our invocant can copy. 如果 INVOCANT 中有 METHOD,那么 can 方法就返回一个可以调用的该子过程的 引用。如果没有定义这样的 子过程,can 返回 undef。