perl-patterns

Solid

现代 Perl 5.36+ 的惯用法、最佳实践和约定,用于构建稳健、可维护的 Perl 应用程序。

AI & Automation 196,640 stars 30253 forks Updated 2 days ago MIT

Install

View on GitHub

Quality Score: 95/100

Stars 20%
100
Recency 20%
100
Frontmatter 20%
70
Documentation 15%
100
Issue Health 10%
50
License 10%
100
Description 5%
100

Skill Content

# 现代 Perl 开发模式 适用于构建健壮、可维护应用程序的 Perl 5.36+ 惯用模式和最佳实践。 ## 何时启用 * 编写新的 Perl 代码或模块时 * 审查 Perl 代码是否符合惯用法时 * 重构遗留 Perl 代码以符合现代标准时 * 设计 Perl 模块架构时 * 将 5.36 之前的代码迁移到现代 Perl 时 ## 工作原理 将这些模式作为偏向现代 Perl 5.36+ 默认设置的指南应用:签名、显式模块、聚焦的错误处理和可测试的边界。下面的示例旨在作为起点被复制,然后根据您面前的实际应用程序、依赖栈和部署模型进行调整。 ## 核心原则 ### 1. 使用 `v5.36` 编译指令 单个 `use v5.36` 即可替代旧的样板代码,并启用严格模式、警告和子程序签名。 ```perl # 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!"; } ``` ### 2. 子程序签名 使用签名以提高清晰度和自动参数数量检查。 ```perl 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; # ... } ``` ### 3. 上下文敏感性 理解标量上下文与列表上下文——这是 Perl 的核心概念。 ```perl 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 co...

Details

Author
affaan-m
Repository
affaan-m/everything-claude-code
Created
4 months ago
Last Updated
2 days ago
Language
JavaScript
License
MIT

Integrates with

Related Skills