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 || '.';

相关文章

1. 如何去重 #!/usr/bin/perl use strict; my %hash; while(...
最近写了一个perl脚本,实现的功能是将表格中其中两列的数据...
表的数据字典格式如下:如果手动写MySQL建表语句,确认麻烦,...
巡检类工作经常会出具日报,最近在原有日报的基础上又新增了...
在实际生产环境中,常常需要从后台日志中截取报文,报文的形...
最近写的一个perl程序,通过关键词匹配统计其出现的频率,让...