in reply to Why Moose uses this syntax??!!
Why are you so hung up on this? It seems to be more or less a matter of taste. There are plenty of CPAN modules that take a list of raw name/value pairs, and plenty that take a hash reference instead.
As pointed out by ikegami, the way Moose does it is probably more efficient. OTOH, Perl Best Practices chapter nine ("Use a hash of named arguments for any subroutine that has more than three parameters") advises "as tempting as it may be, don't pass them as a list of raw name/value pairs". Conway recommends passing named arguments as a hash reference instead, because, as ikegami also noticed, doing so gives you a good chance of catching common blunders, such as the one made by the OP, at compile-time ("Odd number of elements in hash"), rather than run-time.
Update: As indicated at Named Subroutine Parameters: Compile-time errors vs. Run-time warnings, this "reason" was an error in the book: anonymous hash population is done at run-time, not compile-time. Conway still stands by the advice though because "Error messages that point users to the right place are definitely worth the (tiny) overhead of passing named args in a hash".
BTW, PBP chapter nine used to be the free sample chapter, but has now become a broken link. :-(
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Why Moose uses this syntax??!!
by ikegami (Patriarch) on Dec 29, 2014 at 17:41 UTC |