in reply to Re: passing array as an argument
in thread passing array as an argument

Another thing is that, for performance reason, even there is only one parameter, it is still a good idea to pass array ref, instead of array.

(davido didn't mention this simply because this was not asked in the original post)

my @numb = (1..100); print sum(\@numb); sub sum { my $numb = shift; my $total; foreach (@$numb) {$total += $_}; return $total; }

Replies are listed 'Best First'.
Re: Re: Re: passing array as an argument
by sauoq (Abbot) on Oct 31, 2003 at 00:48 UTC
    Another thing is that, for performance reason, even there is only one parameter, it is still a good idea to pass array ref, instead of array.

    Well, that's not strictly true. Perl passes by reference by default. Consider:

    perl -le 'sub scare { $_[0] = "boo!" } my $me; scare($me); print $me'
    Often people subvert this by copying the parameters themselves. Code like this is more common than not:
    sub foo { my @args = @_; # ... do stuff ... }
    And usually, it's fine because argument lists aren't very big.

    As for passing a single large array, though, there isn't much reason to pass a reference other than to be able to name it something nice in the sub rather than work directly with @_.

    -sauoq
    "My two cents aren't worth a dime.";