Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Re: Moose - my new religion

by cavac (Deacon)
on Jan 08, 2012 at 11:52 UTC ( #946850=note: print w/replies, xml ) Need Help??

in reply to Moose - my new religion

Maybe it's just me, but what exactly is modern about Moose? To me, it feels a bit like i've been thrown back in time to when i was programming in C++. For me it feels like it is limiting the potential of a program by defining restricted interfaces.

I always felt that C++ and Java OO always tried to force me into a confined space where only things are possible that the original developer of a class felt necessary. Yeah, i realize that this may or may not be a good thing, depending on what you are trying to accomplish.

For me, using the Perl 5 native OO actually feels like i have been released from a prison camp. I don't have to overload objects or have my local, modified copy of a Perl module just because the long-gone author of that module forgot to close a file. I just wadd a single line of code like $broken->{infile}->close() and be done with it. Makes - in my opinion - for a much more readable code and is easier to debug than having to step through multiple parent classes.

Last time i tested it (quite some time ago), Moose also had a terrible, unacceptable performance penalty that prohibited me from using it on our production servers.

As i said, this is my personal opinion, this may differ if i where in a big development team and/or had much more server resources available. But as it stands now, in a developer team of one, for me Moose does not make sense at all...

Disclaimer: There is of course the chance i misunderstood completly how Moose works. But then again, i'm quite happy with the "classic Perl 5 OO", so i didn't invest weeks and weeks of learning Moose in exquisite detail.

"Believe me, Mike, I calculated the odds of this succeeding against the odds I was doing something incredibly stupidů and I went ahead anyway." (Crow in "MST3K The Movie")

Replies are listed 'Best First'.
Re^2: Moose - my new religion
by tobyink (Abbot) on Jan 08, 2012 at 12:49 UTC

    Assuming you already use OO, Moose makes a lot of common tasks much easier. Writing object accessors (i.e. getter/setter methods) all the time gets tedious. Moose makes them trivial - and even adds type checking. So if your BankAccount class has a "current_balance" attribute, rather than writing something like this:

    sub current_balance { my $self = shift; if (@_) { my $new = shift; croak "not a number" unless Scalar::Util::looks_like_number($new); $self->{current_balance} = $new; } return $self->{current_balance}; }

    You just write this:

    has current_balance => (is => 'rw', isa => 'Num');

    And as a bonus, you get the same type checking thrown in to your constructor. (And you never need to write sa constructor again!)

    It frees you from having to worry about the ins and outs of writing constructors and accessors and so on, allowing you to spend more time focussed on higher level stuff. That's got to be a good thing.

    Method modifiers are another great thing. Say you have a class DB which offers methods "select", "insert", "update" and "delete". For debugging you might want to create this class:

    package DebuggingDB; use Moose; foreach my $method (qw/select insert update delete/) { before $method => sub { warn "$method was called" }; }

    That way, whenever one of those methods is called, you see a warning about it. But for this to work, you'd need to go through your code and replace DB->new with DebuggingDB->new everywhere. But we can go better than that...

    package DebuggingDB; use Moose::Role; foreach my $method (qw/select insert update delete/) { before $method => sub { warn "$method was called" }; }

    Now DebuggingDB is not a class, but a role, and we can compose it at run-time:

    use Moose::Util qw/apply_all_roles/; apply_all_roles('DB', 'DebuggingDB') if $ENV{DEBUG};

    But maybe we know the connection to the accounts database is working fine, and we only need to debug the connection to marketing...

    my $accounts = DB->new(...); my $marketing = DB->new(...); use Moose::Util qw/apply_all_roles/; apply_all_roles($marketing, 'DebuggingDB') if $ENV{DEBUG};

    The ability to apply roles not just to classes, but to objects at run-time, is very powerful. This is something you can do without Moose, certainly, but Moose makes it a lot easier.

    Lastly, it gives you some very powerful introspection: much more so than Perl's default OO introspection (isa, can and DOES).

    Yes, Moose does have a performance hit. However, much of that is compile-time rather than run-time. So if you have a long-running process, such as a daemon, this is usually quite acceptable. (Whereas in a CGI script, where start-up time is important, Moose would probably be impractical.)

    Moose isn't right for every project, but if you're doing OO, if start-up time isn't so important, and you don't care about adding some pretty heavy dependencies to your project, then it's a natural choice.

      I value your input. Thanks!

      Update: Seems like this reply is getting the odd negative vote. I agree that this post may not be to the likings of everybody. And you're in your rights to give me negative votes. But i'll ask you to add a comment explaining what you didn't like about it so i can use it as a learning experience.

      My programs usually have very few traditional getter/setter constructs. These days, i mostly work with multiple processes (prefork stuff), so i do more or less all data exchange though DBI or Memcached (depending on if the data needs to be valid all the time or is just for debugging or non-critical single user settings).

      If i need to add multiple similar functions (in this case specifying a modular plugin API) i do something like this:

      Update 2: Added readmore tags

      I don't think i have more than a dozen really classical getter/setters in my 40.000+ lines of Maplat Perl code (OSS + DarkPAN).

      "Believe me, Mike, I calculated the odds of this succeeding against the odds I was doing something incredibly stupidů and I went ahead anyway." (Crow in "MST3K The Movie")

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://946850]
[choroba]: Corion Nice
[Corion]: Hi choroba! I'm somewhat fond of this picture - I think it is Breaking Bad-style even though I haven't watched the series at all ;)
[Corion]: Hmmm - and now that I look at it, the gallery I'm using doesn't produce non-Javascript compatible links in the sense that hotlinking to an image will only work for Javascript enabled...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2017-02-27 08:30 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (377 votes). Check out past polls.