Perl 中的//= 是什么? [英] What is //= in Perl?
问题描述
我找到了一些我需要的 Perl 代码示例,但其中包含我不认识的内容.
I found an example of some Perl code I needed, but it had something in it that I didn't recognise.
my $i //= '08';
我在任何地方都找不到对此的任何参考!好像是一样的:
I can't find any reference to this anywhere! It appears to be the same as:
my $i = '08';
我错过了什么吗?
推荐答案
//=
操作符是 的赋值运算符">赋值运算符'逻辑定义或' 运算符.
The //=
operator is the assignment operator version of the //
or 'logical defined-or' operator.
在 my
变量声明的上下文中,变量最初是未定义的,所以它是等价于赋值(最好写成 my $i= '08';
).不过总的来说,
In the context of a my
variable declaration, the variable is initially undefined so it is equivalent to assignment (and would be better written as my $i = '08';
). In general, though,
$i //= '08';
是以下的简写:
$i = (defined $i) ? $i : '08';
它在 Perl 操作符 (perldoc perlop
) 中的两个位置(简洁地在赋值操作符部分下,以及在逻辑定义或"部分中完整描述)中有记录.它是在 Perl 5.10.0 中添加的.
It is documented in the Perl operators (perldoc perlop
) in two places (tersely under the assignment operators section, and in full in the section on 'logical defined-or'). It was added in Perl 5.10.0.
这篇关于Perl 中的//= 是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!