{
    "mode": "perldoc",
    "parameter": "Type::Tiny::Manual::Params",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/perldoc/Type%3A%3ATiny%3A%3AManual%3A%3AParams/json",
    "generated": "2026-06-12T05:48:09Z",
    "sections": {
        "NAME": {
            "content": "Type::Tiny::Manual::Params - advanced information on Type::Params\n",
            "subsections": []
        },
        "MANUAL": {
            "content": "To get started with Type::Params, please read Type::Tiny::Manual::UsingWithMoo which will cover\na lot of the basics, even if you're not using Moo.\n\n\"validate\" and \"validatenamed\"\nThe generally recommended way of using Type::Params is this:\n\nsub mysub {\nstate $check = compile( SIGNATURE );\nmy @args = $check->( @ );\n}\n\nBut it is possible to do it in one call:\n\nsub mysub {\nmy @args = validate( \\@, SIGNATURE );\n}\n\nThere is also a \"validatenamed\" function which acts as a counterpart for \"compilenamed\".\n\nThis will generally be slower and less efficient than using \"compile\" first because Type::Tiny\ncan do a lot of optimizations in that first stage to make the second stage a lot faster. (And\nthe results of \"compile\" get stored in the \"state\" variable so that only has to happen once.)\n\nThere is rarely a reason to use \"validate\" and \"validatenamed\", but they exist if you want\nthem.\n\n\"multisig\"\nMultisig allows you to allow multiple ways of calling a sub.\n\nsub repeatstring {\nstate $check = multisig(\ncompile(\nInt,\nStr,\n),\ncompilenamed(\n{ namedtolist => 1 },\ncount  => Int,\nstring => Str,\n),\n);\n\nmy ($count, $string) = $check->(@);\nreturn $string x $count;\n}\n\nrepeatstring(            \"Hello\",          42  );    # works\nrepeatstring(  string => \"Hello\", count => 42  );    # works\nrepeatstring({ string => \"Hello\", count => 42 });    # works\nrepeatstring( qr/hiya/ );                            # dies\n\nIt combines multiple checks and tries each until one works.\n\n\"wrapsubs\" and \"wrapmethods\"\n\"wrapsubs\" turns the \"compile\" idea inside out.\n\nInstead of this:\n\nsub foobar {\nstate $check = compile(Int, Str);\nmy ($foo, $bar) = @;\n...;\n}\n\nYou do this:\n\nsub foobar {\nmy ($foo, $bar) = @;\n...;\n}\nwrapsubs foobar => [ Int, Str ];\n\nOr this:\n\nsub foobar {\nmy ($foo, $bar) = @;\n...;\n}\nwrapsubs foobar => compile( Int, Str );\n",
            "subsections": [
                {
                    "name": "Mixed Named and Positional Parameters",
                    "content": "This can be faked using positional parameters and a slurpy dictionary.\n\nstate $check = compile(\nInt,\nslurpy Dict[\nfoo => Int,\nbar => Optional[Int],\nbaz => Optional[Int],\n],\n);\n\n@ = (42, foo => 21);                 # ok\n@ = (42, foo => 21, bar  => 84);     # ok\n@ = (42, foo => 21, bar  => 10.5);   # not ok\n@ = (42, foo => 21, quux => 84);     # not ok\n\nFrom Type::Params 1.009002, \"head\" and \"tail\" options are accepted, which provide another\noption for mixed named and positional arguments:\n\nstate $check = compilenamed(\n{ head => [ Int ] },\nfoo => Int,\nbar => Optional[Int],\nbaz => Optional[Int],\n],\n);\n\nThe \"head\" is shifted off @ before @ is considered as a hash. The \"tail\" is popped off @\nbefore @ is considered as a hash.\n"
                },
                {
                    "name": "Proper Signatures",
                    "content": "Don't you wish your subs could look like this?\n\nsub setname (Object $self, Str $name) {\n$self->{name} = $name;\n}\n\nWell; here are a few solutions for sub signatures that work with Type::Tiny...\n\nZydeco\nZydeco is a Perl OO syntax toolkit with Type::Tiny support baked in throughout.\n\npackage MyApp {\nuse Zydeco;\n\nclass Person {\nhas name ( type => Str );\n\nmethod rename (Str $newname) {\nprintf(\"%s will now be called %s\\n\", $self->name, $newname);\n$self->name($newname);\n}\n\ncoerce from Str via {\n$class->new(name => $)\n}\n}\n\nclass Company {\nhas owner ( type => 'Person' );\n}\n}\n\nmy $acme = MyApp->newcompany(owner => \"Robert\");\n$acme->owner->rename(\"Bob\");\n\nKavorka\nKavorka is a sub signatures implementation written to natively use Type::Utils' \"dwimtype\" for\ntype constraints, and take advantage of Type::Tiny's features such as inlining, and coercions.\n\nmethod setname (Str $name) {\n$self->{name} = $name;\n}\n\nKavorka's signatures provide a lot more flexibility, and slightly more speed than Type::Params.\n(The speed comes from inlining almost all type checks into the body of the sub being declared.)\n\nKavorka also includes support for type checking of the returned value.\n\nKavorka can also be used as part of Moops, a larger framework for object oriented programming in\nPerl.\n\nFunction::Parameters\nFunction::Parameters offers support for Type::Tiny and MooseX::Types.\n\nuse Types::Standard qw( Str );\nuse Function::Parameters;\n\nmethod setname (Str $name) {\n$self->{name} = $name;\n}\n\nAttribute::Contract\nBoth Kavorka and Function::Parameters require a relatively recent version of Perl.\nAttribute::Contract supports older versions by using a lot less magic.\n\nYou want Attribute::Contract 0.03 or above.\n\nuse Attribute::Contract -types => [qw/Object Str/];\n\nsub setname :ContractRequires(Object, Str) {\nmy ($self, $name) = @;\n$self->{name} = $name;\n}\n\nAttribute::Contract also includes support for type checking of the returned value.\n"
                },
                {
                    "name": "Type::Params versus X",
                    "content": "Params::Validate\nType::Params is not really a drop-in replacement for Params::Validate; the API differs far too\nmuch to claim that. Yet it performs a similar task, so it makes sense to compare them.\n\n*   Type::Params will tend to be faster if you've got a sub which is called repeatedly, but may\nbe a little slower than Params::Validate for subs that are only called a few times. This is\nbecause it does a bunch of work the first time your sub is called to make subsequent calls a\nlot faster.\n\n*   Params::Validate doesn't appear to have a particularly natural way of validating a mix of\npositional and named parameters.\n\n*   Type::Utils allows you to coerce parameters. For example, if you expect a Path::Tiny object,\nyou could coerce it from a string.\n\n*   If you are primarily writing object-oriented code, using Moose or similar, and you are using\nType::Tiny type constraints for your attributes, then using Type::Params allows you to use\nthe same constraints for method calls.\n\n*   Type::Params comes bundled with Types::Standard, which provides a much richer vocabulary of\ntypes than the type validation constants that come with Params::Validate. For example,\nTypes::Standard provides constraints like \"ArrayRef[Int]\" (an arrayref of integers), while\nthe closest from Params::Validate is \"ARRAYREF\", which you'd need to supplement with\nadditional callbacks if you wanted to check that the arrayref contained integers.\n\nWhatsmore, Type::Params doesn't just work with Types::Standard, but also any other\nType::Tiny type constraints.\n\nParams::ValidationCompiler\nParams::ValidationCompiler does basically the same thing as Type::Params.\n\n*   Params::ValidationCompiler and Type::Params are likely to perform fairly similarly. In most\ncases, recent versions of Type::Params seem to be *slightly* faster, but except in very\ntrivial cases, you're unlikely to notice the speed difference. Speed probably shouldn't be a\nfactor when choosing between them.\n\n*   Type::Params's syntax is more compact:\n\nstate $check = compile(Object, Optional[Int], slurpy ArrayRef);\n\nVersus:\n\nstate $check = validationfor(\nparams => [\n{ type => Object },\n{ type => Int,      optional => 1 },\n{ type => ArrayRef, slurpy => 1 },\n],\n);\n\n*   Params::ValidationCompiler probably has slightly better exceptions.\n"
                }
            ]
        },
        "NEXT STEPS": {
            "content": "Here's your next step:\n\n*   Type::Tiny::Manual::NonOO\n\nType::Tiny in non-object-oriented code.\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::Params - advanced information on Type::Params",
    "flags": [],
    "examples": [],
    "see_also": []
}