Moose::...eetened(3)
β¨ Moose::...eetened(3) β¨
okay bestie, so this page is Moose::...eetened(3) π β it lives in the User Contributed Perl Documentation pile, and it is literally the "show me the receipts" page for Moose. buckle up π
π NAME π
Moose::Manual::Unsweetened β Moose idioms in plain old Perl 5 without the sugar π¬π«
πΈ VERSION πΈ
version 2.4000
π¦ DESCRIPTION π¦
so like. if you're sitting there going "what the heck does Moose even do" and "how is it saving me time", this doc is for you, fr fr. it shows you what Moose is actually doing under the hood for you. it's a straight-up translation from Moose sugar back into plain old Perl 5. no cap, no magic, just vibes and also a lot of confess calls π
π CLASSES AND ATTRIBUTES π
okay so first, we define two very tiny classes the Moose way. main character energy, minimal code β¨
package Person;
use DateTime;
use DateTime::Format::Natural;
use Moose;
use Moose::Util::TypeConstraints;
has name => (
is => 'rw',
isa => 'Str',
required => 1,
);
# Moose doesn't know about non-Moose-based classes.
class_type 'DateTime';
my $en_parser = DateTime::Format::Natural->new(
lang => 'en',
time_zone => 'UTC',
);
coerce 'DateTime'
=> from 'Str'
=> via { $en_parser->parse_datetime($_) };
has birth_date => (
is => 'rw',
isa => 'DateTime',
coerce => 1,
handles => { birth_year => 'year' },
);
enum 'ShirtSize' => [qw( s m l xl xxl )];
has shirt_size => (
is => 'rw',
isa => 'ShirtSize',
default => 'l',
);
so this is a pretty simple class with three attributes. and we also define an enum type to validate t-shirt sizes, because we absolutely do not want to end up with something like "blue" for the shirt size π that's not a size bestie, that's a colour.
next one:
package User;
use Email::Valid;
use Moose;
use Moose::Util::TypeConstraints;
extends 'Person';
subtype 'Email'
=> as 'Str'
=> where { Email::Valid->address($_) }
=> message { "$_ is not a valid email address" };
has email_address => (
is => 'rw',
isa => 'Email',
required => 1,
);
this class subclasses Person to add exactly one attribute: email address. slay π
π§ the same thing but in plain old Perl 5 π§
now we show what these classes look like with the sugar taken away. and for the sake of argument, we're not using any base classes or any helpers like Class::Accessor. just raw dogging Perl 5 π
package Person;
use strict;
use warnings;
use Carp qw( confess );
use DateTime;
use DateTime::Format::Natural;
sub new {
my $class = shift;
my %p = ref $_[0] ? %{ $_[0] } : @_;
exists $p{name}
or confess 'name is a required attribute';
$class->_validate_name( $p{name} );
exists $p{birth_date}
or confess 'birth_date is a required attribute';
$p{birth_date} = $class->_coerce_birth_date( $p{birth_date} );
$class->_validate_birth_date( $p{birth_date} );
$p{shirt_size} = 'l'
unless exists $p{shirt_size};
$class->_validate_shirt_size( $p{shirt_size} );
return bless \%p, $class;
}
sub _validate_name {
shift;
my $name = shift;
local $Carp::CarpLevel = $Carp::CarpLevel + 1;
defined $name
or confess 'name must be a string';
}
{
my $en_parser = DateTime::Format::Natural->new(
lang => 'en',
time_zone => 'UTC',
);
sub _coerce_birth_date {
shift;
my $date = shift;
return $date unless defined $date && ! ref $date;
my $dt = $en_parser->parse_datetime($date);
return $dt ? $dt : undef;
}
}
sub _validate_birth_date {
shift;
my $birth_date = shift;
local $Carp::CarpLevel = $Carp::CarpLevel + 1;
$birth_date->isa('DateTime')
or confess 'birth_date must be a DateTime object';
}
sub _validate_shirt_size {
shift;
my $shirt_size = shift;
local $Carp::CarpLevel = $Carp::CarpLevel + 1;
defined $shirt_size
or confess 'shirt_size cannot be undef';
my %sizes = map { $_ => 1 } qw( s m l xl xxl );
$sizes{$shirt_size}
or confess "$shirt_size is not a valid shirt size (s, m, l, xl, xxl)";
}
sub name {
my $self = shift;
if (@_) {
$self->_validate_name( $_[0] );
$self->{name} = $_[0];
}
return $self->{name};
}
sub birth_date {
my $self = shift;
if (@_) {
my $date = $self->_coerce_birth_date( $_[0] );
$self->_validate_birth_date( $date );
$self->{birth_date} = $date;
}
return $self->{birth_date};
}
sub birth_year {
my $self = shift;
return $self->birth_date->year;
}
sub shirt_size {
my $self = shift;
if (@_) {
$self->_validate_shirt_size( $_[0] );
$self->{shirt_size} = $_[0];
}
return $self->{shirt_size};
}
wow. that was a whole mouthful π one thing to notice is just how much space the data validation code eats up. like, it's a lot. and because of that, it's pretty common for Perl 5 programmers to just... not bother π which is unfortunately how you get surprises down the line. like, "why is birth_date an email address?" bestie. why.
also. did you spot the (intentional) bug? π
it's in the _validate_birth_date() method. we should check that the value in $birth_date is actually defined and an object before we go and call isa() on it! leaving out those checks means our data validation code could actually cause our program to die. oops π
note that if we add a superclass to Person we'll have to change the constructor to account for that. iykyk.
(as an aside β getting all the little details of what Moose does for you just right in this example was really not easy, which honestly emphasizes the point of the example. Moose saves you a lot of work! β¨)
now let's see User:
package User;
use strict;
use warnings;
use Carp qw( confess );
use Email::Valid;
use Scalar::Util qw( blessed );
use parent 'Person';
sub new {
my $class = shift;
my %p = ref $_[0] ? %{ $_[0] } : @_;
exists $p{email_address}
or confess 'email_address is a required attribute';
$class->_validate_email_address( $p{email_address} );
my $self = $class->SUPER::new(%p);
$self->{email_address} = $p{email_address};
return $self;
}
sub _validate_email_address {
shift;
my $email_address = shift;
local $Carp::CarpLevel = $Carp::CarpLevel + 1;
defined $email_address
or confess 'email_address must be a string';
Email::Valid->address($email_address)
or confess "$email_address is not a valid email address";
}
sub email_address {
my $self = shift;
if (@_) {
$self->_validate_email_address( $_[0] );
$self->{email_address} = $_[0];
}
return $self->{email_address};
}
that one was shorter, but it also only has one attribute, so, you know. math π
between the two classes, we've got a whole lot of code that doesn't really do much. we could probably simplify this by defining some sort of "attribute and validation" hash, like this:
package Person;
my %Attr = (
name => {
required => 1,
validate => sub { defined $_ },
},
birth_date => {
required => 1,
validate => sub { blessed $_ && $_->isa('DateTime') },
},
shirt_size => {
required => 1,
validate => sub { defined $_ && $_ =~ /^(?:s|m|l|xl|xxl)$/i },
}
);
and then we could define a base class that accepts such a definition and does the right thing. keep that sort of thing up and we're well on our way to writing a half-assed version of Moose π iconic honestly.
of course, there are CPAN modules that do some of what Moose does, like Class::Accessor, Class::Meta, and so on. but none of them put together all of Moose's features along with a layer of declarative sugar, and none of these other modules are designed for extensibility in the same way as Moose. with Moose, it's easy to write a MooseX module to replace or extend a piece of built-in functionality. obsessed π«Ά
Moose is a complete OO package in and of itself, and it's part of a rich ecosystem of extensions. it also has an enthusiastic community of users and is being actively maintained and developed. periodt.
π AUTHORS π
the legends themselves, no cap:
- Stevan Little <stevan@cpan.org>
- Dave Rolsky <autarch@urth.org>
- Jesse Luehrs <doy@cpan.org>
- Shawn M Moore <sartak@cpan.org>
- ΧΧΧΧ Χ§ΧΧ'ΧΧ (Yuval Kogman) <nothingmuch@woobling.org>
- Karen Etheridge <ether@cpan.org>
- Florian Ragwitz <rafl@debian.org>
- Hans Dieter Pearcey <hdp@cpan.org>
- Chris Prather <chris@prather.org>
- Matt S Trout <mstrout@cpan.org>
π COPYRIGHT AND LICENSE π
this software is copyright (c) 2006 by Infinity Interactive, Inc.
this is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. slay π
and that's the whole page bestie π¦ this was Moose::...eetened(3) from perl v5.42.0, dated 2025-07-25 β love that for us. mwah π
β¨
π¦ where this page comes from
- fedora 43: perl-Moose 2.4000-4.fc43 Β· GPL-1.0-or-later OR Artistic-1.0-Perl
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms