in reply to Friday Code Quiz
There have already been some correct answers in the thread, but here's a full explanation.
|
Stage 1: People who run the code see that $i is set to 1. They then try to come up with an explanation. One common one I've seen is that they claim that the comma operator returns its left operand in scalar context. This gives them the right answer, but for the wrong reason. Stage 2: Other people realise that the comma operator returns its rightoperand in scalar context. This means that $i should be set to 2. This makes sense, but is disproved by the evidence. Stege 3: Finally, we reach for the precedence table and realise that Perl is parsing the expression as ($i=1),2;. It's worth pointing out that -w gives a helpful warning, but also that B::Deparse is a useful tool in investigating weirdness like this: perl -MO=Deparse -e'$i=1,2' |
"The first rule of Perl club is you don't talk about Perl club."
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Friday Code Quiz
by spudzeppelin (Pilgrim) on Sep 21, 2001 at 20:00 UTC | |
|
Re: Re: Friday Code Quiz
by runrig (Abbot) on Sep 21, 2001 at 20:16 UTC |