PE

Perl Moose/Moo OOP Architecture

Structuring Perl applications with Moose or Moo for typed, role-based object-oriented design

Details

Language / Topic
perlPerl
Category
Architecture

Rules

balanced
- Use `Moo` for lightweight OOP and `Moose` for full meta-object protocol features — define attributes with `has 'name' => (is => 'ro', isa => Str, required => 1)`.
- Use `Role::Tiny` (with `Moo`) or `Moose::Role` to define composable behaviors — prefer roles over inheritance for shared functionality.
- Use `Type::Tiny` or `Moose` built-in type constraints for attribute validation — define domain types in a `Types.pm` module.
- Declare all attributes with `is => 'ro'` (immutable) by default — use `is => 'rw'` only when mutability is explicitly required.
- Use `BUILD` for post-construction validation and `BUILDARGS` to normalize constructor arguments before attribute assignment.
- Decompose large classes into focused roles (`Role::Serializable`, `Role::Loggable`) and compose them with `with 'Role::Name'`.
- Use `MooseX::Types::Structured` or `Type::Tiny::Struct` to declare parameterized types for complex attribute values.