加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

perl – sub foo使用的语法是什么:method {shift-> bar(@_)}

发布时间:2020-12-15 21:54:00 所属栏目:大数据 来源:网络整理
导读:sub foo : method { shift-bar(@_) } 什么:方法在这里意味着什么? 我从来没用过这种方式…… 解决方法 :方法是 function attribute描述.如此标记的子程序不会触发“不明确的调用解析为CORE ::%s”警告. 从ysth的评论: The warning happens when the sub
sub foo : method { shift->bar(@_) }

什么:方法在这里意味着什么?

我从来没用过这种方式……

解决方法

:方法是 function attribute描述.如此标记的子程序不会触发“不明确的调用解析为CORE ::%s”警告.

从ysth的评论:

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.

更新

调用foo时,此代码只调用方法栏:

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(@_);
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读