in reply to Subroutines and Passing data ...

There are a couple of different ways to do this.

## 1. As an array sub one { return ( $val1, $val2 ); } my @ar = one(); print $ar[0], $ar[1]; ## 2. As an arrayref sub two { return [ $val1, $val2 ]; } my $aref = two(); print $aref->[0], $aref->[1]; ## 3. As a hashref sub three { return { val1 => $val1, val2 => $val2 }; } my $href = three(); print $href->{ 'val1' }, $href->{ 'val2' };

I'm sure there are other ways too, but these are commonly the way I do it. I would suggest 3. for unrelated values, and 1. only if you actually want to return an array of similar values.

    -Bryan

Replies are listed 'Best First'.
Re^2: Subroutines and Passing data ...
by tphyahoo (Vicar) on Jun 13, 2005 at 14:07 UTC
    I would choose method three as well. When I was starting out doing this I tried returning data in a regular hash (not a hash ref), and that worked, kind of sort of, but then my data got a little more complicated, and I tried putting refs to other hashes in my hash, and this crashed and burned.

    So, I would get used to passing data back and forth with hashrefs, or in some circumstances, array refs. I wouldn't return any non-ref array or hash values, just for the sake of... well, that is what my gut says.

Re^2: Subroutines and Passing data ...
by salva (Canon) on Jun 13, 2005 at 13:43 UTC
    another way:
    sub another_way { ... return (val1 => $val1, val2 => $val2); } my %hash = another_way(); print "$hash{val1} $hash{val2}\n";