Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re^2: strange output from conditional operator

by december (Pilgrim)
on May 15, 2009 at 12:23 UTC ( [id://764260]=note: print w/replies, xml ) Need Help??


in reply to Re: strange output from conditional operator
in thread strange output from conditional operator

Yes that works – I knew it does – but I was wondering why doing it this way caused such strange output...

I've given thought to operator precedence, but I figured things wouldn't align up and I'd get a warning or error.

So how would this get parsed? Something like the following I presume:

(defined($var) ? $var) .= ('y' : $var .= 'n') if $el eq 'apple';

Thanks for the explanation, operator precedence makes a lot more sense now. I'm just surprised things lined up well enough not to cause any warnings or syntax errors.

Replies are listed 'Best First'.
Re^3: strange output from conditional operator
by moritz (Cardinal) on May 15, 2009 at 12:31 UTC
    The thread I've linked to above has an example both for how something very similar gets parsed, and how to find out how it's parsed. Did you not read it?
Re^3: strange output from conditional operator
by JadeNB (Chaplain) on May 15, 2009 at 21:53 UTC
    (defined($var) ? $var) .= ('y' : $var .= 'n') if $el eq 'apple';
    No, because there can be no ? without an accompanying :. Thus, the ‘middle’ assignment is parsed as you like (since the tokeniser hasn't seen a : yet, hence knows that it's still inside the conditional), but the ‘last’ one isn't (since the tokeniser believes it's done with the conditional after finding the second occurrence of $var):
    (defined($var) ? $var .= 'y' : $var) .= 'n'

    (Of course, the statement about every ? requiring an accompanying : is a lie, as regular expressions show; but you know what I mean. :-) )

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (6)
As of 2024-04-23 18:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found