Re: Shift vs. @_
- From: "Paul Lalli" <mritty@xxxxxxxxx>
- Date: 10 Apr 2006 05:36:37 -0700
Adam W wrote:
I was wondering when it is appropriate to do multiple shifts on
parameters passed to a subroutine and when it is appropriate to just
feed it @_.
For example, you have:
&fancy_sub($a, $b, $c, @d);
Don't use the & to call subroutines unless you know what that does and
you want those two side-effects. In all other cases, leave the & off.
perldoc perlsub
for more information
Is it better (I'm leaving this term vague on purpose) to write:
sub fancy_sub {
my($a, $b, $c, @d) = @_;
...do some fancy stuff...
}
or
sub fancy_sub {
my $a = shift;
my $b = shift;
my $c = shift;
my @d = @_;
...do some fancy stuff...
}
It is pointless to ask which is "better" when the two algorithms do not
do the same thing. The first makes copies of the values in @_. The
latter removes elements from @_ and assigns them to the individual
variables. That is, in the first, @_ remains as it was, containing
all the arguments passed into the script. In the second, @_ has lost
its first three elements.
So the question is not "which is better?", but rather "Which do you
want?" That is not a question that has a generic answer.
Paul Lalli
.
- References:
- [Subroutine Optimization] Shift vs. @_
- From: Adam W
- [Subroutine Optimization] Shift vs. @_
- Prev by Date: Re: a way to avoid recompiling perl?
- Next by Date: Re: match word in first array to any word in second array
- Previous by thread: [Subroutine Optimization] Shift vs. @_
- Next by thread: Re: [Subroutine Optimization] Shift vs. @_
- Index(es):
Relevant Pages
|