views:

638

answers:

3

I've stumbled upon the MooseX::Declare documentation, and I have to say "WOW!". That looks really nice! I think it's the cleanest OOP syntax Perl has.

use MooseX::Declare;

class BankAccount {
    has 'balance' => ( isa => 'Num', is => 'rw', default => 0 );

    method deposit (Num $amount) {
        $self->balance( $self->balance + $amount );
    }

    method withdraw (Num $amount) {
        my $current_balance = $self->balance();
        ( $current_balance >= $amount )
            || confess "Account overdrawn";
        $self->balance( $current_balance - $amount );
    }
}

Example Copied from the documentation.

What do you think? Can Perl OOP be any nicer?

+8  A: 

I am not sure what your question is. "Can Perl OOP be any nicer?" Probably, but I do agree that MX::Declare is good. I use it almost exclusively now.

jrockway
+3  A: 

I've learned to "hate" Perl. I stumbled across MooseX a day ago and it brought some hope back. Will definitely take a look at it in the future.

buster
+2  A: 

I've been playing with it off and on for a few weeks....

The only thing that trips me up is remembering to use inside the class defs.

Otherwise, I've been thinking about using for my next small to medium size project.

Unless Moose efficiency has improved greatly in the last year or so since I last played with it, you don't really want to use it on small projects. It incurs a very substantial compile-time overhead. AFAICT, it seems to be mostly useful for large and complex systems that have heavy computational or io requirements, where compile time is only a miniscule percentage of total execution time.
Zed
Moose has improved greatly in the last year or so, in fact there have been 31 releases since June 12 last year. There is still startup overhead and so if you have a hard requirement for startup times you'll need to look into work around but I've been using Moose for a year now for my command line scripts and been happy with it.
perigrin
I should also note that MooseX::Declare is still a bit young and has some overhead that Moose itself doesn't. The method signature parsing for example took me by surprise at first. That said if you don't use a feature you won't incur (most of) the cost of that feature either.
perigrin