perl 运算符“||="是什么?做? [英] what does perl operator "||=" do?

查看:44
本文介绍了perl 运算符“||="是什么?做?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

运算符 ||= 在 perl 中有什么作用?

What does the operator ||= do in perl?

更具体地说,如果您有这样的代码:

to be more specific if you have a code like:

my ($my_link);
$my_link  ||= DownloadF($file,'l') if $s->{_l};
$my_link  ||= DownloadF($file,'h') if $s->{_h};
$my_link  ||= DownloadF($file,'o') if $s->{_o};

||= 应该做什么,||= 和简单的 = 有什么区别?

what is ||= suppose to do and what is the difference between ||= and a simple =?

推荐答案

Perl 支持大量赋值运算符.||= 只是一个 logical or(带有短路)赋值.

Perl supports lots of assignment operators. ||= is just a logical or (complete with shortcircuit,) assignment.

所以基本上你正在看的是:

So essentially what you're looking at is:

if ($s->{_l}) {
  $my_link = $my_link || DownloadF($file,'l');
}

因此,如果 $my_link 评估为某个真值,则 $my_link 将被分配给自身(本质上是无操作),否则 DownloadF 的结果 已分配.

So if $my_link evaluates to some true value then $my_link will be assigned to itself (a no-op essentially), otherwise the result of DownloadF is assigned.

perl 支持的其他赋值运算符:

Other assignment operators supported by perl:

 **= += *= &= <<= &&=
-= /= |= >>= ||=
.= %= ^= //=
x=

这篇关于perl 运算符“||="是什么?做?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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