Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

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.


In reply to Re^2: Moose - my new religion by tobyink
in thread Moose - my new religion by jdrago999

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others taking refuge in the Monastery: (11)
    As of 2014-12-18 23:35 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      Is guessing a good strategy for surviving in the IT business?





      Results (69 votes), past polls