docs(zh-CN): update

This commit is contained in:
neo
2026-03-13 17:45:44 +08:00
parent f548ca3e19
commit 4c0107a322
88 changed files with 16872 additions and 280 deletions

View File

@@ -0,0 +1,77 @@
---
paths:
- "**/*.pl"
- "**/*.pm"
- "**/*.t"
- "**/*.psgi"
- "**/*.cgi"
---
# Perl 模式
> 本文档在 [common/patterns.md](../common/patterns.md) 的基础上扩展了 Perl 特定的内容。
## 仓储模式
在接口背后使用 **DBI****DBIx::Class**
```perl
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
```perl
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** 进行文件操作
```perl
use autodie;
use Path::Tiny;
my $content = path('config.json')->slurp_utf8;
```
## 模块接口
使用 `Exporter 'import'` 配合 `@EXPORT_OK` — 绝不使用 `@EXPORT`
```perl
use Exporter 'import';
our @EXPORT_OK = qw(parse_config validate_input);
```
## 依赖管理
使用 **cpanfile** + **carton** 以实现可复现的安装:
```bash
carton install
carton exec prove -lr t/
```
## 参考
查看技能:`perl-patterns` 以获取全面的现代 Perl 模式和惯用法。