$obj->SUPER::promote();
$obj->SUPER->promote();
开发者_运维百科Anyone knows if they're the same?
No. The -> operator means call a reference (in this case, an object reference), and it will look for the SUPER method, not the super base class.
Here is code to show it:
#!/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();
Run it, you'll get:
$VAR1 = bless( {}, 'MyOBJ::Sub' );
When you change the last line to use -> instead of :: you get:
Can't locate object method "SUPER" via package "MyOBJ" at test.pl line 45.
From the "perldoc perlop" manual
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)
Since the left side is neither an object ref or a class name (SUPER is a language defined bareword for polymorphism), it's treated as a method, which doesn't exist, hence the error.
精彩评论