如果答案已经确定,Perl是否经过优化以跳过剩余的逻辑操作数? [英] Is Perl optimized to skip remaining logic operands if the answer is already decided?

查看:51
本文介绍了如果答案已经确定,Perl是否经过优化以跳过剩余的逻辑操作数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,如果$do eq 'b',我需要匹配一些文本. 如果我运行此代码:

if (($do eq 'b') && (/text/))
{
do stuff
}

如果第一个括号为假,perl是否会评估第一个括号和第二个括号,或者在第二个括号的评估中停止它?

后续问题此处. (我不知道该提一个新问题还是将其发布在这里)

是.这种行为通常在惯用的perl中使用.

考虑:

open FILE, ">$fname" or die $!;

如果打开文件没有错误(open返回true),则不执行die语句.这不是die例程的特殊处理;这只是perl处理逻辑评估的方式.

For instance, I need to match some text if $do eq 'b'. If I run this code:

if (($do eq 'b') && (/text/))
{
do stuff
}

Would perl evaluate the first parenthesis and second parenthesis no matter what or would it stop at the evaluation of the second parenthesis if the first parenthesis was false?

Follow-up question here. (I didn't know if I should make a new question or post it here)

解决方案

Yes. This behavior is commonly used in idiomatic perl.

Consider:

open FILE, ">$fname" or die $!;

If there is no error opening the file (open returns true), the die statement is not executed. This is not special treatment of the die routine; it is just the way perl handles logic evaluation.

这篇关于如果答案已经确定,Perl是否经过优化以跳过剩余的逻辑操作数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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