Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re^2: Variable assignment error checking

by greengaroo (Hermit)
on Dec 15, 2013 at 02:45 UTC ( #1067203=note: print w/ replies, xml ) Need Help??


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

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.


Comment on Re^2: Variable assignment error checking
Re^3: Variable assignment error checking
by Anonymous Monk on Dec 15, 2013 at 06:39 UTC
    $cough ||= cough( $cough );
      Call the function cough($cough) only if $cough is true?

      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

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

        This :) When brain function less (like sleep deprivation), pattern recognition takes place instead of logic processing ... and  $foo = bar($foo) || $foo; is so close to  $foo = $foo || bar($foo); the association to  $foo ||= bar( $foo ); happens, and tired brain fail to realize they're different

        human brains, what can I tell you :)

        hmm, its more like logic processing doesn't check pattern recognition ... argument validation, even for brains

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1067203]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (7)
As of 2015-07-05 05:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (60 votes), past polls