in reply to Variable assignment error checking

This is assuming that func() doesn't have any side-effects against its parameters.

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

...unless I'm misunderstanding the question. ;)


Dave

Replies are listed 'Best First'.
Re^2: Variable assignment error checking
by greengaroo (Hermit) on Dec 15, 2013 at 02:45 UTC

    davido has the answer. That's the way to do it. If func returns 0 or undef, $var will be assigned to $var, therefore keeping its original value. I use this technique all the time.

    A for will get you from A to Z; a while will get you everywhere.
      $cough ||= cough( $cough );

        The OP requested to assign the return value of func($var) if that return value is true. Otherwise, to hold the original value of $var.

        You coughed up this: If $var is true, keep it, otherwise store a return value from func($var).

        To put it another way: Specification: if the function returns a true value, store it. Otherwise keep the original value.

        Your solution: If the original value is true keep it, otherwise call the function and keep its return value instead.

        Inverted logic. But you're not alone; several people upvoted your post, so it must be an easy mistake to make.


        Dave

        Call the function cough($cough) only if $cough is true?