Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Re: Why does the first $c evaluate to the incremented value ... (bug)

by oiskuu (Hermit)
on Mar 05, 2014 at 00:23 UTC ( #1077021=note: print w/replies, xml ) Need Help??

in reply to Why does the first $c evaluate to the incremented value in [$c, $c += $_] ?

In C, a comma operator is a "sequence point", meaning a statement such as y = (++x, ++x, ++x); is perfectly valid. (But (++x + ++x); is undefined behavior.) Comma in declaration list, initializers, parameter list, ..., is not an operator but a punctuator.

perlop says a comma in list context is "just the list argument separator". This is not a case with operator precedence. Indeed, it's a nasty bug!

perl -le '$c = 0; print @$_ for map { [$c, $c += $_] } (2,6,5,7);' perl -le '$c = 0; print @$_ for map { ["$c", $c += $_] } (2,6,5,7);'

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1077021]
[shmem]: ah, geany uses its own idea of tags. Ah well...
[Lady_Aleena]: My perl-fu is bad.
[Lady_Aleena]: I"m getting: Not a SCALAR reference at ../files/lib/Base/ line 114. which is weird because it should be a string.

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (9)
As of 2017-04-27 12:38 GMT
Find Nodes?
    Voting Booth?
    I'm a fool:

    Results (506 votes). Check out past polls.