On Mon, Mar 07, 2005 at 08:11:13AM -0800, Palit, Nilanjan wrote:
> I'm forwarding the responses I gave to individuals to the whole group
> (sorry for the duplicates to the individuals):
>
>
> =====
> I don't usually expect the correct answer on that question - I usually
> feel good about the candidate if they see the problem on paper & put
> parens in the right places:
>
> $> perl -e '$x=1; $y=($x++)+1; print "x=$x, y=$y\n"'
>
> This is because '++' has a higher precedence than '+'.
I'd pick a nit on this answer.
Precedence has nothing to do with it. The issue is how the
tokenizer breaks the input sequence '+++' into operator tokens.
So, tokenizing determines whether:
$x+++1
is:
$x ++ + 1
or:
$x + ++ 1
Of course, the tokenizer chooses the first - it gobbles the
longest legal string possible after the first +.
After that is determined, the grammar determines that ++ needs
to be combined with a term (which cannot be the + operator),
while + needs to combine with two terms (which cannot include
the ++ operator), so ++ is of necessity bound first. But it
is only after the tokenizer has decided what operators were
found in the text and the grammar has found multiple ways of
combining a sequence of terms and operators that precedence
comes onto play.
There would only be ambiguity that would get resolved by
precedence when the unary operator was outside the term op
term combination. I.e. something like:
++$x+1
-> ++($x+1) [wrong]
or (++$x)+1 [right]
Depending upon the level of person you were looking to hire,
this answer might be too pedantic and indicate a troublmaker
that you'd prefer to avoid. :-)
--
_______________________________________________
Boston-pm mailing list
[email protected]
http://mail.pm.org/mailman/listinfo/boston-pm