perl – 我可以使用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 );
}

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

解决方法

容易(但需要添加到文档).
class BankAccount is mutable {
}

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

你可以去

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

相关文章

1. 如何去重 #!/usr/bin/perl use strict; my %hash; while(...
最近写了一个perl脚本,实现的功能是将表格中其中两列的数据...
表的数据字典格式如下:如果手动写MySQL建表语句,确认麻烦,...
巡检类工作经常会出具日报,最近在原有日报的基础上又新增了...
在实际生产环境中,常常需要从后台日志中截取报文,报文的形...
最近写的一个perl程序,通过关键词匹配统计其出现的频率,让...