http://www.perlmonks.org?node_id=976893

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 (Canon) 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 (Archbishop) 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."