If you are returning a simple, short list of values, or even a hash, returning the values is probably the best, because it is the simplest.
However,
my @values = f(...); # simple read,
# @values is a copy
or
my %hash = f(...); # hash is also a copy
my $value = $hash{'value'};
makes a copy of all values, so if you return a lot of values, you might have some performance issues. In that cast, a reference might be faster.
If you want to return more than one array or arrays and hashes where the returned lengths are unknown, references are your only choice:
my (@v1, @v2) = f(...);
makes
@v1 slurp all return values, leaving @v2 empty. Solution, with a few comments:
sub f {
(...)
return \@v1, \@v2, \%v3; # return 2 array and a hash
# reference
}
my ($ar1, $ar2, $hr3) = f(); # collect references
my @ar1 = @{ $ar1 }; # get the first array,
# creating a duplicate
foreach (@ {$ar2} ) {...} # avoid copying the array,
# probably more efficient
my $value = $hr3->{'value'} # some people find this
# notation cumbersome
# and avoid references
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.