提供现代 Perl 5.36+ 惯用法与最佳实践,帮助编写稳健且易维护的应用。
复制安装指令,让 AI 自动完成配置 · 推荐新手
请帮我安装 askskill 上的 "perl-patterns" 技能: 1. 下载 https://raw.githubusercontent.com/affaan-m/ECC/main/skills/perl-patterns/SKILL.md 2. 保存为 ~/.claude/skills/perl-patterns/SKILL.md 3. 装好后重载技能,告诉我可以用了
请将这段旧版 Perl 代码重构为符合 Perl 5.36+ 习惯用法的写法,说明使用了哪些现代特性、可维护性改进和潜在风险。
输出重构后的代码,并附上现代写法、改进点与注意事项说明。
请为一个中型 Perl 应用制定项目结构、模块组织、错误处理、测试和命名约定,要求符合现代 Perl 最佳实践。
给出清晰的项目规范清单,便于团队统一开发和维护。
请审查这段 Perl 代码是否符合现代 Perl 5.36+ 最佳实践,指出可读性、健壮性、异常处理和测试覆盖方面的问题,并给出修改建议。
输出结构化审查意见及可执行的优化建议。
Idiomatic Perl 5.36+ patterns and best practices for building robust, maintainable applications.
Apply these patterns as a bias toward modern Perl 5.36+ defaults: signatures, explicit modules, focused error handling, and testable boundaries. The examples below are meant to be copied as starting points, then tightened for the actual app, dependency stack, and deployment model in front of you.
v5.36 PragmaA single use v5.36 replaces the old boilerplate and enables strict, warnings, and subroutine signatures.
# Good: Modern preamble
use v5.36;
sub greet($name) {
say "Hello, $name!";
}
# Bad: Legacy boilerplate
use strict;
use warnings;
use feature 'say', 'signatures';
no warnings 'experimental::signatures';
sub greet {
my ($name) = @_;
say "Hello, $name!";
}
Use signatures for clarity and automatic arity checking.
use v5.36;
# Good: Signatures with defaults
sub connect_db($host, $port = 5432, $timeout = 30) {
# $host is required, others have defaults
return DBI->connect("dbi:Pg:host=$host;port=$port", undef, undef, {
RaiseError => 1,
PrintError => 0,
});
}
# Good: Slurpy parameter for variable args
sub log_message($level, @details) {
say "[$level] " . join(' ', @details);
}
# Bad: Manual argument unpacking
sub connect_db {
my ($host, $port, $timeout) = @_;
$port //= 5432;
$timeout //= 30;
# ...
}
Understand scalar vs list context — a core Perl concept.
use v5.36;
my @items = (1, 2, 3, 4, 5);
my @copy = @items; # List context: all elements
my $count = @items; # Scalar context: count (5)
say "Items: " . scalar @items; # Force scalar context
Use postfix dereference syntax for readability with nested structures.
use v5.36;
my $data = {
users => [
{ name => 'Alice', roles => ['admin', 'user'] },
{ name => 'Bob', roles => ['user'] },
],
};
# Good: Postfix dereferencing
my @users = $data->{users}->@*;
my @roles = $data->{users}[0]{roles}->@*;
my %first = $data->{users}[0]->%*;
# Bad: Circumfix dereferencing (harder to read in chains)
my @users = @{ $data->{users} };
my @roles = @{ $data->{users}[0]{roles} };
isa Operator (5.32+)Infix type-check — replaces blessed($o) && $o->isa('X').
use v5.36;
if ($obj isa 'My::Class') { $obj->do_something }
use v5.36;
sub parse_config($path) {
my $content = eval { path($path)->slurp_utf8 };
die "Config error: $@" if $@;
return decode_json($content);
}
use v5.36;
use Try::Tiny;
sub fetch_user($id) {
my $user = try {
$db->resultset('User')->find($id)
// die "User $id not found\n";
}
catch {
warn "Failed to fetch user $id: $_";
undef;
};
return $user;
}
use v5.40;
sub divide($x, $y) {
try {
die "Division by zero" if $y == 0;
return $x / $y;
}
catch ($e) {
warn "Error: $e";
return;
}
}
Prefer Moo for lightweight, modern OO. Use Moose only when its metaprotocol is needed.
# Good: Moo class
package User;
use Moo;
use Types::Standard qw(Str Int ArrayRef);
use namespace::autoclean;
has name => (is => 'ro', isa => Str, required => 1);
has email => (is => 'ro', isa => Str, required => 1);
has age => (is => 'ro', isa => Int, default => sub { 0 });
…
讲解 Rust 惯用模式、所有权与并发实践,帮助构建安全高性能应用