perl - 我可以使用 MooseX::Declare 在类之外定义函数吗?

标签 perl module moose

我最近开始使用模块 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 );
}

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

最佳答案

容易(但需要添加到文档中)。

class BankAccount is mutable {
}

顺便说一句,你为什么要在类之外定义你的方法?

你可以去
class BankAccount is mutable {
    method foo (Int $bar) {
         # do stuff
    }
}

关于perl - 我可以使用 MooseX::Declare 在类之外定义函数吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/502463/

相关文章:

perl - 如何重写这个评估 block

ruby - 为什么 "extend"不向类添加常量,而 "include"却添加常量?

Perl Moose TypeDecorator 错误。我该如何调试?

php - 类似于 CLOS 的 PHP 对象模型

regex - 从文本文件中获取数据

perl - Mojolicious 应用程序使用谷歌身份验证,插件 OAuth2

perl - 这个 Perl grep 如何工作来确定几个散列的联合?

javascript - Internet Explorer 中模块的加载超时

python "string"模块?

perl - 可以将 MooseX 模块与 Mouse 类一起使用吗?