Perl: precedence (Leftward list operator) -
from programming perl pg 90, says:
@ary = (1, 3, sort 4, 2); print @ary; the commas on right of sort evaluated before sort commas on left evaluated after. ... list operators tend gobble .. , act simple term"
- does assignment result in sort being processed or happen when
@aryexpanded print? - what mean "comma" stuff?? understanding in assignment statement, comma has lower priority list operator therefore sort runs first , gobbles it's arguments
(4 , 2).. how heck comma being evaluated @ all?? statemnent becomes (1, 3, 2, 4) list assigned.. comma acting list separator , not operator!! in fact on pg:108 says: not confuse scalar context use of comma list context use.. what leftward , rightward list operator?
print @aryrightward list operator?? has low priority?print($foo, exit);
here, how precedence evaluated? print list operator looks function should run first! has 2 arguments $foo , exit.. why exit not treated string??? after priority-wise print(the list operator) has higher priority??
print $foo, exit; here, have print , , operators list operator has higher precedence.. so.. exit should treated string - why not??
print ($foo & 255) + 1, "\n"; here since it's list operator prints $foo & 255 shouldn't similar happen above mentioned exit stuff..
when in doubt how perl parsing construct, can run code through b::deparse module, generate perl source code compiled internal representation. first example:
$ perl -mo=deparse,-p -e '@ary = (1, 3, sort 4, 2); print @ary;' (@ary = (1, 3, sort(4, 2))); print(@ary); -e syntax ok so can see, sort takes 2 arguments right.
as far execution order goes, can find out b::concise module (i've added comments):
$ perl -mo=concise,-exec -e '@ary = (1, 3, sort 4, 2); print @ary;' 1 <0> enter 2 <;> nextstate(main 1 -e:1) v:{ 3 <0> pushmark s # start of list 4 <$> const[iv 1] s # 1 added list 5 <$> const[iv 3] s # 3 added list 6 <0> pushmark s # start of sort's argument list 7 <$> const[iv 4] s # 4 added sort's argument list 8 <$> const[iv 2] s # 2 added sort's argument list 9 <@> sort lk # sort run, , returns list outer list <0> pushmark s b <#> gv[*ary] s c <1> rv2av[t2] lkrm*/1 d <2> aassign[t3] vks/common # list assigned array e <;> nextstate(main 1 -e:1) v:{ f <0> pushmark s # start of print's argument list g <#> gv[*ary] s # array loaded print's argument list h <1> rv2av[t5] lk/1 <@> print vk # print outputs it's argument list j <@> leave[1 ref] vkp/refc -e syntax ok for second example:
$ perl -mo=deparse,-p -e 'print $foo, exit;' print($foo, exit); -e syntax ok $ perl -mo=concise,-exec -e 'print $foo, exit;' 1 <0> enter 2 <;> nextstate(main 1 -e:1) v:{ 3 <0> pushmark s 4 <#> gvsv[*foo] s # add $foo argument list 5 <0> exit s # call `exit` , add return value list 6 <@> print vk # print list, never here 7 <@> leave[1 ref] vkp/refc -e syntax ok so can see, exit builtin run while trying assemble argument list print. since exit causes program quit, print command never gets run.
and last one:
$ perl -mo=deparse,-p -e 'print ($foo & 255) + 1, "\n";' ((print(($foo & 255)) + 1), '???'); # '???' means optimized away -e syntax ok $ perl -mo=concise,-exec -e 'print ($foo & 255) + 1, "\n";' 1 <0> enter 2 <;> nextstate(main 1 -e:1) v:{ 3 <0> pushmark v 4 <0> pushmark s 5 <#> gvsv[*foo] s 6 <$> const[iv 255] s 7 <2> bit_and[t2] sk 8 <@> print sk 9 <$> const[iv 1] s <2> add[t3] vk/2 b <@> list vk c <@> leave[1 ref] vkp/refc -e syntax ok
Comments
Post a Comment