微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

这两个语句在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.jb51.cc/Perl/172137.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐