in reply to Returning two lists from a sub

#!/usr/bin/perl -w
use strict;

# This won't work:
my (@a,@b) = getlists_1();

print "getlists_1: A ( @a ) and B ( @b ) \n";

# Instead, pass by reference:
(*a,*b) = getlists_2(\@a, \@b);

print "getlists_2: A ( @a ) and B ( @b ) \n";

# ------------------------------------------------------------------
# From 'man perlsub':
# If you return one or more aggregates (arrays and hashes), these will be
# flattened together into one large indistinguishable list.
# ------------------------------------------------------------------
sub getlists_1 {
    my @foo = (1,2,3);
    my @bar = (4,5,6);
    return @foo,@bar;
}


sub getlists_2 {
    my ($foo, $bar) = @_;
    @$foo = (1,2,3);
    @$bar = (4,5,6);
    return ($foo,$bar);
}

Replies are listed 'Best First'.
Re^2: Returning two lists from a sub
by chromatic (Archbishop) on Jun 07, 2007 at 21:34 UTC

    The trick of assigning to the two strict-proof typeglobs is cute, but I can't really recommend it seriously.