这两个语句在perl中是否总是相同的?

$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是多态性的语言定义的裸字),因此它被视为一种方法,它不存在,因此也就是错误.

相关文章

1. 如何去重 #!/usr/bin/perl use strict; my %hash; while(...
最近写了一个perl脚本,实现的功能是将表格中其中两列的数据...
表的数据字典格式如下:如果手动写MySQL建表语句,确认麻烦,...
巡检类工作经常会出具日报,最近在原有日报的基础上又新增了...
在实际生产环境中,常常需要从后台日志中截取报文,报文的形...
最近写的一个perl程序,通过关键词匹配统计其出现的频率,让...