sub foo : method { shift->bar(@_) }
What does : method
mean here?
I've ne开发者_如何转开发ver used it this way ...
: method
is function attribute description. A subroutine so marked will not trigger the "Ambiguous call resolved as CORE::%s" warning.
From ysth's comment:
The warning happens when the sub has the same name as a builtin and it is called without & and not as a method call; perl uses the builtin instead but gives a warning. The :method quiets the warning because it clearly indicates the sub was never intended to be called as a non-method anyway.
Update
This code just calls method bar
when foo
is called:
sub foo : method { ## Mark function as method
shift->bar(@_) ## Pass all parameters to bar method of same object
}
More details:
: method
- Indicates that the referenced subroutine is a method. A subroutine so marked will not trigger the "Ambiguous call resolved as CORE::%s" warning.shift
- gets first parameter from@_
, which will be$self
->bar(@_)
- call same class methodbar
with all other parameters
You can read this as:
sub foo : method {
my ($self) = shift @_;
return $self->bar(@_);
}
精彩评论