in reply to Return Value of List-Producing Elements in Scalar Context

Normally, $a = @array; will store the number of elements in the list in $a. That is not always the case, because of wantarray. For example:

$scalar = localtime(); print($scalar, "\n"); # "Mon Aug 23 11:31:16 2004" @array = localtime(); $scalar = @array; print($scalar, ':', join(',', @array), "\n"); # 9:16,31,11,23,7,104,1,235,1 @array = scalar(localtime()); $scalar = @array; print($scalar, ':', join(',', @array), "\n"); # 1:Mon Aug 23 11:34:30 2004

Update: Fixed error pointed out by davorg

Replies are listed 'Best First'.
Re^2: Return Value of List-Producing Elements in Scalar Context
by davorg (Chancellor) on Aug 23, 2004 at 15:38 UTC
    Normally, $a = ( ...list... ); will store the number of elements in the list in $a.

    Did you test that before you wrote it?

    $ perl -le '$a = (3, 4, 5); print $a' 5

    I think you're confusing lists and arrays.

    --
    <http://www.dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

      I did test it, but the last element of the list was the number of elements in the list... Fixed my original post.