Moose OOP or Standard Perl?
Asked Answered
N

6

13

I'm going into writing some crawlers for a web-site, the idea is that the site will use some back-end Perl scripts to fetch data from other sites, my design (in a very abstract way..) will be to write a package, lets say:

package MyApp::Crawler::SiteName

where site name will be a module / package for crawling specific sites, I will obviously will have other packages that will be shared across different modules, but that not relevant here.

anyway, making long short, my question is: Why (or why not...) should I prefer Moose over Standard OO Perl?

Nearsighted answered 17/6, 2011 at 22:14 Comment(0)
R
23

While I disagree with Flimzy's introduction ("I've not used Moose, but I have used this thing that uses Moose"), I agree with his premise.

Use what you feel you can produce the best results with. If the (or a) goal is to learn how to effectively use Moose then use Moose. If the goal is to produce good code and learning Moose would be a distraction to that, then don't use Moose.

Your question however is open-ended (as others have pointed out). There is no answer that will be universally accepted as true, otherwise Moose's adoption rate would be much higher and I wouldn't be answering this. I can really only explain why I choose to use Moose every time I start a new project.

As Sid quotes from the Moose documentation. Moose's core goal is to be a cleaner, standardized way of doing what Object Oriented Perl programmers have been doing since Perl 5.0 was released. It provides shortcuts to make doing the right thing simpler than doing the wrong thing. Something that is, in my opinion, lacking in standard Perl. It provides new tools to make abstracting your problem into smaller more easily solved problems simpler, and it provides a robust introspection and meta-programming API that tries to normalize the beastiary that is hacking Perl internals from Perl space (ie what I used to refer to as Symbol Table Witchery).

I've found that my natural sense of how much code is "too much" has been reduced by 66% since I started using Moose[^1]. I've found that I more easily follow good design principles such as encapsulation and information hiding, since Moose provides tools to make it easier than not. Because Moose automatically generates much of the boiler-plate that I normally would have to write out (such as accessor methods, delegate methods, and other such things) I find that it's easier to quickly get up to speed on what I was doing six months ago. I also find myself writing far less tricky code just to save myself a few keystrokes than I would have a few years ago.

It is possible to write clean, robust, elegant Object Oriented Perl that doesn't use Moose[^2]. In my experience it requires more effort and self control. I've found that in those occasions where the project demands I can't use Moose, my regular Object Oriented code has benefitted from the habits I have picked up from Moose. I think about it the same way I would think about writing it with Moose and then type three times as much code as I write down what I expect Moose would generate for me[^3].

So I use Moose because I find it makes me better programmer, and because of it I write better programs. If you don't find this to be true for you too, then Moose isn't the right answer.

[^1]: I used to start to think about my design when I reached ~300 lines of code in a module. Now I start getting uneasy at ~100 lines.

[^2]: Miyagawa's code in Twiggy is an excellent example that I was reading just today.

[^3]: This isn't universally true. There are several stories going around about people who write less maintainable, horrific code by going overboard with the tools that Moose provides. Bad programmers can write bad code anywhere.

Recipient answered 17/6, 2011 at 23:30 Comment(5)
I've not used Moose, but I have used this thing that uses Moose? I think you have a misunderstanding of Catalyst. Catalyst does not use Moose; it is an alternative MVC.Isochor
Catalyst is a large application built on top of Moose. The example controller in the Catalyst::Manual::Intro in fact has you using Moose to build your controller. Catalyst's being an implementation of MVC is orthogonal to how Catalyst itself is built, and how it expects people to build and extend it.Recipient
I see that is now true. It wasn't always.Isochor
It's been true since 5.8000_01 2008-10-13 22:52:00 ;)Recipient
Yes, I see that now. I started using catalyst long before that. Regretfully.Isochor
U
10

You find the answer why to use Moose in the Documentation of it.

The main goal of Moose is to make Perl 5 Object Oriented programming easier, more consistent, and less tedious. With Moose you can think more about what you want to do and less about the mechanics of OOP.

From my experience and probably others will tell you the same. Moose reduces your code size extremly, it has a lot of features and just standard features like validation, forcing values on creation of a object, lazy validation, default values etc. are just so easy and readable that you will never want to miss Moose.

Unit answered 17/6, 2011 at 22:52 Comment(0)
D
3

Use Moose. This is from something I wrote last night (using Mouse in this case). It should be pretty obvious what it does, what it validates, and what it sets up. Now imagine writing the equivalent raw OO. It's not super hard but it does start to get much harder to read and not just the code proper but the intent which can be the most important part when reading code you haven't seen before or in awhile.

has "io" =>
    is => "ro",
    isa => "FileHandle",
    required => 1,
    handles => [qw( sysread )],
    trigger => sub { binmode +shift->{io}, ":bytes" },
    ;

I wrote a big test class last year that also used the handles functionality to redispatch a ton of methods to the underlying Selenium/WWWMech object. Disappearing this sort of boilerplate can really help readability and maintenance.

Deodar answered 17/6, 2011 at 22:42 Comment(2)
I'm not 100% sure that breaking encapsulation and using an after modifier is what you want here. Would a trigger that sets the ":bytes" layer on the stored FH not work better for you? Or does it need to be set on every fetch from the attribute?Recipient
@perigrin, you've got it right. I'll update it from after to a trigger.Deodar
I
2

I've never used Moose, but I've used Catalyst, and have extensive experience with OO Perl and non-OO Perl. And my experience tells me that the best method to use is the method you're most comfortable using.

For me, that method has become "anything except Catalyst" :) (That's not to say that people who love and swear by Catalyst are wrong--it's just my taste).

If you already have the core of a crawler app that you can build on, use whatever it's written in. If you're starting from scratch, use whatever you have the most experience in--unless this is your chance to branch out and try something new, then by all means, accomplish your task while learning something new at the same time.

I think this is just another instance of "which language is best?" which can never be answered, except by the individual.

Isochor answered 17/6, 2011 at 22:19 Comment(1)
could you elaborate on "why"? I'm comtemplating a switch to Catalyst and would love a look from the other side of the coin.Ancilla
D
1

When I learned about objects in Perl, first thing I thought was why it's so complicated when Perl is usually trying to keep things simple.

With Moose I see that uncomplicated OOP is possible in Perl. It sort of bring OOP of Perl back to manageable level.

(yes, I admit, I don't like perl's object design.)

Davinadavine answered 19/6, 2011 at 7:34 Comment(0)
A
0

Although this was asked 10 years ago, much has changed in the Perl world. I think most people now see that Moose didn't deliver all people thought it might. There are several derivative projects, lots of MooseX shims, and so on. That much churn is the code smell of something that's close to useful but not quite there. Even Stevan Little, the original author, was working on it's replacement, Moxie, but it never really went anywhere. I don't think anyone was ever completely satisfied with Moose.

Currently, the future is probably not Moose, irrespective of your estimation of that framework. I have a chapter for Moose in Intermediate Perl, but I'd remove it in the next edition. That's not a decision from quality, just on-the-ground truth from what's happening in that space.

Ovid is working on Corrina, a similar but also different framework that tries to solve some of the same problems. Ovid outlines various things he wants to improve, so you can use that as a basis for your own decision. No matter what you think about either Moose or Corrina, I think the community is now transitioning out of its Moose phase.

You may want to listen to How Moose made me a bad OO programmer, a short talk from Tadeusz Sośnierz at PerlCon 2019.

Even if your skeptical, I'd say try Moose in a small project before you commit to reconfiguring a large codebase. Then, try it in a medium project. I tend to think that frameworks like Moose look appealing in the sorts of examples that fit on a page, but don't show their weaknesses until the problems get much more complex. Don't go all in until you know a little more.

You should at least know the basics of Moose, though, because you are likely to run into other code that uses it.

The Perl 5 Porters may even include this one in core perl, but a full delivery may happen in the five to ten year range, and even then you'd have to insist on a supported Perl. Most people I encounter are about three to five years behind on Perl versions. If you control your perl version, that's not a problem. Not everyone does though. Holding out for Corrina may not be a good short-term plan.

Aves answered 20/2, 2022 at 20:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.