It seems so obvious now. Should possibly have just tested it myself before asking...
Thank you all for the explanations! On 29 May 2014 21:36, "Jim Gibson" <jimsgib...@gmail.com> wrote: > > On May 29, 2014, at 1:20 PM, James Kerwin wrote: > > > Hello all, long time lurker, first time requester... > > > > I have a Perl exam tomorrow and came across a question that I just > cannot find an answer to (past paper, this isn't cheating or homework etc.). > > > > Explain the difference between: > > > > ($test)=(@test); > > > > And > > > > $test=@test; > > > > If anybody could shed any light on this I'd be very grateful. > > The difference is the "context" of the assignment: "scalar" or "list", and > how @test (or (@test)) is evaluated in that context. > > In the first statement ($test) = (@test), the parentheses around $test in > the left-hand side (LHS) of the assignment places the evaluation of the > right-hand side (RHS) in list context. In list context, with two lists on > either side of the assignment operator (=), assignment is made from each > element on the RHS to the corresponding element on the LHS. Therefore, the > one and only element on the LHS ($test) gets assigned the value of the > first element of the RHS, and $test ends up with the value of $test[0]. > > In the second statement, the assignment is done in scalar context, and the > RHS is evaluated in scalar context. A list evaluated in scalar context > returns the number of elements in the list, and $test is assigned the value > ($#test+1). > > Note that the parentheses around @test in the first statement are > irrelevant. The context is list with or without them. > > Try it yourself: > > % perl -e '@t=qw(1 2 3);$t=@t;print qq($t\n);' > 3 > % perl -e '@t=qw(1 2 3);($t)=@t;print qq($t\n);' > 1 > % perl -e '@t=qw(1 2 3);($t)=(@t);print qq($t\n);' > 1 > > > >