in reply to Memory Use and/or efficiency passing scalars to subs
This allows you to use the subroutine in two ways. One directly modifying the parameters passed:sub foo { if (defined wantarray) { # we're in scalar or list context my @param = @_; # make a copy of the parameters foo( @param ); # recursive call for ease of maintenance return @param; # return the result } # we only get here if in void context # do what you need to do on @_ directly, only thing to maintain } #foo
and one returning the changed parameters, keeping the old:foo( @data );
This gives me a lot of flexibility: on the one hand the direct changing which is CPU and memory efficient (no need to copy anything) and the copying way, which also comes in handy if you're passing constant (non left-value) parameters, like so:@newdata = foo( @olddata );
If you're really concerned about performance, you could remove the recursive call, but that would leave you with two identical code paths to maintain, which is always a bad thing.@data = foo( qw(foo bar baz) );
Liz
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Memory Use and/or efficiency passing scalars to subs
by knexus (Hermit) on Aug 30, 2003 at 21:14 UTC |