Evalutating a list in scalar context returns the last element in the list, not its length:

print scalar(1, 2, 3, 4, 3, 2, 1);

will print "1".
Nope, evaluating a list in scalar context returns the length of the list.
(*) Apologies, I was getting lists and arrays confused there. I was talking about arrays, not lists. That should have read:
"Evaluating an array in scalar context returns the length of the array."
However, I hope my following explaination of why scalar(list) does not return the length of the list is still useful.


The documentation for scalar says:
Because scalar is a unary operator, if you accidentally use a parenthesized list for the EXPR, this behaves as a scalar comma expression, evaluating all but the last element in void context and returning the final element evaluated in scalar context. This is seldom what you want.
So what is actually happening here is (1, 2, 3, 4, 3, 2, 1) evaluates to 1, the last element in the list.
And scalar 1 means evaluate 1 in scalar context, which is *drum roll* 1 ! :)

use warnings; would have detected this.

Example code:
use strict; use warnings; print "A: ", scalar(10, 20, 30, 40, 50, 60, 70), "\n"; # = scalar +(70) = scalar 70 = 70 print "B: ", scalar(70), "\n"; # + = scalar 70 = 70 print "C: ", scalar 70, "\n"; # + = 70 my $x = scalar(10, 20, 30, 40, 50, 60, 70); # = scalar +(70) = scalar 70 = 70 print "D: ", $x, "\n"; $x = (10, 20, 30, 40, 50, 60, 70); # = +(70) = 70 print "E: ", $x, "\n"; my @a = (10, 20, 30, 40, 50, 60, 70); print "F: ", scalar @a, "\n"; # = 7, the + length of @a
Output:
Useless use of a constant (10) in void context at D:\stuart\play\temp\ +pp.pl line 4. Useless use of a constant (20) in void context at D:\stuart\play\temp\ +pp.pl line 4. Useless use of a constant (30) in void context at D:\stuart\play\temp\ +pp.pl line 4. Useless use of a constant (40) in void context at D:\stuart\play\temp\ +pp.pl line 4. Useless use of a constant (50) in void context at D:\stuart\play\temp\ +pp.pl line 4. Useless use of a constant (60) in void context at D:\stuart\play\temp\ +pp.pl line 4. Useless use of a constant (10) in void context at D:\stuart\play\temp\ +pp.pl line 10. Useless use of a constant (20) in void context at D:\stuart\play\temp\ +pp.pl line 10. Useless use of a constant (30) in void context at D:\stuart\play\temp\ +pp.pl line 10. Useless use of a constant (40) in void context at D:\stuart\play\temp\ +pp.pl line 10. Useless use of a constant (50) in void context at D:\stuart\play\temp\ +pp.pl line 10. Useless use of a constant (60) in void context at D:\stuart\play\temp\ +pp.pl line 10. Useless use of a constant (10) in void context at D:\stuart\play\temp\ +pp.pl line 13. Useless use of a constant (20) in void context at D:\stuart\play\temp\ +pp.pl line 13. Useless use of a constant (30) in void context at D:\stuart\play\temp\ +pp.pl line 13. Useless use of a constant (40) in void context at D:\stuart\play\temp\ +pp.pl line 13. Useless use of a constant (50) in void context at D:\stuart\play\temp\ +pp.pl line 13. Useless use of a constant (60) in void context at D:\stuart\play\temp\ +pp.pl line 13. A: 70 B: 70 C: 70 D: 70 E: 70 F: 7


UPDATE: (a few minutes later) added comments to code, and 1 extra example

UPDATE #2: As choroba pointed out in the following post, I was getting lists and arrays confused. See (*) above.

In reply to Re: Get the length of a list. by zork42
in thread Get the length of a list. by nbtrap

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.