{
    "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-10-06T07:26:42Z",
    "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( signature );\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 = signature( method => 1, positional => [ $Years ] );\nmy ( $self, $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() function,\nand the input validation mechanism for Object::Accessor all work in the same way, which is\nbasically a limited pure-Perl implementation of the smart match operator. While this doesn't\ndirectly support Type::Tiny constraints, it does support coderefs. You can use Type::Tiny's\n\"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"
                },
                {
                    "name": "Class::Plain",
                    "content": "There is not currently a high level of integration, but here's a quick example of type checking\nattributes in the constructor.\n\nIf any of your accessors are \":rw\" then you would also need to add type checks to those.\n\nuse Class::Plain;\n\nclass Point {\nuse Types::Common -types, -sigs;\n\nfield x :reader;\nfield y :reader;\n\nsignaturefor new => (\nmethod => !!1,\nbless  => !!0,\nnamed  => [\nx => Int,\ny => Int,\n],\n);\n\nmethod asarrayref () {\nreturn [ $self->x, $self->y ];\n}\n}\n\nThe following signature may also be of interest:\n\nsignaturefor new => (\nmethod   => !!1,\nmultiple => [\n{\nnamed => [\nx => Int,\ny => Int,\n],\nbless => !!0,\n},\n{\npositional => [ Int, Int ],\ngotonext  => sub {\nmy ( $class, $x, $y ) = @;\nreturn ( $class, { x => $x, y => $y } ),\n},\n},\n],\n);\n\nThis would allow your class to be instantiated using any of the following:\n\nmy $point11 = Point->new( { x => 1, y => 1 } );\nmy $point22 = Point->new(   x => 2, y => 2   );\nmy $point33 = Point->new( 3, 3 );\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-2023 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": []
}