Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

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


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

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (7)
As of 2021-02-26 13:45 GMT
Find Nodes?
    Voting Booth?

    No recent polls found