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

Hi all,

please have a look at the following code sample:

#!/usr/bin/perl use strict; use warnings; use 5.010; sub get_a_list { return qw(first mid last); } sub get_an_array { my @a = qw(first mid last); return @a; } my $a = get_a_list(); say "\$a: $a"; my $b = get_an_array(); say "\$b: $b";

While the behaviour of case 'b' is clear to me, I can't find an explanation for case 'a'. Can someone give me that explanation or a pointer to documentation explaining it? Why do I get the last element of the list and not the first. And why do I get the element count in case 'b' but not in case 'a'?

Thank you in advance.

Best regards
McA

Replies are listed 'Best First'.
Re: Need explanation for subtle difference
by Athanasius (Archbishop) on May 26, 2015 at 10:07 UTC

    Hello McA,

    Assignment to a scalar variable puts the right-hand-side of the assignment into scalar context — which means the return statement is evaluated in scalar context.

    Now, evaluating a list in scalar context invokes the Comma Operator, which “evaluates its left argument, throws that value away, then evaluates its right argument and returns that value.” And since the comma operator is left associative, the final (i.e., right-most) comma is evaluated last, so the last element of the list is returned.

    Evaluating an array in scalar context returns the number of elements in the array:

    If you evaluate an array in scalar context, it returns the length of the array. (Note that this is not true of lists, which return the last value, like the C comma operator, nor of built-in functions, which return whatever they feel like returning.)

    perldata#Scalar-values.

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

      ... evaluates its left argument, throws that value away, then evaluates its right argument and returns that value.

      McA: As e.g.:

      c:\@Work\Perl\monks>perl -wMstrict -le "sub foo { print 'hi from foo()'; } my $bar = 42; ;; my $agent = (foo(), $bar, 666, 99); print qq{agent $agent}; " Useless use of private variable in void context at -e line 1. Useless use of a constant (666) in void context at -e line 1. hi from foo() agent 99
      Note the  Useless use of ... warnings for the variable and constant as these are evaluated and thrown away. Perl understands that the evaluation of a subroutine may have (useful) side-effects.


      Give a man a fish:  <%-(-(-(-<

Re: Need explanatiin for subtle difference
by Anonymous Monk on May 26, 2015 at 09:55 UTC

      I'm clapping... ;-)

      Thank you for this valuable pointer. I haven't found it.

      Regards
      McA

Re: Need explanatiin for subtle difference
by BillKSmith (Monsignor) on May 26, 2015 at 20:05 UTC
    A good explanation is somewhat more subtle than other monks have indicated. First, we must realize that return inherits its context from the use of the function call. In both of your cases, it is scalar context. The difference between an actual array and a list in scalar context is documented in List Value Constructors. In your example, we should expect the array size from an actual array and the last element from a list. This is what you report.
    Bill