小编sac*_*jsk的帖子

我可以使用MooseX :: Declare定义类外的函数吗?

我最近开始使用模块MooseX :: Declare.我喜欢它的语法.它优雅而整洁.有没有人遇到过你想要在类中编写许多函数(其中一些很大)和运行到页面中的类定义的情况?是否有任何解决方法使类定义只需要声明的函数和类外的实际函数定义?

我要找的是这样的 -

class BankAccount {
    has 'balance' => ( isa => 'Num', is => 'rw', default => 0 );
    # Functions Declaration.
    method deposit(Num $amount);
    method withdraw(Num $amount);
}

# Function Definition.
method BankAccount::deposit (Num $amount) {
    $self->balance( $self->balance + $amount );
}

method BankAccount::withdraw (Num $amount) {
    my $current_balance = $self->balance();
    ( $current_balance >= $amount )
    || confess "Account overdrawn";
    $self->balance( $current_balance - $amount );
}
Run Code Online (Sandbox Code Playgroud)

我可以看到有一种方法可以使类变异.有谁知道怎么做?

perl module moose

8
推荐指数
1
解决办法
551
查看次数

标签 统计

module ×1

moose ×1

perl ×1