{
    "mode": "perldoc",
    "parameter": "Moose::Manual::Unsweetened",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/perldoc/Moose%3A%3AManual%3A%3AUnsweetened/json",
    "generated": "2026-06-16T10:16:30Z",
    "sections": {
        "NAME": {
            "content": "Moose::Manual::Unsweetened - Moose idioms in plain old Perl 5 without the sugar\n",
            "subsections": []
        },
        "VERSION": {
            "content": "version 2.2200\n",
            "subsections": []
        },
        "DESCRIPTION": {
            "content": "If you're trying to figure out just what the heck Moose does, and how it saves you time, you\nmight find it helpful to see what Moose is *really* doing for you. This document shows you the\ntranslation from Moose sugar back to plain old Perl 5.\n",
            "subsections": []
        },
        "CLASSES AND ATTRIBUTES": {
            "content": "First, we define two very small classes the Moose way.\n\npackage Person;\n\nuse DateTime;\nuse DateTime::Format::Natural;\nuse Moose;\nuse Moose::Util::TypeConstraints;\n\nhas name => (\nis       => 'rw',\nisa      => 'Str',\nrequired => 1,\n);\n\n# Moose doesn't know about non-Moose-based classes.\nclasstype 'DateTime';\n\nmy $enparser = DateTime::Format::Natural->new(\nlang      => 'en',\ntimezone => 'UTC',\n);\n\ncoerce 'DateTime'\n=> from 'Str'\n=> via { $enparser->parsedatetime($) };\n\nhas birthdate => (\nis      => 'rw',\nisa     => 'DateTime',\ncoerce  => 1,\nhandles => { birthyear => 'year' },\n);\n\nenum 'ShirtSize' => [qw( s m l xl xxl )];\n\nhas shirtsize => (\nis      => 'rw',\nisa     => 'ShirtSize',\ndefault => 'l',\n);\n\nThis is a fairly simple class with three attributes. We also define an enum type to validate\nt-shirt sizes because we don't want to end up with something like \"blue\" for the shirt size!\n\npackage User;\n\nuse Email::Valid;\nuse Moose;\nuse Moose::Util::TypeConstraints;\n\nextends 'Person';\n\nsubtype 'Email'\n=> as 'Str'\n=> where { Email::Valid->address($) }\n=> message { \"$ is not a valid email address\" };\n\nhas emailaddress => (\nis       => 'rw',\nisa      => 'Email',\nrequired => 1,\n);\n\nThis class subclasses Person to add a single attribute, email address.\n\nNow we will show what these classes would look like in plain old Perl 5. For the sake of\nargument, we won't use any base classes or any helpers like \"Class::Accessor\".\n\npackage Person;\n\nuse strict;\nuse warnings;\n\nuse Carp qw( confess );\nuse DateTime;\nuse DateTime::Format::Natural;\n\nsub new {\nmy $class = shift;\nmy %p = ref $[0] ? %{ $[0] } : @;\n\nexists $p{name}\nor confess 'name is a required attribute';\n$class->validatename( $p{name} );\n\nexists $p{birthdate}\nor confess 'birthdate is a required attribute';\n\n$p{birthdate} = $class->coercebirthdate( $p{birthdate} );\n$class->validatebirthdate( $p{birthdate} );\n\n$p{shirtsize} = 'l'\nunless exists $p{shirtsize};\n\n$class->validateshirtsize( $p{shirtsize} );\n\nreturn bless \\%p, $class;\n}\n\nsub validatename {\nshift;\nmy $name = shift;\n\nlocal $Carp::CarpLevel = $Carp::CarpLevel + 1;\n\ndefined $name\nor confess 'name must be a string';\n}\n\n{\nmy $enparser = DateTime::Format::Natural->new(\nlang      => 'en',\ntimezone => 'UTC',\n);\n\nsub coercebirthdate {\nshift;\nmy $date = shift;\n\nreturn $date unless defined $date && ! ref $date;\n\nmy $dt = $enparser->parsedatetime($date);\n\nreturn $dt ? $dt : undef;\n}\n}\n\nsub validatebirthdate {\nshift;\nmy $birthdate = shift;\n\nlocal $Carp::CarpLevel = $Carp::CarpLevel + 1;\n\n$birthdate->isa('DateTime')\nor confess 'birthdate must be a DateTime object';\n}\n\nsub validateshirtsize {\nshift;\nmy $shirtsize = shift;\n\nlocal $Carp::CarpLevel = $Carp::CarpLevel + 1;\n\ndefined $shirtsize\nor confess 'shirtsize cannot be undef';\n\nmy %sizes = map { $ => 1 } qw( s m l xl xxl );\n\n$sizes{$shirtsize}\nor confess \"$shirtsize is not a valid shirt size (s, m, l, xl, xxl)\";\n}\n\nsub name {\nmy $self = shift;\n\nif (@) {\n$self->validatename( $[0] );\n$self->{name} = $[0];\n}\n\nreturn $self->{name};\n}\n\nsub birthdate {\nmy $self = shift;\n\nif (@) {\nmy $date = $self->coercebirthdate( $[0] );\n$self->validatebirthdate( $date );\n\n$self->{birthdate} = $date;\n}\n\nreturn $self->{birthdate};\n}\n\nsub birthyear {\nmy $self = shift;\n\nreturn $self->birthdate->year;\n}\n\nsub shirtsize {\nmy $self = shift;\n\nif (@) {\n$self->validateshirtsize( $[0] );\n$self->{shirtsize} = $[0];\n}\n\nreturn $self->{shirtsize};\n}\n\nWow, that was a mouthful! One thing to note is just how much space the data validation code\nconsumes. As a result, it's pretty common for Perl 5 programmers to just not bother.\nUnfortunately, not validating arguments leads to surprises down the line (\"why is birthdate an\nemail address?\").\n\nAlso, did you spot the (intentional) bug?\n\nIt's in the \"validatebirthdate()\" method. We should check that the value in $birthdate is\nactually defined and an object before we go and call \"isa()\" on it! Leaving out those checks\nmeans our data validation code could actually cause our program to die. Oops.\n\nNote that if we add a superclass to Person we'll have to change the constructor to account for\nthat.\n\n(As an aside, getting all the little details of what Moose does for you just right in this\nexample was really not easy, which emphasizes the point of the example. Moose saves you a lot of\nwork!)\n\nNow let's see User:\n\npackage User;\n\nuse strict;\nuse warnings;\n\nuse Carp qw( confess );\nuse Email::Valid;\nuse Scalar::Util qw( blessed );\n\nuse parent 'Person';\n\nsub new {\nmy $class = shift;\nmy %p = ref $[0] ? %{ $[0] } : @;\n\nexists $p{emailaddress}\nor confess 'emailaddress is a required attribute';\n$class->validateemailaddress( $p{emailaddress} );\n\nmy $self = $class->SUPER::new(%p);\n\n$self->{emailaddress} = $p{emailaddress};\n\nreturn $self;\n}\n\nsub validateemailaddress {\nshift;\nmy $emailaddress = shift;\n\nlocal $Carp::CarpLevel = $Carp::CarpLevel + 1;\n\ndefined $emailaddress\nor confess 'emailaddress must be a string';\n\nEmail::Valid->address($emailaddress)\nor confess \"$emailaddress is not a valid email address\";\n}\n\nsub emailaddress {\nmy $self = shift;\n\nif (@) {\n$self->validateemailaddress( $[0] );\n$self->{emailaddress} = $[0];\n}\n\nreturn $self->{emailaddress};\n}\n\nThat one was shorter, but it only has one attribute.\n\nBetween the two classes, we have a whole lot of code that doesn't do much. We could probably\nsimplify this by defining some sort of \"attribute and validation\" hash, like this:\n\npackage Person;\n\nmy %Attr = (\nname => {\nrequired => 1,\nvalidate => sub { defined $ },\n},\nbirthdate => {\nrequired => 1,\nvalidate => sub { blessed $ && $->isa('DateTime') },\n},\nshirtsize => {\nrequired => 1,\nvalidate => sub { defined $ && $ =~ /^(?:s|m|l|xl|xxl)$/i },\n}\n);\n\nThen we could define a base class that would accept such a definition and do the right thing.\nKeep that sort of thing up and we're well on our way to writing a half-assed version of Moose!\n\nOf course, there are CPAN modules that do some of what Moose does, like \"Class::Accessor\",\n\"Class::Meta\", and so on. But none of them put together all of Moose's features along with a\nlayer of declarative sugar, nor are these other modules designed for extensibility in the same\nway as Moose. With Moose, it's easy to write a MooseX module to replace or extend a piece of\nbuilt-in functionality.\n\nMoose is a complete OO package in and of itself, and is part of a rich ecosystem of extensions.\nIt also has an enthusiastic community of users and is being actively maintained and developed.\n",
            "subsections": []
        },
        "AUTHORS": {
            "content": "*   Stevan Little <stevan@cpan.org>\n\n*   Dave Rolsky <autarch@urth.org>\n\n*   Jesse Luehrs <doy@cpan.org>\n\n*   Shawn M Moore <sartak@cpan.org>\n\n*   יובל קוג'מן (Yuval Kogman) <nothingmuch@woobling.org>\n\n*   Karen Etheridge <ether@cpan.org>\n\n*   Florian Ragwitz <rafl@debian.org>\n\n*   Hans Dieter Pearcey <hdp@cpan.org>\n\n*   Chris Prather <chris@prather.org>\n\n*   Matt S Trout <mstrout@cpan.org>\n",
            "subsections": []
        },
        "COPYRIGHT AND LICENSE": {
            "content": "This software is copyright (c) 2006 by Infinity Interactive, Inc.\n\nThis is free software; you can redistribute it and/or modify it under the same terms as the Perl\n5 programming language system itself.\n",
            "subsections": []
        }
    },
    "summary": "Moose::Manual::Unsweetened - Moose idioms in plain old Perl 5 without the sugar",
    "flags": [],
    "examples": [],
    "see_also": []
}