这里的双斜杠是什么意思? [英] what does the double forward slash mean here?

查看:362
本文介绍了这里的双斜杠是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是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!';

推荐答案

从此页面开始:

尽管在C语言中没有直接等效项,但是Perl的//运算符与其C样式or有关.实际上,它与||完全相同,只是它测试左侧的定义而不是其.因此,如果已定义EXPR1 // EXPR2,则返回EXPR1的值,否则,将返回EXPR2的值. (EXPR1标量上下文中进行评估,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-style or. 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 of EXPR1 if it's defined, otherwise, the value of EXPR2 is returned. (EXPR1 is evaluated in scalar context, EXPR2 in the context of // itself). Usually, this is the same result as defined(EXPR1) ? EXPR1 : EXPR2 (except that the ternary-operator form can be used as a lvalue, while EXPR1 // 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, use defined($a // $b).

这篇关于这里的双斜杠是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆