Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

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'

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2021-04-13 06:54 GMT
Find Nodes?
    Voting Booth?

    No recent polls found