http://www.perlmonks.org?node_id=719133


in reply to Re: If you believe in Lists in Scalar Context, Clap your Hands
in thread If you believe in Lists in Scalar Context, Clap your Hands

OK. Lists cannot be lvalues, though you may have a list of lvalues...

Suppose I refine the points paying question: as an rvalue how does one describe the difference between a list and an array ?

  • Comment on Re^2: If you believe in Lists in Scalar Context, Clap your Hands

Replies are listed 'Best First'.
Re^3: If you believe in Lists in Scalar Context, Clap your Hands
by ikegami (Patriarch) on Oct 24, 2008 at 02:46 UTC

    Lists cannot be lvalues, though you may have a list of lvalues...

    Say again? An lvalue list returns a list of lvalues and enforces list context, just like an rvalue list returns a list of rvalues in list context.

    In both of the following snippets, you'll see an list as an lvalue ("M" flag).

    >perl -MO=Concise -e"($a,$b)=(4,5)" a <@> leave[1 ref] vKP/REFC ->(end) 1 <0> enter ->2 2 <;> nextstate(main 1 -e:1) v ->3 9 <2> aassign[t3] vKS ->a - <1> ex-list lKP ->6 <-- RHS list 3 <0> pushmark s ->4 4 <$> const[IV 4] s ->5 5 <$> const[IV 5] s ->6 - <1> ex-list lKPRM* ->9 <-- LHS list w/ "M" 6 <0> pushmark sRM* ->7 - <1> ex-rv2sv sKRM*/1 ->8 7 <#> gvsv[*a] s ->8 - <1> ex-rv2sv sKRM*/1 ->- 8 <#> gvsv[*b] s ->9 -e syntax OK
    >perl -MO=Concise -e"1 for ($a,$b)" d <@> leave[1 ref] vKP/REFC ->(end) 1 <0> enter ->2 2 <;> nextstate(main 1 -e:1) v ->3 3 <;> nextstate(main 1 -e:1) v ->4 c <2> leaveloop vK/2 ->d 8 <{> enteriter(next->9 last->c redo->9) lK/8 ->a - <0> ex-pushmark s ->4 - <1> ex-list lKPM ->7 <-- lvalue list 4 <0> pushmark sM ->5 - <1> ex-rv2sv sKM/1 ->6 5 <#> gvsv[*a] s ->6 - <1> ex-rv2sv sKM/1 ->- 6 <#> gvsv[*b] s ->7 7 <#> gv[*_] s ->8 - <1> null vK/1 ->c b <|> and(other->9) vK/1 ->c a <0> iter s ->b - <@> lineseq vK ->- - <0> ex-const v ->9 9 <0> unstack v ->a -e syntax OK
Re^3: If you believe in Lists in Scalar Context, Clap your Hands
by moritz (Cardinal) on Oct 23, 2008 at 19:34 UTC
    An array is everything that could be used as an lvalue (perhaps with the exception of :lvalue subs, haven't tried those), independently of you actually use it as an lvalue.

    That definition so far helped in all real-world cases that I've look at.

      perhaps with the exception of :lvalue subs, haven't tried those

      lvalue subs are called in scalar context, so they can't return a list.

      use strict; use warnings; my ($i,$j); sub f :lvalue { ($i,$j) } f() = (4,5); print("$i,$j\n");
      Useless use of a constant in void context at script.pl line 5. Use of uninitialized value in concatenation (.) or string at script.pl + line 6. ,5
        Wrong. Don't forget that plain = is the scalar assignment operator, unless there is a hash or array on the left. ()= is the list assignment operator:
        $ perl use strict; use warnings; my ($i,$j); sub f :lvalue { ($i,$j) } (f()) = (4,5); print("$i,$j\n"); __END__ 4,5
        However, I would be very leery of using a relatively new feature like that that has probably not been used much in that way.