$obj->SUPER::promote(); $obj->SUPER->promote();
谁知道他们是否一样?
解决方法
不. – > operator表示调用引用(在本例中为对象引用),它将查找SUPER方法,而不是超级基类.
#!/usr/bin/perl -w package MyOBJ; use strict; use warnings; use Data::Dumper; sub new { my ($class) = @_; my $self = {}; bless $self,$class; return $self; } sub promote { my ($self) = @_; print Dumper($self); } 1; package MyOBJ::Sub; use strict; use warnings; use base 'MyOBJ'; 1; use strict; use warnings; my $obj = MyOBJ::Sub->new(); $obj->SUPER::promote();
运行它,你会得到:
$VAR1 = bless( {},'MyOBJ::Sub' );
当您更改要使用的最后一行时 – >而不是::你得到:
Can't locate object method "SUPER" via package "MyOBJ" at test.pl line 45.
来自“perldoc perlop”手册
The Arrow Operator
If the right side is either a “[…]”,“{…}”,or a “(…)” subscript,then the left side must be either a hard or symbolic reference to an array,a hash,or a subroutine respectively.
Otherwise,the right side is a method name or a simple scalar variable containing either the method name or a subroutine reference,and the left side must be either an object (a blessed reference) or a class name (that is,a package name)
由于左侧既不是对象引用也不是类名(SUPER是多态性的语言定义的裸字),因此它被视为一种方法,它不存在,因此也就是错误.