Personally, I can see very little point using a hash just to name the returns from a subroutine. What are you going to do with the names?
In the example you gave, all your doing is using them to unpack the hash into seperately named entities anyway, so don't. Pass back references for the arrays (or hashes) and assign them directly to the named references in the calling code.
The result is quicker, cleaner and more readable (IMO).
### Take in the string $val, produce some ### scalars and two arrays sub mysub { my $val = shift; my @foos = 0 .. rand(50); my @bars = 0 .. rand(50); return scalar @foos, # How many foos scalar @bars, # How many bars [sort @foos], # Assorted foos (no chilli) [sort @bars]; # Ditto bars (hic!) } my $val = 'Irrelevant'; my ($foo, $bar, $gak, $flub) = mysub($val); print "I have $foo foos: @{$gak}\n"; print "I have $bar bars: @{$flub}\n"; # That's "sheep" and "pub" when +I'm sober. print "The first foo is $gak->[0]\n"; print "The last bar is $flub->[-1]\n"; __DATA__ D:\Perl\test>junk I have 24 foos: 0 1 10 11 12 13 14 15 16 17 18 19 2 20 21 22 23 3 4 5 +6 7 8 9 I have 29 bars: 0 1 10 11 12 13 14 15 16 17 18 19 2 20 21 22 23 24 25 +26 27 28 3 4 5 6 7 8 9 The first foo is 0 The last bar is 9
A numeric sort would be more sensible for numeric data, but its only an example!
In reply to Re: Optimizing the use of hashes in subroutines
by BrowserUk
in thread Optimizing the use of hashes in subroutines
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |