Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Why Moose uses this syntax??!!

by Anonymous Monk
on Dec 28, 2014 at 06:23 UTC ( [id://1111539]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Why Moose uses syntax like this:

has 'friends' => ( is => 'rw', isa => 'Array', default => () );

instead of using this syntax

has 'friends' => { is => 'rw', isa => 'Array', default => () };

I think the latter syntax does make more sense.
Please someone make me convinced

Replies are listed 'Best First'.
Re: Why Moose uses this syntax??!!
by eyepopslikeamosquito (Archbishop) on Dec 28, 2014 at 11:10 UTC

    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. :-(

      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).

Re: Why Moose uses this syntax??!!
by CountZero (Bishop) on Dec 28, 2014 at 09:35 UTC
    Because that is the way the developer of Moose has decided it should be!

    It may look to you like it should be an anonymous hash, but "Yeah, well, that's just, like, your opinion, man".

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics
Re: Why Moose uses this syntax??!!
by ikegami (Patriarch) on Dec 28, 2014 at 10:05 UTC

    Why would you want to do the equivalent of

    has 'friends' => do { my %h = ( ... ); \%h };

    instead of

    has 'friends' => ( ... );

    It's wasteful.

Re: Why Moose uses this syntax??!! (because you dont know the answer)
by Anonymous Monk on Dec 28, 2014 at 10:07 UTC

    Please someone make me convinced

    See Perl Moose syntax, its the same question (deceptively simple interview question)

    Why is this red, convince me?
    First choice
Re: Why Moose uses this syntax??!! (merge duplicates)
by LanX (Saint) on Dec 28, 2014 at 15:00 UTC
    Why did you repost your question again???

    Are you aware of the struggle to merge two threads with proper discussions together again?

    Cheers Rolf

    (addicted to the Perl Programming Language and ☆☆☆☆ :)

Re: Why Moose uses this syntax??!!
by Anonymous Monk on Dec 28, 2014 at 08:45 UTC
    $ perl -wE 'my $m = {is => "rw", isa => "Array", default => ()}' Odd number of elements in anonymous hash at -e line 1.
      That's because default => () is incorrect. This doesn't answer the question. If anything, that shows an advantage of using a hash ("free" parameter count check).
        "because it's not correct" is a reasonable answer, I'd say :) Other then that, I don't know Moose and the OP doesn't know it either :)
Re: Why Moose uses this syntax??!!
by Anonymous Monk on Dec 28, 2014 at 11:49 UTC

    So "performance" is the answer?

      That may be a reason, but maybe the author just decided that this is how the syntax will be, and since there's nothing wrong with it, they're free to make that decision. This is Perl - There Is More Than One Way To Do It.

Re: Why Moose uses this syntax??!!
by Anonymous Monk on Dec 29, 2014 at 05:16 UTC
    has 'x' => (is => 'rw', isa => 'Int'); has 'x' => {is => 'rw', isa => 'Int'};

    The second syntax is more righteous
    Some times some people say TIMTOWTDI. But in this case there is just one way better than another
    I'm gonna tell ya the reasons
    First, the second syntax is faster than first one because you pass one string and just one hash reference while first syntax passes one string and list of law key value pairs. In the first case, the subroutine probably does something like:

     my ($propname,%options)=@_

    This is just wasteful isn't it? You make list of key value pairs and copy to @_, and copy again to %options.
    But in the second case, you only make list once and what you copy is just a reference.
    When you make a big program which uses a lot of classes, the second syntax would be more efficient
    Second, inconsistencies.
    There are so many module which uses first syntax and there also second syntax.
    That leads users into confusion because of inconsistencies of syntaxes. for example the module 'Template' uses second syntax in 'process' subroutine

    use Template; # some useful options (see below for full list) my $config = { INCLUDE_PATH => '/search/path', # or list ref INTERPOLATE => 1, # expand "$var" in plain text POST_CHOMP => 1, # cleanup whitespace PRE_PROCESS => 'header', # prefix each template EVAL_PERL => 1, # evaluate Perl code blocks }; # create Template object my $template = Template->new($config); # define template variables for replacement my $vars = { var1 => $value, var2 => \%hash, var3 => \@list, var4 => \&code, var5 => $object, }; # specify input filename, or file handle, text reference, etc. my $input = 'myfile.html'; # process input template, substituting variables $template->process($input, $vars) || die $template->error();

    The laziness of not typing two characters '{' and '}' make bigger problem called 'inconsistency'
    So what I want to say is: Moose must support at least both syntaxes
    PS: It seems that here are so many emotional people. And you even don't give me logical answers.And taste is not a matter of programming. It is matter of food or something. Please find taste in other places

      Disagree; there is no performance or handling difference with subroutine argument lists that small, the inconsistency, you assert, is a matter of preference not fact, and this is how many of us write those attributes–

      has x => is => "rw", isa => "Int";

      Plenty of modules also handle args this way and your suggestion would lead to monstrosities like my ( $self, $args, $something, $else, $i, $can, $now_hash, $manage_list, $todo ) = @_; because if it’s possible, someone will do it.

      That leads users into confusion because of inconsistencies of syntaxes

      What users? Those who don't read the docs?

      And taste is not a matter of programming.

      Yes, it is - you're proving this point by continuing this discussion. Unless of course you can prove that your imagined performance difference actually exists by showing a benchmark that shows a significant difference.

      So what I want to say is: Moose must support at least both syntaxes

      Great, then file a feature request with Moose, or even better, write a patch.

Re: Why Moose uses this syntax??!!
by Anonymous Monk on Dec 28, 2014 at 12:20 UTC

    PBP link:
    http://justpaste.it/imt8

Re: Why Moose uses this syntax??!!
by sundialsvc4 (Abbot) on Dec 28, 2014 at 20:03 UTC

    Many of the design decisions in Moose trace back to the fact that Perl-5 is the underlying implementation language.   The use of parentheses, and the => operator, both already exist in the Perl-5 language, and they define a list, whereas braces denote a hash.   The Moose team worked within the context of the language they were building upon, to create the higher-level constructs that they did.   Then, they took care to make the usage consistent, so that subsequent developers using Moose would not have to “guess” which one to use in a given situation.

    They (koff, koff ...) never set out to “convince you,” and they don’t have to do so now.   If they ... ;-) ... “offended your sense of sensibilities,” then c’est la guerre.

      Twat. Both syntaxes being discussed "already exist in the Perl-5 language". You really have never bothered to learn Perl at all have you.

      Which makes it all the more annoying that you continue to pontificate wrongly despite the dozens of times that it has been proved to you that you are totally unqualified to be doling out advice on Perl.

      Do you really just forget how wrong you've been after a couple of weeks?

      If so, I suggest you go back and look at your worst nodes and then at the replies to them which explain why you've been downvoted, each time you return here after a few days break, to remind you about all the stuff you think you know, but don't.


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (3)
As of 2024-03-29 06:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found