[EMAIL PROTECTED] wrote:
Palit, Nilanjan said:
~~$> perl -e '$x=1; $y=$x+++1; print "x=$x, y=$y\n"'
Bummer. I just got a ding on your interview.
How do you parse $x+++1 ?
$y=$x+++1
is
$x+1
$y=$x
$x++
Kinda. The thing is that $x++ ("unitary postfix notation"?)
can't really be broken out that way.
It really works more like this:
$x = 1;
$y = $x + 1;
$x = $x + 1;
The $x++ resolves to $x in the context of the expression in
which it's being used, and *after* that bit of processing,
$x gets incremented.
$x = 1;
$y = $x++;
print "\$x = $x; \$y = $y\n";
$x = 2; $y = 1
(And in contrast, the following may be of interest, where
the $x incrementing takes place *before* the rest of the
expression is processed:)
$x = 1;
$y = ++$x;
print "\$x = $x; \$y = $y\n";
$x = 2; $y = 2
But all of that is why I try to avoid using ++ inline in
an expression. I know how it works and remember it easily,
but it seems a waste of energy to think about, and it's too
easy to make a one-off error (did we count the zero or one?)
and it really rarely actually improves code or readability
in the slightest.
At best, I may use a ++ inside an expression for a debug
print line like: print ...stuff... unless ($counter++ % 50).
to print something every 50th step. But, even there - do
I really want $counter++ or would ++$counter make more sense?
(and, of course, did I start counting at zero or one?)
--d
_______________________________________________
Boston-pm mailing list
Boston-pm@mail.pm.org
http://mail.pm.org/mailman/listinfo/boston-pm