Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Collecting all values with shift

by erikharrison (Deacon)
on Aug 30, 2002 at 04:27 UTC ( #193998=note: print w/ replies, xml ) Need Help??


in reply to Collecting all values with shift

If you are not sure of the number of args, just assign to an array

sub foo { my (@args) = @_; }

Why not use @_ directly, you may ask? Well, first, @_ has magical powers. Second, it's less readable.

In fact, ask yourself why the sub has a variable number of args - is it trying to do to much? If so, then maybe you ought to consider breaking the function down into smaller bits with more clearly defined purposes

If the function has a clear need for multiple args, it may still be true that the first couple of args have a defined purpose and the remainder of the args define a variable length additional data. In that case, consider naming those important initial args in a useful way, leaving the remainder in a list.

sub foo_which_processes_a_list { my ( $employee_name, @tasks) = @_; }

This has the added benefit of promoting useful information out of the collection of the list to a more important looking scalar

Cheers,
Erik

Light a man a fire, he's warm for a day. Catch a man on fire, and he's warm for the rest of his life. - Terry Pratchet


Comment on Re: Collecting all values with shift
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (9)
As of 2015-07-06 08:55 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 (70 votes), past polls