http://qs1969.pair.com?node_id=743238

sandy1028 has asked for the wisdom of the Perl Monks concerning the following question:

Difference between my($Files) = @_; and my $Files = @_

Replies are listed 'Best First'.
Re: Difference between my ($foo) & my $foo
by GrandFather (Saint) on Feb 12, 2009 at 07:50 UTC
    my($Files) = @_;

    List context.

    my $Files = @_;

    Scalar context.


    True laziness is hard work
Re: Difference between my ($foo) & my $foo
by ELISHEVA (Prior) on Feb 12, 2009 at 08:52 UTC
Re: Difference between my ($foo) & my $foo
by Anonymous Monk on Feb 12, 2009 at 07:50 UTC
    $ perl -MO=Deparse,-p - my($Files) = @_; my $FilesR = @_; ^D (my($Files) = @_); (my $FilesR = @_); - syntax OK
    Its list context versus scalar context, example
    @_ = 0 .. 3; my( $one, $two, $three ) = @_; print "one($one)two($two)three($three)\n"; ( $one, $two, $three ) = scalar @_; print "one($one)two($two)three($three)\n"; __END__ one(0)two(1)three(2) one(4)two()three()
Re: Difference between my ($foo) & my $foo
by planetscape (Chancellor) on Feb 14, 2009 at 05:03 UTC
Re: Difference between my ($foo) & my $foo
by repellent (Priest) on Feb 12, 2009 at 19:54 UTC
    With:
    my (undef, $a, undef, $b, @rest, $c) = (1, 2, 3, 4, 5, 6, 7, 8);

    Now:
    • @rest will be greedily assigned: @rest = (5, 6, 7, 8)
    • the other variables are respectively assigned: ($a, $b, $c) = (2, 4, undef)

    Then:
    my $count = @rest; # 4 - because there are four items (scalar contex +t) my ($first) = @rest; # 5 - because first item is 5 (list context)

    But:
    my $last = (11, 22, 33, 999); # 999 - because assigning LIST to scalar + only picks last item