in reply to Advantages to returning array vs. reference
For previous discussions on this topic, see here.
In short, passing around references is much much faster, but can confuse people who are new to Perl, and may give your caller access to things they shouldn't if (say) you're passing back references to package variables.
Provided you don't have these problems, you may want to use wantarray to figure out what the caller is expecting. If they want a scalar, give them a reference. If they want a list, then return an array:
This adds a bit more DWIM-ism to your code, although it also runs the risk of confusing some people if they're expecting to get the number of results if they're calling your function in a scalar context.if (wantarray) { return @results; } else { return \@results; }
Cheers,
Paul
|
|---|