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

Advantages to passing a hash to a subroutine?

by alliswell (Novice)
on Jun 18, 2012 at 20:37 UTC ( #976893=perlquestion: print w/replies, xml ) Need Help??
alliswell has asked for the wisdom of the Perl Monks concerning the following question:

One advantage to passing a hash to a subroutine is that it can pass arguments in any order.

Is there any other reasons for using a hash instead of an array which would be helpful or a best practice? Why?

  • Comment on Advantages to passing a hash to a subroutine?

Replies are listed 'Best First'.
Re: Advantages to passing a hash to a subroutine?
by tobyink (Abbot) on Jun 18, 2012 at 21:05 UTC

    If the sub takes a lot of arguments (more than one or two), then passing a hash can be a lot more readable.

    perform_wedding($smith, $jones, $brown);

    Which one's the husband, which is the wife, and who is performing the ceremony?

    perform_wedding( official => $smith, wife => $jones, husband => $brown, );

    Ah, that's better!

    Another advantage, for subs which are intended to be called as methods (in an object oriented design), is it allows subclasses to easily override methods, taking additional parameters without disturbing the superclass. e.g.

    Imagine we have:

    my $venue = Wedding::Venue->new; $venue->perform_wedding($smith, $jones, $brown);

    And we have a subclass Elvis::Wedding::Venue for a venue which offers Elvis-themed weddings. It takes an extra parameter which says what song is sung at the ceremony...

    my $venue = Elvis::Wedding::Venue->new; $venue->perform_wedding($smith, $jones, $brown, "Hound Dog");

    Now, a new version of Wedding::Venue is released which takes a fourth parameter for the date of the wedding...

    my $venue = Wedding::Venue->new; $venue->perform_wedding($smith, $jones, $brown, '2012-06-18');

    ... and suddenly there's a conflict over that last parameter. This becomes a lot safer with named parameters.

    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

      Thank you, I like the readability answer.

Re: Advantages to passing a hash to a subroutine?
by toolic (Bishop) on Jun 18, 2012 at 20:47 UTC
    Not only can you pass them in any order with a hash, but you can omit any number of arguments without ugly commas and empty quotes (if you were trying to preserve position):
    foo('','','',123); # array

    Another advantage is that the arguments are self-documenting with a hash.

    Also consider passing your hash by reference.

      Thank you toolic

      Can you give an example on how the arguments are self-documenting as compared to not using a hash?

      Thank you!

        I think I see the example of the self-documenting hash from tobyinc. It makes it more clear.

        perform_wedding( official => $smith, wife => $jones, husband => $brown, );
Re: Advantages to passing a hash to a subroutine?
by eyepopslikeamosquito (Chancellor) on Jun 19, 2012 at 08:20 UTC

    The reasons behind why to use a hash of named arguments as a subroutine parameter is discussed in Perl Best Practices, Chapter 9, "Use a hash of named arguments for any subroutine that has more than three parameters". Luckily, Chapter 9 happens to be the free sample chapter from this book.

      Yes, thanks for pointing out the free PDF. Perfect!

      In Perl Best Practices, they state using "Named arguments replace the need to remember any ordering..Especially if you have, or are likely to have more than three arguments."

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://976893]
Approved by toolic
[holli]: you know, monk days are fun and all. but too many of them are bad for your health
[1nickt]: ...modern car. Suggests using TLA+ or other software modeling language to autogenerate code.
1nickt is not sure more and more layers separating the programmer from the program is the right way to go...
[holli]: happy one, shmem ;)
[1nickt]: I had an interview couople of weeks ago, heard from a principal engineer that a friend of his who works at The Google told him that the self-teaching software that determines the dynamic pricing for their adverts...
[1nickt]: ... has become so complex that there is no one at the Goole who knows what it does.
[marto]: William Gibson was right
[moritz]: a classical problem with machine learning
[1nickt]: moritz isn;t the difference today that the machine learning algos are in charge of most stuffs?
[moritz]: depends on what you mean by "most stuff"

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (13)
As of 2017-11-21 13:02 GMT
Find Nodes?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:

    Results (301 votes). Check out past polls.