双正斜杠在这里是什么意思? [英] what does the double forward slash mean here?
问题描述
我是 Perl 的新手,在工作中遇到了这段代码,我搜索了一段时间但没有找到答案.任何人都可以帮助用简单的英语解释它的功能吗?谢谢.
I'm new to Perl and came across this piece of code at work, I search for a while but did not find the answer. Can anyone help to explain its function in plain english? thanks.
my $abc = delete $args{ 'abc' } // croak 'some information!';
推荐答案
从此页面:http://perldoc.perl.org/perlop.html#Logical-Defined-Or
虽然在 C 中没有直接的等价物,但 Perl 的 //
运算符与其 C 风格的 or
相关.实际上,它与||
完全相同,只是它测试的是左侧的定义性 而不是它的真相.因此,EXPR1//EXPR2
返回EXPR1
的值,否则返回EXPR2
的值.(EXPR1
在 scalar 上下文中求值,EXPR2
在 //
本身的上下文中求值).通常,这与 defined(EXPR1) 的结果相同?EXPR1 : EXPR2
(除了三元运算符形式可以用作左值,而 EXPR1//EXPR2
不能,并且 EXPR1 只会被评估一次).这对于为变量提供默认值非常有用.如果您确实想测试是否至少定义了 $a
和 $b
之一,请使用 defined($a//$b)
.
Although it has no direct equivalent in C, Perl's
//
operator is related to its C-styleor
. In fact, it's exactly the same as||
, except that it tests the left hand side's definedness instead of its truth. Thus,EXPR1 // EXPR2
returns the value ofEXPR1
if it's defined, otherwise, the value ofEXPR2
is returned. (EXPR1
is evaluated in scalar context,EXPR2
in the context of//
itself). Usually, this is the same result asdefined(EXPR1) ? EXPR1 : EXPR2
(except that the ternary-operator form can be used as a lvalue, whileEXPR1 // EXPR2
cannot, and EXPR1 will only be evaluated once). This is very useful for providing default values for variables. If you actually want to test if at least one of$a
and$b
is defined, usedefined($a // $b)
.
这篇关于双正斜杠在这里是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!