{
    "mode": "perldoc",
    "parameter": "Type::Tiny::Manual::UsingWithMoose",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/perldoc/Type%3A%3ATiny%3A%3AManual%3A%3AUsingWithMoose/json",
    "generated": "2026-06-12T06:21:03Z",
    "sections": {
        "NAME": {
            "content": "Type::Tiny::Manual::UsingWithMoose - how to use Type::Tiny with Moose\n",
            "subsections": []
        },
        "MANUAL": {
            "content": "First read Type::Tiny::Manual::Moo, Type::Tiny::Manual::Moo2, and Type::Tiny::Manual::Moo3.\nEverything in those parts of the manual should work exactly the same in Moose.\n\nThis part of the manual will focus on Moose-specifics.\n\nWhy Use Type::Tiny At All?\nMoose does have a built-in type constraint system which is fairly convenient to use, but there\nare several reasons you should consider using Type::Tiny instead.\n\n*   Type::Tiny type constraints will usually be faster than Moose built-ins. Even without\nType::Tiny::XS installed, Type::Tiny usually produces more efficient inline code than Moose.\nCoercions will usually be a lot faster.\n\n*   Type::Tiny provides helpful methods like \"where\" and \"pluscoercions\" that allow type\nconstraints and coercions to be easily tweaked on a per-attribute basis.\n\nSomething like this is much harder to do with plain Moose types:\n\nhas name => (\nis      => \"ro\",\nisa     => Str->pluscoercions(\nArrayRef[Str], sub { join \" \", @$ },\n),\ncoerce  => 1,\n);\n\nMoose tends to encourage defining coercions globally, so if you wanted one Str attribute to\nbe able to coerce from ArrayRef[Str], then *all* Str attributes would coerce from\nArrayRef[Str], and they'd all do that coercion in the same way. (Even if it might make sense\nto join by a space in some places, a comma in others, and a line break in others!)\n\n*   Type::Tiny provides automatic deep coercions, so if type Xyz has a coercion, the following\nshould \"just work\":\n\nisa xyzlist => ( is => 'ro', isa => ArrayRef[Xyz], coerce => 1 );\n\n*   Type::Tiny offers a wider selection of built-in types.\n\n*   By using Type::Tiny, you can use the same type constraints and coercions for attributes and\nmethod parameters, in Moose and non-Moose code.\n",
            "subsections": [
                {
                    "name": "Type::Utils",
                    "content": "If you've used Moose::Util::TypeConstraints, you may be accustomed to using a DSL for declaring\ntype constraints:\n\nuse Moose::Util::TypeConstraints;\n\nsubtype 'Natural',\nas 'Int',\nwhere { $ > 0 };\n\nThere's a module called Type::Utils that provides a very similar DSL for declaring types in\nType::Library-based type libraries.\n\npackage My::Types {\nuse Type::Library -base;\nuse Type::Utils;\nuse Types::Standard qw( Int );\n\ndeclare 'Natural',\nas Int,\nwhere { $ > 0 };\n}\n\nPersonally I prefer the more object-oriented way to declare types though.\n\nSince Type::Library 1.012, a shortcut has been available for importing Type::Library and\nType::Utils at the same time:\n\npackage MyType {\nuse Type::Library -base, -utils;\n\n...;\n}\n\nIn Moose you might also declare types like this within classes and roles too. Unlike Moose,\nType::Tiny doesn't keep types in a single global flat namespace, so this doesn't work quite the\nsame with Type::Utils. It still creates the type, but it doesn't store it in any type library;\nthe type is returned.\n\npackage My::Class {\nuse Moose;\nuse Type::Utils;\nuse Types::Standard qw( Int );\n\nmy $Natural =          # store type in a variable\ndeclare 'Natural',\nas Int,\nwhere { $ > 0 };\n\nhas number => ( is => 'ro', isa => $Natural );\n}\n\nBut really, isn't the object-oriented way cleaner?\n\npackage My::Class {\nuse Moose;\nuse Types::Standard qw( Int );\n\nhas number => (\nis   => 'ro',\nisa  => Int->where('$ > 0'),\n);\n}\n"
                },
                {
                    "name": "Type::Tiny and MooseX::Types",
                    "content": "Types::Standard should be a drop-in replacement for MooseX::Types. And Types::Common::Numeric\nand Types::Common::String should easily replace MooseX::Types::Common::Numeric and\nMooseX::Types::Common::String.\n\nThat said, if you do with to use a mixture of Type::Tiny and MooseX::Types, they should fit\ntogether pretty seamlessly.\n\nuse Types::Standard qw( ArrayRef );\nuse MooseX::Types::Common::Numeric qw( PositiveInt );\n\n# this should just work\nmy $listofnums = ArrayRef[PositiveInt];\n\n# and this\nmy $listornum = ArrayRef | PositiveInt;\n\n\"-moose\" Import Parameter\nIf you have read this far in the manual, you will know that this is the usual way to import type\nconstraints:\n\nuse Types::Standard qw( Int );\n\nAnd the \"Int\" which is imported is a function that takes no arguments and returns the Int type\nconstraint, which is a blessed object in the Type::Tiny class.\n\nType::Tiny mocks the Moose::Meta::TypeConstraint API so well that most Moose and MooseX code\nwill not be able to tell the difference.\n\nBut what if you need a real Moose::Meta::TypeConstraint object?\n\nuse Types::Standard -moose, qw( Int );\n\nNow the \"Int\" function imported will return a genuine native Moose type constraint.\n\nThis flag is mostly a throwback from when Type::Tiny native objects *didn't* directly work in\nMoose. In 99.9% of cases, there is no reason to use it and plenty of reasons not to. (Moose\nnative type constraints don't offer helpful methods like \"pluscoercions\" and \"where\".)\n\n\"moosetype\" Method\nAnother quick way to get a native Moose type constraint object from a Type::Tiny object is to\ncall the \"moosetype\" method:\n\nuse Types::Standard qw( Int );\n\nmy $tinytype   = Int;\nmy $moosetype  = $tinytype->moosetype;\n\nInternally, this is what the \"-moose\" flag makes imported functions do.\n"
                }
            ]
        },
        "NEXT STEPS": {
            "content": "Here's your next step:\n\n*   Type::Tiny::Manual::UsingWithMouse\n\nHow to use Type::Tiny with Mouse, including the advantages of Type::Tiny over built-in type\nconstraints, and Mouse-specific features.\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::UsingWithMoose - how to use Type::Tiny with Moose",
    "flags": [],
    "examples": [],
    "see_also": []
}