{
    "mode": "perldoc",
    "parameter": "Type::Tiny::Manual::UsingWithOther",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/perldoc/Type%3A%3ATiny%3A%3AManual%3A%3AUsingWithOther/json",
    "generated": "2026-06-12T06:51:21Z",
    "sections": {
        "NAME": {
            "content": "Type::Tiny::Manual::UsingWithOther - using Type::Tiny with Class::InsideOut, Params::Check, and\nObject::Accessor.\n",
            "subsections": []
        },
        "MANUAL": {
            "content": "The antlers crew aren't the only object-oriented programming toolkits in Perl town. Although\nType::Tiny might have been built with Moose, Mouse, and Moo in mind, it can be used with other\ntoolkits.\n\nThese toolkits are... well... hmm... okay... they exist.\n\nIf you are starting a new project, there's very little reason not to use Class::Tiny, Moo, or\nMoose. So you're probably okay to skip this part of the fine manual and go straight to\nType::Tiny::Manual::UsingWithTestMore.\n",
            "subsections": [
                {
                    "name": "Class::InsideOut",
                    "content": "You want Class::InsideOut 1.13 or above, which has support for blessed and overloaded objects\n(including Type::Tiny type constraints) for the \"gethook\" and \"sethook\" options.\n\npackage Person {\nuse Class::InsideOut qw( public );\nuse Types::Standard qw( Str Int );\nuse Types::Common::Numeric qw( PositiveInt );\nuse Type::Params qw( compile );\n\n# Type checks are really easy.\n# Just supply the type as a set hook.\npublic name => my %name, {\nsethook => Str,\n};\n\n# Define a type that silently coerces negative values\n# to positive. It's silly, but it works as an example!\nmy $Years = PositiveInt->pluscoercions(Int, q{ abs($) });\n\n# Coercions are more annoying, but possible.\npublic age => my %age, {\nsethook => sub { $ = $Years->assertcoerce($) },\n};\n\n# Parameter checking for methods is as expected.\nsub getolder {\nstate $check = compile( $Years );\nmy $self = shift;\nmy ($years) = $check->(@);\n$self->setage($self->age + $years);\n}\n}\n"
                },
                {
                    "name": "Params::Check and Object::Accessor",
                    "content": "The Params::Check \"allow()\" function, the \"allow\" option for the Params::Check \"check()\"\nfunction, and the input validation mechanism for Object::Accessor all work in the same way,\nwhich is basically a limited pure-Perl implementation of the smart match operator. While this\ndoesn't directly support Type::Tiny constraints, it does support coderefs. You can use\nType::Tiny's \"compiledcheck\" method to obtain a suitable coderef.\n\nParam::Check example:\n\nmy $tmpl = {\nname => { allow => Str->compiledcheck },\nage  => { allow => Int->compiledcheck },\n};\ncheck($tmpl, { name => \"Bob\", age => 32 })\nor die Params::Check::lasterror();\n\nObject::Accessor example:\n\nmy $obj = Object::Accessor->new;\n$obj->mkaccessors(\n{ name => Str->compiledcheck },\n{ age  => Int->compiledcheck },\n);\n\n*Caveat:* Object::Accessor doesn't die when a value fails to meet its type constraint; instead\nit outputs a warning to STDERR. This behaviour can be changed by setting\n\"$Object::Accessor::FATAL = 1\".\n"
                },
                {
                    "name": "Class::Struct",
                    "content": "This is proof-of-concept of how Type::Tiny can be used to constrain attributes for\nClass::Struct. It's probably not a good idea to use this in production as it slows down\n\"UNIVERSAL::isa\" globally.\n\nuse Types::Standard -types;\nuse Class::Struct;\n\n{\nmy %MAP;\nmy $origisa = \\&UNIVERSAL::isa;\n*UNIVERSAL::isa = sub {\nreturn $MAP{$1}->check($[0])\nif $[1] =~ /^CLASSSTRUCT::TYPETINY::(.+)$/ && exists $MAP{$1};\ngoto $orig;\n};\nmy $origdn = \\&Type::Tiny::displayname;\n*Type::Tiny::displayname = sub {\nif (caller(1) eq 'Class::Struct') {\n$MAP{$[0]{uniq}} = $[0];\nreturn \"CLASSSTRUCT::TYPETINY::\".$[0]{uniq};\n}\ngoto $origdn;\n};\n}\n\nstruct Person => [ name => Str, age => Int ];\n\nmy $bob = Person->new(\nname => \"Bob\",\nage  => 21,\n);\n\n$bob->name(\"Robert\");   # okay\n$bob->name([]);         # dies\n"
                }
            ]
        },
        "NEXT STEPS": {
            "content": "Here's your next step:\n\n*   Type::Tiny::Manual::UsingWithTestMore\n\nType::Tiny for test suites.\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": []
        }
    },
    "summary": "Type::Tiny::Manual::UsingWithOther - using Type::Tiny with Class::InsideOut, Params::Check, and Object::Accessor.",
    "flags": [],
    "examples": [],
    "see_also": []
}