为什么||=不能处理数组?

时间:2022-04-20 13:27:56

I use the ||= operator to provide default values for variables, like

我使用||=操作符为变量提供默认值,比如。

$x ||= 1;

I tried to use this syntax with an array but got a syntax error:

我试着在数组中使用这种语法,但是有一个语法错误:

@array||= 1..3; 
Can't modify array dereference in logical or assignment (||=) ...

What does it mean and how should I provide arrays with default values?

它是什么意思?我应该如何为数组提供默认值?

2 个解决方案

#1


18  

Because || is a scalar operator. If @array||= 1..3; worked, it would evaluate 1..3 in scalar context, which is not what you want. It's also evaluating the array in scalar context (which is ok, because an empty array in scalar context is false), except that you can't assign to scalar(@array).

因为||是一个标量运算符。如果@array | | = 1 . . 3;成功了,它会评估1。在标量上下文中,这不是你想要的。它还在标量上下文中计算数组(这是可以的,因为标量上下文中的空数组是假的),除了不能赋值给标量(@array)。

To assign a default value, use:

若要分配默认值,请使用:

@array = 1..3 unless @array;

But note that there's no way to tell the difference between an array that has never been initialized and one that has been assigned the empty list. It's not like a scalar, where you can distinguish between undef and the empty string (although ||= doesn't distinguish between them).

但是请注意,没有办法区分从未初始化过的数组和已分配空列表的数组之间的区别。它不像一个标量,可以区分undef和空字符串(尽管||=不区分它们)。

eugene y found this perl.perl5.porters message (the official Perl developers' mailing list) that goes into more detail about this.

尤金找到了这个罪犯。搬运工消息(Perl开发人员的官方邮件列表),详细介绍了这一点。

#2


16  

This page has a good explanation, imho:

这个页面有一个很好的解释,imho:

op= can occur between any two expressions, not just a var and an expression, but the left one must be an lvalue in scalar context.

op=可以出现在任意两个表达式之间,不仅是var和表达式,而且左边的表达式必须是标量上下文中的lvalue。

Since @x ||= 42 is equivalent to scalar(@x) = @x || 42, and you aren't allowed to use scalar(@x) as an lvalue, you get an error.

由于@x ||= 42等于标量(@x) = @x || 42,并且不允许使用标量(@x)作为lvalue,因此会出现错误。

#1


18  

Because || is a scalar operator. If @array||= 1..3; worked, it would evaluate 1..3 in scalar context, which is not what you want. It's also evaluating the array in scalar context (which is ok, because an empty array in scalar context is false), except that you can't assign to scalar(@array).

因为||是一个标量运算符。如果@array | | = 1 . . 3;成功了,它会评估1。在标量上下文中,这不是你想要的。它还在标量上下文中计算数组(这是可以的,因为标量上下文中的空数组是假的),除了不能赋值给标量(@array)。

To assign a default value, use:

若要分配默认值,请使用:

@array = 1..3 unless @array;

But note that there's no way to tell the difference between an array that has never been initialized and one that has been assigned the empty list. It's not like a scalar, where you can distinguish between undef and the empty string (although ||= doesn't distinguish between them).

但是请注意,没有办法区分从未初始化过的数组和已分配空列表的数组之间的区别。它不像一个标量,可以区分undef和空字符串(尽管||=不区分它们)。

eugene y found this perl.perl5.porters message (the official Perl developers' mailing list) that goes into more detail about this.

尤金找到了这个罪犯。搬运工消息(Perl开发人员的官方邮件列表),详细介绍了这一点。

#2


16  

This page has a good explanation, imho:

这个页面有一个很好的解释,imho:

op= can occur between any two expressions, not just a var and an expression, but the left one must be an lvalue in scalar context.

op=可以出现在任意两个表达式之间,不仅是var和表达式,而且左边的表达式必须是标量上下文中的lvalue。

Since @x ||= 42 is equivalent to scalar(@x) = @x || 42, and you aren't allowed to use scalar(@x) as an lvalue, you get an error.

由于@x ||= 42等于标量(@x) = @x || 42,并且不允许使用标量(@x)作为lvalue,因此会出现错误。