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

My program


my @list=qw/malay manab milan anand/;
print @list."\n";
print"@list\n";

my output


4
malay manab milan anand

my querry

when i am changing the 1st print to "print @list" the output is coming as
malaymanabmilananand malay manab milan anand
then why in first instance only no of elements i.e 4 is getting printed in scalar context

Replies are listed 'Best First'.
Re: scalar context confusion
by peterdragon (Beadle) on Feb 28, 2008 at 10:45 UTC
    When you access an array in scalar context, it returns the number of elements. You can make this explicit so
    my @list = qw/malay manab milan anand/; my $count1 = scalar @list; # these are my $count2 = @list; # the same
    By surrounding it in quotes you are asking for the values to be interpolated into a string separated by $" (default space character). See http://perldoc.perl.org/perldata.html or type "perldoc perldata" for more information.
Re: scalar context confusion
by ikegami (Patriarch) on Feb 28, 2008 at 11:14 UTC

    The concatenation operator (.) operates on two scalars, so its operand expressions are evaluated in scalar context. In scalar context @list evaluates to the number of elements in @list.

    An array name in a double-quoted string is not a Perl expression. It's not evaluated normally. Context cannot be determined from the surrounding code, cause it's not in Perl code. An array name in a double-quoted string is replaced with the result join($", @array), and $" defaults to a space.

    Update: Minor tweak to wording for preciseness.
    Update: Removed misleading quotes as per reply.

      In scalar context "@list" evaluates to the number of elements in @list.

      I had to view source to see that your quotation marks were outside your code tags. A casual reader might have thought you meant

      In scalar context "@list" evaluates to the number of elements in @list.

      which would have been incorrect, since in scalar context "@list" (quotes in the expression) evaluates to

      malay manab milan anand
        since in scalar context "@list" (quotes in the expression) evaluates to

        Where do you see scalar context in that expression? In scalar context, a string evaluates to a string, because it's a scalar.