{
    "content": [
        {
            "type": "text",
            "text": "# Moose::Manual::Unsweetened (perldoc)\n\n## NAME\n\nMoose::Manual::Unsweetened - Moose idioms in plain old Perl 5 without the sugar\n\n## DESCRIPTION\n\nIf you're trying to figure out just what the heck Moose does, and how it\nsaves you time, you might find it helpful to see what Moose is *really*\ndoing for you. This document shows you the translation from Moose sugar\nback to plain old Perl 5.\n\n## Sections\n\n- **NAME**\n- **VERSION**\n- **DESCRIPTION**\n- **CLASSES AND ATTRIBUTES**\n- **AUTHORS**\n- **COPYRIGHT AND LICENSE**\n\nUse structuredContent.sections for detailed options, examples, and full documentation.\n"
        }
    ],
    "structuredContent": {
        "command": "Moose::Manual::Unsweetened",
        "section": "",
        "mode": "perldoc",
        "summary": "Moose::Manual::Unsweetened - Moose idioms in plain old Perl 5 without the sugar",
        "synopsis": null,
        "tldr_summary": null,
        "tldr_examples": [],
        "tldr_source": null,
        "flags": [],
        "examples": [],
        "see_also": [],
        "section_outline": [
            {
                "name": "NAME",
                "lines": 3,
                "subsections": []
            },
            {
                "name": "VERSION",
                "lines": 2,
                "subsections": []
            },
            {
                "name": "DESCRIPTION",
                "lines": 5,
                "subsections": []
            },
            {
                "name": "CLASSES AND ATTRIBUTES",
                "lines": 306,
                "subsections": []
            },
            {
                "name": "AUTHORS",
                "lines": 20,
                "subsections": []
            },
            {
                "name": "COPYRIGHT AND LICENSE",
                "lines": 5,
                "subsections": []
            }
        ],
        "sections": {
            "NAME": {
                "content": "Moose::Manual::Unsweetened - Moose idioms in plain old Perl 5 without\nthe 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\nsaves you time, you might find it helpful to see what Moose is *really*\ndoing for you. This document shows you the translation from Moose sugar\nback 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\nenum type to validate t-shirt sizes because we don't want to end up with\nsomething 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.\nFor the sake of argument, we won't use any base classes or any helpers\nlike \"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\ndata validation code consumes. As a result, it's pretty common for Perl\n5 programmers to just not bother. Unfortunately, not validating\narguments leads to surprises down the line (\"why is birthdate an email\naddress?\").\n\nAlso, did you spot the (intentional) bug?\n\nIt's in the \"validatebirthdate()\" method. We should check that the\nvalue in $birthdate is actually defined and an object before we go and\ncall \"isa()\" on it! Leaving out those checks means our data validation\ncode could actually cause our program to die. Oops.\n\nNote that if we add a superclass to Person we'll have to change the\nconstructor to account for that.\n\n(As an aside, getting all the little details of what Moose does for you\njust right in this example was really not easy, which emphasizes the\npoint of the example. Moose saves you a lot of work!)\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\nmuch. We could probably simplify this by defining some sort of\n\"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\nand do the right thing. Keep that sort of thing up and we're well on our\nway to writing a half-assed version of Moose!\n\nOf course, there are CPAN modules that do some of what Moose does, like\n\"Class::Accessor\", \"Class::Meta\", and so on. But none of them put\ntogether all of Moose's features along with a layer of declarative\nsugar, nor are these other modules designed for extensibility in the\nsame way as Moose. With Moose, it's easy to write a MooseX module to\nreplace or extend a piece of built-in functionality.\n\nMoose is a complete OO package in and of itself, and is part of a rich\necosystem of extensions. It also has an enthusiastic community of users\nand 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\nthe same terms as the Perl 5 programming language system itself.\n",
                "subsections": []
            }
        }
    }
}