微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

perl – 写“@ARGV || =’.’;”有什么问题?

参见英文答案 > Why doesn’t ||= work with arrays?2个
为什么Perl会在下面的代码段中添加一个功能
$perl -Mstrict -wE '@ARGV ||= ".";'
Can't modify array dereference in logical or assignment (||=) at -e line 1,near "'.';"
Execution of -e aborted due to compilation errors.

虽然它很愉快地处理

$perl -Mstrict -wE '@ARGV = @ARGV || ".";'

我没有看到perldiag的解释在这里有多大帮助:

Can’t modify %s in %s

(F) You aren’t allowed to assign to the item indicated,or otherwise
try to change it,such as with an auto-increment.

非常感谢对这种行为更加人性化的解释.

解决方法

代码@ARGV不可能同时返回数组本身及其中的元素数,所以@ARGV || =’.’;没有意义.你需要评估@ARGV两次,一次是在标量上下文中(以获得元素数),一次是作为左值(以获得数组本身).
@ARGV = @ARGV || '.';

原文地址:https://www.jb51.cc/Perl/171803.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐