2011年10月21日 星期五

$$rarray[1]的結合順序? 其實就是 ${$rarray}[1]啦!

Confusion About Precedence
The expressions involving key lookups might cause some confusion. Do you read $$rarray[1] as
${$rarray[1]} or {$$rarray}[1] or ${$rarray}[1]?
(Pause here to give your eyes time to refocus!)
As it happens, the last one is the correct answer. Perl follows these two simple rules while parsing such
expressions: (1) Key or index lookups are done at the end, and (2) the prefix closest to a variable name
binds most closely. When Perl sees something like $$rarray[1] or $$rhash{"browns"}, it leaves index
lookups ([1] and {"browns"}) to the very end. That leaves $$rarray and $$rhash. It gives preference to
the `$' closest to the variable name. So the precedence works out like this: ${$rarray} and ${$rhash}.
Another way of visualizing the second rule is that the preference is given to the symbols from right to left
(the variable is always to the right of a series of symbols).
Note that we are not really talking about operator precedence, since $, @ , and % are not operators; the
rules above indicate the way an expression is parsed.

沒有留言:

張貼留言