my $abc = delete $args{ 'abc' } // croak 'some information!';
解决方法
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). 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)
.
原文地址:https://www.jb51.cc/Perl/172707.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。