{
    "content": [
        {
            "type": "text",
            "text": "# Type::Tiny::Manual::UsingWithClassTiny (perldoc)\n\n## NAME\n\nType::Tiny::Manual::UsingWithClassTiny - use of Type::Tiny with Class::Tiny\n\n## Sections\n\n- **NAME**\n- **MANUAL** (2 subsections)\n- **NEXT STEPS**\n- **AUTHOR**\n- **COPYRIGHT AND LICENCE**\n- **DISCLAIMER OF WARRANTIES**\n\nUse structuredContent.sections for detailed options, examples, and full documentation.\n"
        }
    ],
    "structuredContent": {
        "command": "Type::Tiny::Manual::UsingWithClassTiny",
        "section": "",
        "mode": "perldoc",
        "summary": "Type::Tiny::Manual::UsingWithClassTiny - use of Type::Tiny with Class::Tiny",
        "synopsis": null,
        "tldr_summary": null,
        "tldr_examples": [],
        "tldr_source": null,
        "flags": [],
        "examples": [],
        "see_also": [],
        "section_outline": [
            {
                "name": "NAME",
                "lines": 2,
                "subsections": []
            },
            {
                "name": "MANUAL",
                "lines": 67,
                "subsections": [
                    {
                        "name": "Class::Tiny::ConstrainedAccessor",
                        "lines": 18
                    },
                    {
                        "name": "Class::Tiny::Antlers",
                        "lines": 22
                    }
                ]
            },
            {
                "name": "NEXT STEPS",
                "lines": 6,
                "subsections": []
            },
            {
                "name": "AUTHOR",
                "lines": 2,
                "subsections": []
            },
            {
                "name": "COPYRIGHT AND LICENCE",
                "lines": 5,
                "subsections": []
            },
            {
                "name": "DISCLAIMER OF WARRANTIES",
                "lines": 4,
                "subsections": []
            }
        ],
        "sections": {
            "NAME": {
                "content": "Type::Tiny::Manual::UsingWithClassTiny - use of Type::Tiny with Class::Tiny\n",
                "subsections": []
            },
            "MANUAL": {
                "content": "Class::Tiny is an even-smaller-than-Moo class builder.\n\nLet's translate the classic Horse class from Moo to Class::Tiny.\n\nMoo:\n\npackage Horse {\nuse Moo;\nuse Types::Standard qw( Str Num ArrayRef );\nuse namespace::autoclean;\n\nhas name       => ( is => 'ro', isa => Str, required => 1 );\nhas gender     => ( is => 'ro', isa => Str );\nhas age        => ( is => 'rw', isa => Num );\nhas children   => (\nis       => 'ro',\nisa      => ArrayRef,\ndefault  => sub { return [] },\n);\n}\n\nClass::Tiny:\n\npackage Horse {\nuse Class::Tiny qw( gender age ), {\nname     => sub { die \"name is required\"; },\nchildren => sub { return [] },\n};\nuse Types::Standard qw( Str Num ArrayRef Dict Optional slurpy Any);\nuse Type::Params qw( wrapmethods compile );\nuse namespace::autoclean;\n\n# type checks\nwrapmethods(\nBUILD    => [Dict[\nname       => Str,\ngender     => Optional[Str],\nage        => Optional[Num],\nchildren   => Optional[ArrayRef],\nslurpy Any,\n]],\nname     => [],\ngender   => [],\nage      => Optional[Num],\nchildren => [],\n);\n}\n\nWhat's going on here?\n\nWell, Class::Tiny, after it has built a new object, will do this:\n\n$self->BUILD($args);\n\n(Technically, it calls \"BUILD\" not just for the current class, but for all parent classes too.)\nWe can hook onto this in order to check type constraints for the constructor.\n\nWe use \"wrapmethods\" from Type::Params to wrap the original \"BUILD\" method (which doesn't\nexist, so \"wrapmethods\" will just assume an empty sub) with a type check for $args. The type\ncheck is just a Dict that checks the class's required and optional attributes and includes\nslurpy Any at the end to be flexible for subclasses adding new attributes.\n\nThen we wrap the \"name\", \"gender\", and \"children\" methods with checks to make sure they're only\nbeing called as getters, and we wrap \"age\", allowing it to be called as a setter with a Num.\n\nThere are also a couple of CPAN modules that can help you out.\n",
                "subsections": [
                    {
                        "name": "Class::Tiny::ConstrainedAccessor",
                        "content": "Class::Tiny::ConstrainedAccessor creates a \"BUILD\" and accessors that enforce Type::Tiny\nconstraints. Attribute types are passed to Class::Tiny::ConstrainedAccessor; attribute defaults\nare passed to Class::Tiny.\n\npackage Horse {\nuse Types::Standard qw( Str Num ArrayRef );\nuse Class::Tiny::ConstrainedAccessor {\nname     => Str,\ngender   => Str,\nage      => Num,\nchildren => ArrayRef,\n};\nuse Class::Tiny qw( gender age ), {\nname     => sub { die \"name is required\"; },\nchildren => sub { return [] },\n};\n}\n"
                    },
                    {
                        "name": "Class::Tiny::Antlers",
                        "content": "Class::Tiny::Antlers provides Moose-like syntax for Class::Tiny, including support for \"isa\".\nYou do not also need to use Class::Tiny itself.\n\npackage Horse {\nuse Class::Tiny::Antlers qw(has);\nuse Types::Standard qw( Str Num ArrayRef );\nuse namespace::autoclean;\n\nhas name       => (\nis        => 'ro',\nisa       => Str,\ndefault   => sub { die \"name is required\" },\n);\nhas gender     => ( is => 'ro',    isa => Str );\nhas age        => ( is => 'rw',    isa => Num );\nhas children   => (\nis        => 'ro',\nisa       => ArrayRef,\ndefault   => sub { return [] },\n);\n}\n"
                    }
                ]
            },
            "NEXT STEPS": {
                "content": "Here's your next step:\n\n*   Type::Tiny::Manual::UsingWithOther\n\nUsing Type::Tiny with Class::InsideOut, Params::Check, and Object::Accessor.\n",
                "subsections": []
            },
            "AUTHOR": {
                "content": "Toby Inkster <tobyink@cpan.org>.\n",
                "subsections": []
            },
            "COPYRIGHT AND LICENCE": {
                "content": "This software is copyright (c) 2013-2014, 2017-2021 by Toby Inkster.\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": []
            },
            "DISCLAIMER OF WARRANTIES": {
                "content": "THIS PACKAGE IS PROVIDED \"AS IS\" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,\nWITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR\nPURPOSE.\n",
                "subsections": []
            }
        }
    }
}