what does perl operator “||=” do?

后端 未结 4 1330
逝去的感伤
逝去的感伤 2021-01-29 12:32

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          


        
4条回答
  •  旧时难觅i
    2021-01-29 13:11

    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');
    }
    

    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.

    Other assignment operators supported by perl:

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

提交回复
热议问题