Is there such a thing as a list in scalar context?

前端 未结 6 1635
孤独总比滥情好
孤独总比滥情好 2020-12-11 16:52
my $mind = ( \'a\', \'little\', \'confused\' );

And it\'s because perldoc perlfaq4 explains the line above as follows (emphasis added):

6条回答
  •  温柔的废话
    2020-12-11 17:23

    Just ask Perl!

    >perl -MO=Concise,-exec -e"my $s = ($x, $y, $z);"
    1  <0> enter
    2  <;> nextstate(main 1 -e:1) v:{
    3  <0> pushmark v
    4  <#> gvsv[*x] s
    5  <#> gvsv[*y] s
    6  <#> gvsv[*z] s
    7  <@> list sKP                      <--- list in (s)calar context.
    8  <0> padsv[$s:1,2] sRM*/LVINTRO
    9  <2> sassign vKS/2
    a  <@> leave[1 ref] vKP/REFC
    

    So yes, one can have a list in scalar context.

    One cannot return a list in scalar context. (Well, it is possible from XS, but the program will crash, probably with a "Bizarre copy" error.)


    It can't be any other way. If there was a distinct list op and comma op, it would be impossible to compile the following:

    sub f { "a", "b", "c" }
    

    Would that result in a list op or a comma op? In reality, there is no such distinction, so yes, it results in that op.

提交回复
热议问题