Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Returning lists vs arrays

by rir (Vicar)
on Jan 20, 2003 at 16:29 UTC ( #228383=perlquestion: print w/replies, xml ) Need Help??

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

In my code I will usually return arrayrefs. Occasionally I will return an array. I can't remember ever returning a list. This seems sensible and also normal practice, except for core perl funcs that are advertised to return lists.

Given an array what is the best way to return it as a list?

Other than punning existing functions are there good reasons to return a list instead of an array?

Replies are listed 'Best First'.
Re: Returning lists vs arrays
by broquaint (Abbot) on Jan 20, 2003 at 16:38 UTC
    Given an array what is the best way to return it as a list?
    When you return an array from a function (assuming list context) you are in fact returning a list. See tilly's definitive node Arrays are not lists for a good explanation on arrays and how they relate to lists (and vice versa).
    HTH

    _________
    broquaint

Re: Returning lists vs arrays
by BrowserUk (Patriarch) on Jan 20, 2003 at 17:01 UTC

    My thought on this is that, if the dictates of your application lend or require you to return a list, rather than a ref, try and return the list without building an array first, if it's sensible to do so.

    As a example, rather than

    sub get_3d_xyz { $self = shift; my @xyz = ( $self->get_x() , $self->get_y() , $self->get_z() ); return @xyz; }

    save allocating and building the intermediate lexical array and do

    sub get_3d_xyz { $self = shift; return ( $self->get_x(), $self->get_y(), $self->get_z() ); }

    Or even,

    use constant SELF => 0; sub get_3d_xyz { ( $_[SELF]->get_x() , $_[SELF]->get_y() , $_[SELF]->get_z() ); }

    Each of those steps shaves a few micro/milliseconds of the operation. Not a lot as and of itself, but at the heart of a 3d-graphic class, significant enough.

    Updated: Corrected a couple of typos. Thanks ihb.


    Examine what is said, not who speaks.

    The 7th Rule of perl club is -- pearl clubs are easily damaged. Use a diamond club instead.

Re: Returning lists vs arrays
by hardburn (Abbot) on Jan 20, 2003 at 16:42 UTC

    --Reposting my answer from the duplicated node--

    I think you're confused by the distinction between arrays and lists. Lists are a bunch of values, while arrays are variables that hold values. See perlfaq4.

    Outside your subroutine, the distinction doesn't matter. It will all look like arrays:

    sub return_array { my @a = (0, 1, 2, 3); return @a; } sub return_list { return (0, 1, 2, 3); } my @array1 = return_array(); my @array2 = return_list();
      Outside your subroutine, the distinction doesn't matter.

      It does, depending on context. See Context aware functions - best practices? for an example of this.

      It will all look like arrays

      (Assuming list context.) Make that "lists" instead. A bunch of values are returned, and as you say yourself, that's a list.

      If you really want to return a list but the whole list is stored in an array you can do   return @stuff[0..$#stuff];

      ihb
Re: Returning lists vs arrays
by rir (Vicar) on Jan 20, 2003 at 17:03 UTC
    This illustrates the differences that I am referring to.
    !/usr/bin/perl use strict; use warnings; sub list { return ( "a", "b", "c") } sub array { my @ret = ( "a", "b", "c"); return @ret; } sub ar_as_list{ my @ret = ( "a", "b", "c"); return $ret[-1] unless wantarray; return @ret; } my $a = list(); my $b = array(); my $c = ar_as_list(); print "list >$a<\narray >$b<\nar_as_list >$c<\n"; __DATA__ list >c< array >3< ar_as_list >c<
      Your model is perhaps flawed. At no time does Perl "return" an "array".

      A more accurate model is that Perl calls a subroutine with a flag that says "I want a scalar" or "I want a list". When the last expression of the subroutine is evaluated (such as for a return operator), that subroutine gets scalar or list eval context applied to it, as appropriate.

      So, a subroutine returns a list or scalar. Always. It never returns an array. If you write return @a, it will return the length of array @a in a scalar context, or a list copied from the contents of @a in a list context. At no time has it "returned" the "array" @a.

      Clear now?

      -- Randal L. Schwartz, Perl hacker
      Be sure to read my standard disclaimer if this is a reply.

      Hi ,

      sub array{ my @ret = ( "a", "b", "c", "d"); return \@ret; } my $b = &array(); my @B = @$b;
Re: Returning lists vs arrays
by OM_Zen (Scribe) on Jan 20, 2003 at 16:51 UTC
    Hi,

    The list is not aplaceholder ,just a set of values and for returning a set of values , an array or an arrayref is better than a list and any fuction returns a set of variable values which are not constants and hence an array is better and mostly used than list
    sub list { return(1,2); }
    is not often used

    sub arrays { @A = (1,2); return (\@A); }
    is something often to use in sub

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://228383]
Approved by broquaint
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (2)
As of 2022-08-18 05:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?