Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: ternary conditional help

by tybalt89 (Monsignor)
on Nov 28, 2016 at 20:34 UTC ( [id://1176732]=note: print w/replies, xml ) Need Help??


in reply to ternary conditional help

Because of precedence

(1 ? $a = 2 : $b) = 'x';

You didn't read your "perldoc perlop" ...

Replies are listed 'Best First'.
Re^2: ternary conditional help
by opaltoot (Novice) on Nov 28, 2016 at 20:43 UTC
    ok thanks, but how is it that  ($a = 2) = 'x' makes $a = 'x' ?

      Hi opaltoot,

      See Assignment Operators:

      ... the scalar assignment operator produces a valid lvalue. Modifying an assignment is equivalent to doing the assignment and then modifying the variable that was assigned to. This is useful for modifying a copy of something, like this:

      ($tmp = $global) =~ tr/13579/24680/;

      ... Likewise,

      ($x += 2) *= 3;

      is equivalent to

      $x += 2; $x *= 3;

      Hope this helps,
      -- Hauke D

        thanks, yes that helps

        This is because the entire expression is evaluated before being passed as arguments to print. print doesn't get called iteratively for each argument. It gets its arguments, and then iterates.

        So that means $a = 2 is evaluated, then ' ', then $a = 3, and then the values of $a, ' ', and $a are passed to print. By the time the expression has been evaluated, $a contains 3 no matter how many times it appears in @_.


        Dave

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (2)
As of 2024-04-26 06:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found