"||"和"||"之间有什么区别?和“或"在Perl? [英] What is the difference between "||" and "or" in Perl?

查看:95
本文介绍了"||"和"||"之间有什么区别?和“或"在Perl?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C样式运算符&&||,...和它们的Perl人类可读版本"and","or",...之间有什么区别?

What is the difference between the C-style operators &&, ||, ... and their Perl human-readable version "and", "or", ...?

Internet代码似乎同时使用了它们:

It seems that Internet code uses them both:

open (FILE, $file) or die("cannot open $file");
open (FILE, $file) || die("cannot open $file");

推荐答案

来自 Perl文档:

在列表运算符的右侧,它的优先级非常低,因此它控制在那里找到的所有逗号分隔的表达式.唯一具有较低优先级的运算符是逻辑运算符"and","or"和"not",可用于评估对列表运算符的调用,而无需额外的括号.

On the right side of a list operator, it has very low precedence, such that it controls all comma-separated expressions found there. The only operators with lower precedence are the logical operators "and", "or", and "not", which may be used to evaluate calls to list operators without the need for extra parentheses.

逻辑,已定义,以及排他的.

Logical or, defined or, and exclusive or.

二进制或"返回两个周围表达式的逻辑和.它与||等效,但优先级非常低.这对于控制流很有用

Binary "or" returns the logical disjunction of the two surrounding expressions. It's equivalent to ||, except for the very low precedence. This makes it useful for control flow

print FH $data or die "Can't write to FH: $!";

这意味着它会短路:即,仅当左表达式为false时,才对右表达式进行求值.由于其优先级,您可能应该避免仅将其用于控制​​流来进行分配.

This means that it short-circuits: i.e., the right expression is evaluated only if the left expression is false. Due to its precedence, you should probably avoid using this for assignment, only for control flow.

$a = $b or $c; # Bug: this is wrong
($a = $b) or $c; # Really means this
$a = $b || $c; # Better written this way

$a = $b or $c; # Bug: this is wrong
($a = $b) or $c; # Really means this
$a = $b || $c; # Better written this way

但是,当它是一个列表上下文分配并且您尝试使用"||"时对于控制流,您可能需要使用或",以便分配具有更高的优先级.

However, when it's a list-context assignment and you're trying to use "||" for control flow, you probably need "or" so that the assignment takes higher precedence.

@info = stat($file) || die; # Oops, scalar sense of stat!
@info = stat($file) or die; # Better, now @info gets its due

@info = stat($file) || die; # Oops, scalar sense of stat!
@info = stat($file) or die; # Better, now @info gets its due

然后,您总是可以使用括号.

Then again, you could always use parentheses.

||

如果任何列表运算符(print()等)或任何一元运算符(chdir()等)后跟左括号作为下一个标记,则该运算符和括号内的参数被认为是最高的优先级,就像正常的函数调用一样.

If any list operator (print(), etc.) or any unary operator (chdir(), etc.) is followed by a left parenthesis as the next token, the operator and arguments within parentheses are taken to be of highest precedence, just like a normal function call.


例如,由于命名的一元运算符的优先级高于||:

For example, because named unary operators have higher precedence than ||:

chdir $foo || die; # (chdir $foo) || die
chdir($foo) || die; # (chdir $foo) || die
chdir ($foo) || die; # (chdir $foo) || die
chdir +($foo) || die; # (chdir $foo) || die

chdir $foo || die; # (chdir $foo) || die
chdir($foo) || die; # (chdir $foo) || die
chdir ($foo) || die; # (chdir $foo) || die
chdir +($foo) || die; # (chdir $foo) || die

这篇关于"||"和"||"之间有什么区别?和“或"在Perl?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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