rules/perl/patterns.md
This file extends common/patterns.md with Perl-specific content.
Use DBI or DBIx::Class behind an interface:
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;
}
Use Moo classes with Types::Standard (equivalent to 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);
autodieuse autodie;
use Path::Tiny;
my $content = path('config.json')->slurp_utf8;
Use Exporter 'import' with @EXPORT_OK — never @EXPORT:
use Exporter 'import';
our @EXPORT_OK = qw(parse_config validate_input);
Use cpanfile + carton for reproducible installs:
carton install
carton exec prove -lr t/
See skill: perl-patterns for comprehensive modern Perl patterns and idioms.