如果答案已经确定,Perl是否经过优化以跳过剩余的逻辑操作数? [英] Is Perl optimized to skip remaining logic operands if the answer is already decided?
问题描述
例如,如果$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屋!