"That depends."

With a hashref you pass into your subroutine an anonymous hash of name/value pairs, which allows for named parameters, unordered parameter lists, facilitates optional parameters, extendability, and so on.

But it can be a little unwieldy. There is something succinct in "split /:/, $string, 3;" that is lost if it becomes:

split( { pattern => qr/:/, input => $string, giveme => 3 } );

In Perl Best Practices the assertion is that when the parameter list grows to four or more it's time to consider named params. The book doesn't attempt to define the Ten Commandments though. Mostly it's "A lot of good ideas unless you have a good argument to the contrary." I can say it's a rare occasion when I'm able to use a function that has named parameters without first looking them up in the function's documentation again. I don't often have to look up split, and when I do it's to clarify a less common use.

Do remember that named parameter lists via hashes aren't checked at compile-time by strict. So in a general sense, while a hash is very convenient, it doesn't afford some of the protections of first class variables. On the other hand, if ever you're tempted to manipulate variable names programatically, it's probably time to revert to using a hash in the first place.


Dave


In reply to Re: Benifits of using hash rather than number of variables? by davido
in thread Benifits of using hash rather than number of variables? by tart

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.