解决方法
:方法是
function attribute描述.如此标记的子程序不会触发“不明确的调用解析为CORE ::%s”警告.
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.
更新
sub foo : method { ## Mark function as method shift->bar(@_) ## Pass all parameters to bar method of same object }
更多细节:
>:method – 表示引用的子例程是一个方法.如此标记的子程序不会触发“不明确的调用解析为CORE ::%s”警告.
> shift – 从@_获取第一个参数,这将是$self
> – > bar(@_) – 使用所有其他参数调用相同的类方法栏
你可以这样读:
sub foo : method { my ($self) = shift @_; return $self->bar(@_); }