SPUG: Returning array/list? (Was: "while question")
ced at carios2.ca.boeing.com
ced at carios2.ca.boeing.com
Wed Sep 17 00:15:58 CDT 2003
> 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
> -------------------------------------------------
>
> 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. Consistent
> with that, they would argue that with_array() in a scalar context
> returns 5 rather than the @ary array, and that with_list() in a scalar
> context returns 'e' rather than the list ('a', 'b', 'c', 'd', 'e').
>
> I'm not trying to advance the ultimate proof of this (I'm not qualified
> to do so) -- but just to note for the record that there are different
> interpretations by some pretty knowledgable people of the behavior you
> originally aluded to in writing of a sub returning an array vs.
> returning a list.
I recall similar threads. As I remember, the differing semantics are
those of list vs.array. An array is an AV* structure which can have
a scalar context. There's an actual length embedded in the structure.
A list, however,is just values on a stack and there's no comparable
context; its behaviour is more like that of the comma operator.
--
Charles DeRykus
More information about the spug-list
mailing list