On May 19, Aaron Craig said:
>
>sub PassAnEntireArray(@)
> {
> my(@array) = @_;
> ...do some stuff...
> }
>
>sub PassAnArrayRef($)
> {
> my($ar) = @_;
> ... do some stuff...
> }
>
>I assume passing array refs (and hash refs for that matter) would be faster
>and more efficient -- does anyone have any definitive answer?
Before I answer your question, I have to ask you to not use subroutine
prototypes. 9 out of 10 Perl programmers use them incorrectly or don't
know what they do.
That being said, there are TWO really useful uses:
sub my_map (&@) {
my $code = shift;
my @return;
push @return, $code->($_) for @_;
return @return;
}
That makes a function that allows for a naked block as its first argument:
@results = my_map { $_ * 2 } (1,2,3,4);
And the other use:
sub my_push (\@@) {
my $aref = shift;
push @$aref, @_;
}
That allows you to send an array to the function BY REFERENCE, without
typing the backslash when the function is called:
my_push @foo, 1, 3, 5, 7;
The problem is: PROTOTYPES MUST BE SEEN BEFORE THE FUNCTION IS CALLED.
So few people realize that (for one reason or another[1]).
To answer your question, it is much faster to pass a reference to an array
or hash than to pass the array or hash. There are times you HAVE to,
anyway (or, if you didn't, you'd have to send extra arguments about the
size of the thing you're passing, which is just hideous).
[1] they don't read docs, or they see other people using them, and assume
they know what they do
--
Jeff "japhy" Pinyan [EMAIL PROTECTED] http://www.pobox.com/~japhy/
Are you a Monk? http://www.perlmonks.com/ http://forums.perlguru.com/
Perl Programmer at RiskMetrics Group, Inc. http://www.riskmetrics.com/
Acacia Fraternity, Rensselaer Chapter. Brother #734
** I need a publisher for my book "Learning Perl's Regular Expressions" **