如果没有给出命令行参数,我该如何使用默认数组?
How do I use a default array if no command line arguments are given?
如果没有给出命令行参数,下面的内容似乎可以正常工作,但是当它们全部给出时,我得到的是提供的参数数量,而不是参数本身。 @ARGV
似乎被 ||
强制标量。我也试过使用 or
和 //
得到类似的结果。此处使用的正确运算符是什么?
say for @ARGV || qw/one two three/;
||
operator 根据其作用的性质强加标量上下文
Binary "or"
returns the logical disjunction of the two surrounding expressions. It's equivalent to ||
except for the very low precedence.
(强调我的)。因此,当它的左侧操作数是一个数组时,它获取数组的长度。
但是,如果那是 0
,那么右侧将被计算
This means that it short-circuits: the right expression is evaluated only if the left expression is false.
中的详细说明
Scalar or list context propagates down to the right operand if it is evaluated.
所以在这种情况下你会得到列表。
没有可以执行您的语句所需的运算符。最接近的可能是
say for (@ARGV ? @ARGV : qw(one two));
但是有更好更系统的方法来处理@ARGV
。
分两行写就可以了
@ARGV = qw[...] unless @ARGV;
say for @ARGV;
如果没有给出命令行参数,下面的内容似乎可以正常工作,但是当它们全部给出时,我得到的是提供的参数数量,而不是参数本身。 @ARGV
似乎被 ||
强制标量。我也试过使用 or
和 //
得到类似的结果。此处使用的正确运算符是什么?
say for @ARGV || qw/one two three/;
||
operator 根据其作用的性质强加标量上下文
Binary
"or"
returns the logical disjunction of the two surrounding expressions. It's equivalent to||
except for the very low precedence.
(强调我的)。因此,当它的左侧操作数是一个数组时,它获取数组的长度。
但是,如果那是 0
,那么右侧将被计算
中的详细说明This means that it short-circuits: the right expression is evaluated only if the left expression is false.
Scalar or list context propagates down to the right operand if it is evaluated.
所以在这种情况下你会得到列表。
没有可以执行您的语句所需的运算符。最接近的可能是
say for (@ARGV ? @ARGV : qw(one two));
但是有更好更系统的方法来处理@ARGV
。
分两行写就可以了
@ARGV = qw[...] unless @ARGV;
say for @ARGV;