Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Re^2: strange output from conditional operator

by december (Pilgrim)
on May 15, 2009 at 12:23 UTC ( #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?

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2022-01-28 10:55 GMT
Find Nodes?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:

    Results (73 votes). Check out past polls.