Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: shift vs @_

by Anonymous Monk
on Oct 03, 2006 at 08:27 UTC ( #576013=note: print w/ replies, xml ) Need Help??


in reply to shift vs @_

Suppose you'd write a function like map, its first argument plays a very different role than the rest - which can be any length. Wouldn't it make sense to write:

sub my_map { my $special_thingy = shift; foreach my $item (@_) { apply($special_thingy, $item); } }
Starting with
my ($special_thingy, @list_of_items) = @_;
is a potentially costly operation, since you are copying everything else in @_ - and it is slightly different as you lose the aliasing. And writing:
sub my_map { foreach my $item (@_[1 ..$#_]) { apply($_[0], $item); } }
or
sub my_map { for (my $i = 1; $i < @_; $i++) { apply($_[0], $_[$i]); } }
is not something I fancy.


Comment on Re: shift vs @_
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://576013]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (17)
As of 2015-07-29 12:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (263 votes), past polls