in reply to Re^4: Variable assignment error checking
in thread Variable assignment error checking

no, its shorthand of Re: Variable assignment error checking
  • Comment on Re^5: Variable assignment error checking

Replies are listed 'Best First'.
Re^6: Variable assignment error checking
by hdb (Monsignor) on Dec 15, 2013 at 10:20 UTC

    It is not! In this expression:

    $var = func($var) || $var;

    the return value of func($var) will be assigned to $var if func($var) is true, otherwise the value of $var. In this expression

    $var ||= func( $var );

    the return value of func($var) will be assigned to $var if $var is false.

    Completely different logic!

    The expression $var ||= func( $var ); is shorthand for $var = $var || func( $var );. Can you spot the difference?

      $var = $var || func( $var ); $var = func( $var ) || $var;
      They're certainly logically equivalent if we disregard the short-circuiting nature of operators. (Though the mathematics branch of logic generally deals with no other return values than true and false...)

        As you say, only if we only worry about true or false...

        my $var = 5; sub func { 10 }; print $var || func($var), "\n"; print func($var) || $var, "\n";