SPUG: Returning array/list? (Was: "while question")

Yitzchak Scott-Thoennes sthoenna at efn.org
Wed Sep 17 03:12:41 CDT 2003


On Tue, Sep 16, 2003 at 09:52:24PM +0000, David Vergin <dvergin at igc.org> wrote:
> Hi Yitzchak (et omnes),
> 
> First, let me to offer a more painstakingly transparent example that
> illustrates the behavior you are pointing to:
> 
> ------------------------------------------------
> #!/usr/bin/perl -w
> use strict;
> 
> sub with_array { my @ary = qw/a b c d e/; @ary }
> sub with_list  { qw/a b c d e/ }
> 
> my $x;
> 
> $x = with_array();
> print $x, "\n";            # 5
> ($x) = with_array();
> print $x, "\n";            # a
> 
> $x = with_list();
> print $x, "\n";            # e
> ($x) = with_list();
> print $x, "\n";            # a
> -------------------------------------------------

Thanks, that's a lot clearer than my hastily typed example.
 
> Then let us just note for the record that there is an alternate
> explanation of what is happening in these examples. 
> 
> There are Perl adepts that argue strongly that what is happening in all
> these cases is that the context of the subroutine call is propagating
> into the subroutine to the point where a value is returned.

I don't think that's an alternate explanation.  That's reality.
If I said anything that conflicts with that, it was an error.
(Though you could describe it in reverse--when a piece of perl guts
needs to know the context, it gets whatever was determined at compile
time.  If it wasn't determined at compile time (i.e. return value for
a block or sub), it looks upward through the calling stack to find
a level where it was determined.)

My point was that changing a scalar assignment to a list assignment
changes not only the boolean value of the assignment (from testing the
scalar that was assigned to testing the count of elements on the right
of the assignment) [which is the part that seemed to be intended by
the poster to whom I responded] but can also have repercussions on the
value assigned because the right side is now in list context instead
of scalar.

[As an implementation detail, perl does sometimes build lists in
scalar context and then throws all but the last element away, but that
doesn't contradict the purist view that on a language-syntactic level,
there is no such thing as a list in scalar context.]



More information about the spug-list mailing list