$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/