What kind of syntactic sugar is available in Perl to reduce code for l/rvalue operators vs. if statements?

后端 未结 5 1471
时光说笑
时光说笑 2021-02-13 04:26

There\'s a bunch out there, as Perl is a pretty sugary language, but the most used statements in any language is the combination of if statements and setting values. I think I\

5条回答
  •  感动是毒
    2021-02-13 04:41

    $r = $r < $s ? $r : $s;:

        $r = $s if $r > $s;
    

    or

        use List::Util qw( min );
    
        $r = min($r, $s);
    

    or:

        sub min_inplace {
           my $min_ref = \shift;
           for (@_) { $$min_ref = $_ if $$min_ref > $_; }
        }
    
        min_inplace($r, $s);
    

    $r = $s if (defined $s);:

        $r = $s // $r;
    

    $r = $t; $r = $s if (defined $s);:

        $r = $s // $t;
    

    $r = !$s ? $s : $t;:

        $r = $s && $t;
    

提交回复
热议问题