However,
ormy @values = f(...); # simple read, # @values is a copy
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.my %hash = f(...); # hash is also a copy my $value = $hash{'value'};
If you want to return more than one array or arrays and hashes where the returned lengths are unknown, references are your only choice:
makes @v1 slurp all return values, leaving @v2 empty. Solution, with a few comments:my (@v1, @v2) = f(...);
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
In reply to Re: Return value from function
by lyklev
in thread Return value from function
by perlCrazy
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |