在以下匿名子例程声明中,Perl似乎将其解析为间接方法调用,而不是作为子例程:
use 5.010; use strict; use warnings; sub proxy { my $new = shift; say "creating proxy: $new"; sub :lvalue { say "running proxy: $new"; tie my $ret,'Some::Package',shift,$new; $ret } } say "before"; my $p1 = proxy '_value'; say "p1 declared: $p1"; my $p2 = proxy 'value'; say "p2 declared: $p2";
打印:
before creating proxy: _value running proxy: _value Can't locate object method "TIESCALAR" via package "Some::Package" ...
如果返回或我的$sub =添加在sub:lvalue {…之前,一切正常工作,它打印:
before creating proxy: _value p1 declared: CODE(0x4c7e6c) creating proxy: value p2 declared: CODE(0x1ea85e4)
如果从子例程中删除:lvalue属性(但当然会更改功能),它也可以工作.
所以我的问题是为什么会发生这种情况?这是Perl中有关匿名子程序中的属性的错误吗?是由于某种原因预期的行为吗?如果是错误,是否注册?