読者です 読者をやめる 読者になる 読者になる

MooseX::Declare

MooseX::Declareくらい綺麗にかければPerlでOOするのが面倒だとは思わないだろうなぁ。
http://search.cpan.org/~flora/MooseX-Declare-0.03/lib/MooseX/Declare.pm

    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 );
        }
    }