On Wed, Aug 02, 2000 at 08:10:01PM +0100, Alan Burlison wrote:
> "Randal L. Schwartz" wrote:
> 
> > Graham> sub def { my @a = (9,8,7); return @a; }
> > 
> > That's not returning the array.  That's returning a copy of the contents
> > of @a in a list context, and the number of elements of @a in a scalar
> > context, using the "@a" operator.  You still haven't "returned the array".
> 
> So then in fact it is impossible to return an array - yes?  You can
> either return a single scalar or a list.  The closest you can come to
> returning an array as an entity is to return a reference to it - \@a.
> 
> Have I got that straight?

Yes that is right. It is impossible to return an array, but just check
the PODs on CPAN and see how many document they return an array or a list
and compare that to the code they have in the module.

The problem is the syntax of what is being returned is separated from it's
context, which is what causes the confusion. Even the core documention cannot
get it right, try running this on all pm's in the distribution

 find ./lib -name \*pm | xargs perl -ne 'print if /return.*array/i' 

See also perlop.pod and the section for the range operator.

So as you can see there is plenty of confusion, so either

a) There needs to be some definition to the words "sub foor returns an array/list"
b) All docs should write something like

    sub foo returns a list of foo,bar,baz but in a scalar context returns baz

    sub bar returns a list of foo,bar,baz but in a scalar context returns 3

The point being, there is confusion and it needs to be addressed. But I, like others,
do not want to see the list/array distinction disappear.

Graham.

Reply via email to