On 11/1/11 Tue Nov 1, 2011 12:02 PM, "rent0n" <ren...@gmx.com> scribbled:
> Hi all, > > Can somebody please explain what is the difference between: > > sub subroutine { > my $var = @_; > ... > return 1; > } > > and: > > sub subroutine { > my ($var) = @_; > ... > return 1; > } > > I think it's something related to the context but I'm not completely > sure. What I know is that to have the behaviour I want I have to declare > the variable like in the second example (with parentheses). > This confuses me a bit because it's not the way I usually declare > variables in my main program (that is, without parentheses). You are correct: it has to do with context. In the statement my $var = @_; the context of the assignment is scalar. Therefore, under the rules of Perl, an array evaluated in scalar context returns the number of elements, which is then assigned to $var. In contrast, in the statement my( $var ) = @_; the context of the assignment is array (or list), therefore elements on the left-hand-side get assigned to their equivalent positions on the right-hand-side, so $var is assigned the first element of the array @_; -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/