Perl 模式¶
本文档在 common/patterns.md 的基础上扩展了 Perl 特定的内容。
仓储模式¶
在接口背后使用 DBI 或 DBIx::Class:
package MyApp::Repo::User;
use Moo;
has dbh => (is => 'ro', required => 1);
sub find_by_id ($self, $id) {
my $sth = $self->dbh->prepare('SELECT * FROM users WHERE id = ?');
$sth->execute($id);
return $sth->fetchrow_hashref;
}
DTOs / 值对象¶
使用带有 Types::Standard 的 Moo 类(相当于 Python 的 dataclasses):
package MyApp::DTO::User;
use Moo;
use Types::Standard qw(Str Int);
has name => (is => 'ro', isa => Str, required => 1);
has email => (is => 'ro', isa => Str, required => 1);
has age => (is => 'ro', isa => Int);
资源管理¶
- 始终使用 三参数 open 配合
autodie - 使用 Path::Tiny 进行文件操作
模块接口¶
使用 Exporter 'import' 配合 @EXPORT_OK — 绝不使用 @EXPORT:
依赖管理¶
使用 cpanfile + carton 以实现可复现的安装:
参考¶
查看技能:perl-patterns 以获取全面的现代 Perl 模式和惯用法。
本页原文 Markdown:在 AtomGit 查看·内容源自开源项目 ev/ECC