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

Hello All:

Working my way through Learning Perl (the llama book), specifically the section Scalar and List Context.

Please consider the following:

my $x; my @array; @array = qw( a b c); ($x) = qw( a b c); print $x . "\n"; $x = qw( a b c); print $x . "\n";
Which produces:

C:\dev\learningPerl>perl ex3c.pl a c
In this assignment: ($x) = qw( a b c);

In my mind we have list to list. The list on the left only has one slot so the other two values are lost.

Can some please explain what is going on here:

$x = qw( a b c);

and why does "c" get assigned to $x

Many thanks for your kind assistance.

Best

KD

Replies are listed 'Best First'.
Re: scalar vs list context
by roboticus (Chancellor) on Feb 04, 2012 at 01:34 UTC

    kevind0718:

    From perldata under the heading 'List value constructors':

    In a context not requiring a list value, the value of what appears to be a list literal is simply the value of the final element, as with the C comma operator. For example,

    @foo = ('cc', '-E', $bar);

    assigns the entire list value to array @foo, but

    $foo = ('cc', '-E', $bar);

    assigns the value of variable $bar to the scalar variable $foo.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

Re: scalar vs list context
by JavaFan (Canon) on Feb 04, 2012 at 08:41 UTC
    qw(a b c) is a shorthand for ("a", "b", "c"). Note that this is not a list by itself; it's only a list when it's in list context. When assigning to a scalar, it's in scalar context, and then we have the comma operator in scalar context. The comma operator in scalar context evaluates its left hand side, discards it, and then returns the value of its right hand side.

    If you run $x = qw(a b c); with warnings enabled, it will complain about constants a and b being used in void context.

Re: scalar vs list context
by Anonymous Monk on Feb 04, 2012 at 01:51 UTC
Re: scalar vs list context
by tobyink (Canon) on Feb 04, 2012 at 10:04 UTC

    Firstly, you've got to distinguish between lists and arrays. An array is basically a list identified an @-sign.

    use strict; use warnings; # Assigning a list to an array. @a is now ('a', 'b', 'c'). my @a = ('a', 'b', 'c'); # Assigning an array to a list. $f is 'a'. my ($f) = @a; # Assigning a list to a list. $f2 is also 'a'. my ($f2) = ('a', 'b', 'c'); # Because we're assigning in scalar context, # the comma operator is used with its scalar # meaning. In scalar context comma means # "evaluate the left side, discard that, then # evaluate the right side". So $l is 'c'. # # "use warnings" will give us two errors here # because 'a' and 'b' are evaluated but never # used. my $l = ('a', 'b', 'c'); # Assigning an array to a scalar. $n is 3. my $n = @a; use Data::Dumper; print Dumper { '@a' => \@a, '$f' => $f, '$f2' => $f2, '$l' => $l, '$n' => $n, }; __END__ Output is: $VAR1 = { '@a' => [ 'a', 'b', 'c' ], '$n' => 3, '$l' => 'c', '$f2' => 'a', '$f' => 'a' };
Re: scalar vs list context
by rustic (Monk) on Feb 04, 2012 at 11:51 UTC
    You also have it under the "Quote-Like Operators" in perlop and look for qw/STRING/