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

标签 perl oop

$obj->SUPER::promote();

$obj->SUPER->promote();

有谁知道他们是一样的吗?

最佳答案

-> 运算符意味着调用一个引用(在这种情况下,一个对象引用),它会寻找 super 方法,而不是 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 是一种为多态定义的裸字语言),因此它被视为不存在的方法,因此会出现错误。

关于perl - 这两个语句在 perl 中总是相同的吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6479200/

相关文章:

excel - Perl 使用 OLE 写入 Excel 工作表

perl - 如何在perl中将文件句柄作为模块和子之间的引用传递

oop - 装饰器设计模式问题?

Java重载和覆盖

javascript - 如何使用 JavaScript 变量值作为对象选择器

perl - Perl 什么时候强加字符串上下文?

perl - 如何使用 makefile/autoconf 制作可安装的 Perl 程序?

javascript - Javascript 如何将值从 "super class"传递到 "child class"

oop - 没有 writeln 时找不到构造函数

perl - Perl 中的 AWK?我有一个 AWK 命令行。我不知道如何在 Perl 脚本中使用它