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

前端之家收集整理的这篇文章主要介绍了这两个语句在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是多态性的语言定义的裸字),因此它被视为一种方法,它不存在,因此也就是错误.

原文链接:https://www.f2er.com/Perl/172137.html

猜你在找的Perl相关文章