in reply to letters++, bug or trick?

It's all perfectly logical really. The first thing to notice is that the initialisation of $c makes no difference to the outcome:

perl -e "$c=qq(z);$d=qq(d); $c = $d++; $d = $c++; print $c,$d;" ed perl -e "$d=qq(d); $c = $d++; $d = $c++; print $c,$d;" ed

So,

  1. $d is set to 'd'
  2. $c is set to $d ('d')
  3. $d is incremented ('e')
  4. $d is set to $c ('d')
  5. $c is incremented ('e')
  6. 'ed' is printed.

In the end, the whole thing reduces to:

perl -e "$d=qq(d); $c = $d; $d = $c++; print $c,$d;" ed

or even just

perl -e "$c = $d = qq(d); $c++; print $c,$d;" ed

Which looks a lot less mysterious :)


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
"I'd rather go naked than blow up my ass"

Replies are listed 'Best First'.
Re^2: letters++, bug or trick?
by xiaoyafeng (Deacon) on Jan 30, 2010 at 07:51 UTC
    Ah! too much ++ confused me! the last statment is really a concise answer! thanks!




    I am trying to improve my English skills, if you see a mistake please feel free to reply or /msg me a correction