Modern Perl Development Patterns
Idiomatic Perl 5.36+ patterns and best practices for building robust, maintainable applications.
When to Activate
- Writing new Perl code or modules
- Reviewing Perl code for idiom compliance
- Refactoring legacy Perl to modern standards
- Designing Perl module architecture
- Migrating pre-5.36 code to modern Perl
When NOT to Use
- Other languages (use
python-patterns, golang-patterns, etc.)
- Perl security review specifically (use
perl-security)
How It Works
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.
Contents
| Topic |
Reference |
Core principles (pragma, signatures, context, deref, isa) |
references/core-principles.md |
| Error handling (eval/die, Try::Tiny, try/catch) |
references/error-handling.md |
| Modern OO (Moo, roles, native class) |
references/modern-oo.md |
| Regular expressions |
references/regex.md |
| Data structures and references |
references/data-structures.md |
| File I/O (three-arg open, Path::Tiny) |
references/file-io.md |
| Module organization and Exporter |
references/modules.md |
| Tooling (perltidy, perlcritic, carton) |
references/tooling.md |
Quick Reference: Modern Perl Idioms
| Legacy Pattern |
Modern Replacement |
use strict; use warnings; |
use v5.36; |
my ($x, $y) = @_; |
sub foo($x, $y) { ... } |
@{ $ref } |
$ref->@* |
%{ $ref } |
$ref->%* |
open FH, "< $file" |
open my $fh, '<:encoding(UTF-8)', $file |
blessed hashref |
Moo class with types |
$1, $2, $3 |
$+{name} (named captures) |
eval { }; if ($@) |
Try::Tiny or native try/catch (5.40+) |
BEGIN { require Exporter; } |
use Exporter 'import'; |
| Manual file ops |
Path::Tiny |
blessed($o) && $o->isa('X') |
$o isa 'X' (5.32+) |
builtin::true / false |
use builtin 'true', 'false'; (5.36+, experimental) |
Anti-Patterns
# 1. Two-arg open (security risk)
open FH, $filename; # NEVER
# 2. Indirect object syntax (ambiguous parsing)
1---2name: perl-patterns3description: "Use when modern Perl 5.36+ idioms, best practices, and conventions for building robust, maintainable Perl applications. Only for Perl — not for other languages. Triggers on \"perl-patterns\", \"perl patterns\", \"patterns\"."4metadata:5 source: https://skillmd.com/skills/majinmagros/perl-patterns6 origin: ECC7---89# Modern Perl Development Patterns1011Idiomatic Perl 5.36+ patterns and best practices for building robust, maintainable applications.1213## When to Activate1415- Writing new Perl code or modules16- Reviewing Perl code for idiom compliance17- Refactoring legacy Perl to modern standards18- Designing Perl module architecture19- Migrating pre-5.36 code to modern Perl2021## When NOT to Use2223- Other languages (use `python-patterns`, `golang-patterns`, etc.)24- Perl security review specifically (use `perl-security`)2526## How It Works2728Apply 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.2930## Contents3132| Topic | Reference |33|---|---|34| Core principles (pragma, signatures, context, deref, `isa`) | `references/core-principles.md` |35| Error handling (eval/die, Try::Tiny, try/catch) | `references/error-handling.md` |36| Modern OO (Moo, roles, native class) | `references/modern-oo.md` |37| Regular expressions | `references/regex.md` |38| Data structures and references | `references/data-structures.md` |39| File I/O (three-arg open, Path::Tiny) | `references/file-io.md` |40| Module organization and Exporter | `references/modules.md` |41| Tooling (perltidy, perlcritic, carton) | `references/tooling.md` |4243## Quick Reference: Modern Perl Idioms4445| Legacy Pattern | Modern Replacement |46|---|---|47| `use strict; use warnings;` | `use v5.36;` |48| `my ($x, $y) = @_;` | `sub foo($x, $y) { ... }` |49| `@{ $ref }` | `$ref->@*` |50| `%{ $ref }` | `$ref->%*` |51| `open FH, "< $file"` | `open my $fh, '<:encoding(UTF-8)', $file` |52| `blessed hashref` | `Moo` class with types |53| `$1, $2, $3` | `$+{name}` (named captures) |54| `eval { }; if ($@)` | `Try::Tiny` or native `try/catch` (5.40+) |55| `BEGIN { require Exporter; }` | `use Exporter 'import';` |56| Manual file ops | `Path::Tiny` |57| `blessed($o) && $o->isa('X')` | `$o isa 'X'` (5.32+) |58| `builtin::true / false` | `use builtin 'true', 'false';` (5.36+, experimental) |5960## Anti-Patterns6162```perl63# 1. Two-arg open (security risk)64open FH, $filename; # NEVER6566# 2. Indirect object syntax (ambiguous parsing)