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

Re: Why Moose uses this syntax??!!

by eyepopslikeamosquito (Archbishop)
on Dec 28, 2014 at 11:10 UTC ( [id://1111557]=note: print w/replies, xml ) Need Help??


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

    Not at compile time.

    >perl -c -we"my %h = 'a';" -e syntax OK

    How could it be?

    >perl -c -we"my %h = f();" -e syntax OK

    I disagree with the advice you quoted. f({...}) simply forces extra complexity in the caller just to avoid doing croak if @_ % 2; (which you don't really need anyway).

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (6)
As of 2024-04-23 13:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found