Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Accessing Subroutine Arguments

by Chmrr (Vicar)
on Apr 04, 2002 at 00:07 UTC ( #156501=note: print w/ replies, xml ) Need Help??


in reply to Accessing Subroutine Arguments

Since this is the realm of coding style, it's almost certainly all personal preference. ;>

I know that I tend to prefer using a hash once I get above three or four parameters. Some of the reasons that I can think of why I tend to do so:

  • I don't have to remember the order in which the parameters are passed. Granted, I do have to remember the names of the keys, but I think that's a small sacrifice. One can also use the same two or three key names for multiple subroutines ot make it easier to remember.
  • Self-documentation. As you hinted at, to me it's clearer what:load_foo(filename=>"bar",template="baz", debug=>1); is doing than: load_foo("bar","baz",1); does.
  • It means I can establish defaults for some parameters rather easily. To use the same fictitious load_foo subroutine:
    sub load_foo { my %args = ( # Defaults template => "zort", debug => 0, @_); ... # yadda yadda yadda }
  • No reordering. If you're using lots of individual scalars, and you find out you don't need the third our of seven of them -- you'd better make sure you take that one out of all of the places where you call it, or you're up a creek. With a hash, it's not so much of an issue.

*shrug* All of the above are just personal preferences, yes. Do what seems to work best for you.

perl -pe '"I lo*`+$^X$\"$]!$/"=~m%(.*)%s;$_=$1;y^`+*^e v^#$&V"+@( NO CARRIER'


Comment on Re: Accessing Subroutine Arguments
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (2)
As of 2015-07-06 01:49 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 (68 votes), past polls