{
    "content": [
        {
            "type": "text",
            "text": "# Type::Utils (perldoc)\n\n## NAME\n\nType::Utils - utility functions to make defining and using type constraints a little easier\n\n## SYNOPSIS\n\npackage Types::Mine;\nuse Type::Library -base;\nuse Type::Utils -all;\nBEGIN { extends \"Types::Standard\" };\ndeclare \"AllCaps\",\nas \"Str\",\nwhere { uc($) eq $ },\ninlineas { my $varname = $[1]; \"uc($varname) eq $varname\" };\ncoerce \"AllCaps\",\nfrom \"Str\", via { uc($) };\n\n## DESCRIPTION\n\nThis module provides utility functions to make defining and using type constraints a little\neasier.\n\n## Sections\n\n- **NAME**\n- **SYNOPSIS**\n- **STATUS**\n- **DESCRIPTION** (4 subsections)\n- **EXPORT**\n- **BUGS**\n- **SEE ALSO**\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::Utils",
        "section": "",
        "mode": "perldoc",
        "summary": "Type::Utils - utility functions to make defining and using type constraints a little easier",
        "synopsis": "package Types::Mine;\nuse Type::Library -base;\nuse Type::Utils -all;\nBEGIN { extends \"Types::Standard\" };\ndeclare \"AllCaps\",\nas \"Str\",\nwhere { uc($) eq $ },\ninlineas { my $varname = $[1]; \"uc($varname) eq $varname\" };\ncoerce \"AllCaps\",\nfrom \"Str\", via { uc($) };",
        "tldr_summary": null,
        "tldr_examples": [],
        "tldr_source": null,
        "flags": [],
        "examples": [],
        "see_also": [],
        "section_outline": [
            {
                "name": "NAME",
                "lines": 2,
                "subsections": []
            },
            {
                "name": "SYNOPSIS",
                "lines": 15,
                "subsections": []
            },
            {
                "name": "STATUS",
                "lines": 2,
                "subsections": []
            },
            {
                "name": "DESCRIPTION",
                "lines": 3,
                "subsections": [
                    {
                        "name": "Type declaration functions",
                        "lines": 125
                    },
                    {
                        "name": "Coercion declaration functions",
                        "lines": 41
                    },
                    {
                        "name": "Type library management",
                        "lines": 9
                    },
                    {
                        "name": "Other",
                        "lines": 170
                    }
                ]
            },
            {
                "name": "EXPORT",
                "lines": 7,
                "subsections": []
            },
            {
                "name": "BUGS",
                "lines": 2,
                "subsections": []
            },
            {
                "name": "SEE ALSO",
                "lines": 8,
                "subsections": []
            },
            {
                "name": "AUTHOR",
                "lines": 2,
                "subsections": []
            },
            {
                "name": "COPYRIGHT AND LICENCE",
                "lines": 5,
                "subsections": []
            },
            {
                "name": "DISCLAIMER OF WARRANTIES",
                "lines": 4,
                "subsections": []
            }
        ],
        "sections": {
            "NAME": {
                "content": "Type::Utils - utility functions to make defining and using type constraints a little easier\n",
                "subsections": []
            },
            "SYNOPSIS": {
                "content": "package Types::Mine;\n\nuse Type::Library -base;\nuse Type::Utils -all;\n\nBEGIN { extends \"Types::Standard\" };\n\ndeclare \"AllCaps\",\nas \"Str\",\nwhere { uc($) eq $ },\ninlineas { my $varname = $[1]; \"uc($varname) eq $varname\" };\n\ncoerce \"AllCaps\",\nfrom \"Str\", via { uc($) };\n",
                "subsections": []
            },
            "STATUS": {
                "content": "This module is covered by the Type-Tiny stability policy.\n",
                "subsections": []
            },
            "DESCRIPTION": {
                "content": "This module provides utility functions to make defining and using type constraints a little\neasier.\n",
                "subsections": [
                    {
                        "name": "Type declaration functions",
                        "content": "Many of the following are similar to the similarly named functions described in\nMoose::Util::TypeConstraints.\n\n\"declare $name, %options\"\n\"declare %options\"\nDeclare a named or anonymous type constraint. Use \"as\" and \"where\" to specify the parent\ntype (if any) and (possibly) refine its definition.\n\ndeclare EvenInt, as Int, where { $ % 2 == 0 };\n\nmy $EvenInt = declare as Int, where { $ % 2 == 0 };\n\n*NOTE:* If the caller package inherits from Type::Library then any non-anonymous types\ndeclared in the package will be automatically installed into the library.\n\nHidden gem: if you're inheriting from a type constraint that includes some coercions, you\ncan include \"coercion => 1\" in the %options hash to inherit the coercions.\n\n\"subtype $name, %options\"\n\"subtype %options\"\nDeclare a named or anonymous type constraint which is descended from an existing type\nconstraint. Use \"as\" and \"where\" to specify the parent type and refine its definition.\n\nActually, you should use \"declare\" instead; this is just an alias.\n\nThis function is not exported by default.\n\n\"type $name, %options\"\n\"type %options\"\nDeclare a named or anonymous type constraint which is not descended from an existing type\nconstraint. Use \"where\" to provide a coderef that constrains values.\n\nActually, you should use \"declare\" instead; this is just an alias.\n\nThis function is not exported by default.\n\n\"as $parent\"\nUsed with \"declare\" to specify a parent type constraint:\n\ndeclare EvenInt, as Int, where { $ % 2 == 0 };\n\n\"where { BLOCK }\"\nUsed with \"declare\" to provide the constraint coderef:\n\ndeclare EvenInt, as Int, where { $ % 2 == 0 };\n\nThe coderef operates on $, which is the value being tested.\n\n\"message { BLOCK }\"\nGenerate a custom error message when a value fails validation.\n\ndeclare EvenInt,\nas Int,\nwhere { $ % 2 == 0 },\nmessage {\nInt->validate($) or \"$ is not divisible by two\";\n};\n\nWithout a custom message, the messages generated by Type::Tiny are along the lines of *Value\n\"33\" did not pass type constraint \"EvenInt\"*, which is usually reasonable.\n\n\"inlineas { BLOCK }\"\nGenerate a string of Perl code that can be used to inline the type check into other\nfunctions. If your type check is being used within a Moose or Moo constructor or accessor\nmethods, or used by Type::Params, this can lead to significant performance improvements.\n\ndeclare EvenInt,\nas Int,\nwhere { $ % 2 == 0 },\ninlineas {\nmy ($constraint, $varname) = @;\nmy $perlcode =\n$constraint->parent->inlinecheck($varname)\n. \"&& ($varname % 2 == 0)\";\nreturn $perlcode;\n};\n\nwarn EvenInt->inlinecheck('$xxx');  # demonstration\n\nYour \"inlineas\" block can return a list, in which case these will be smushed together with\n\"&&\". The first item on the list may be undef, in which case the undef will be replaced by\nthe inlined parent type constraint. (And will throw an exception if there is no parent.)\n\ndeclare EvenInt,\nas Int,\nwhere { $ % 2 == 0 },\ninlineas {\nreturn (undef, \"($ % 2 == 0)\");\n};\n\n\"classtype $name, { class => $package, %options }\"\n\"classtype { class => $package, %options }\"\n\"classtype $name\"\nShortcut for declaring a Type::Tiny::Class type constraint.\n\nIf $package is omitted, is assumed to be the same as $name. If $name contains \"::\" (which\nwould be an invalid name as far as Type::Tiny is concerned), this will be removed.\n\nSo for example, \"classtype(\"Foo::Bar\")\" declares a Type::Tiny::Class type constraint named\n\"FooBar\" which constrains values to objects blessed into the \"Foo::Bar\" package.\n\n\"roletype $name, { role => $package, %options }\"\n\"roletype { role => $package, %options }\"\n\"roletype $name\"\nShortcut for declaring a Type::Tiny::Role type constraint.\n\nIf $package is omitted, is assumed to be the same as $name. If $name contains \"::\" (which\nwould be an invalid name as far as Type::Tiny is concerned), this will be removed.\n\n\"ducktype $name, \\@methods\"\n\"ducktype \\@methods\"\nShortcut for declaring a Type::Tiny::Duck type constraint.\n\n\"union $name, \\@constraints\"\n\"union \\@constraints\"\nShortcut for declaring a Type::Tiny::Union type constraint.\n\n\"enum $name, \\@values\"\n\"enum \\@values\"\nShortcut for declaring a Type::Tiny::Enum type constraint.\n\n\"intersection $name, \\@constraints\"\n\"intersection \\@constraints\"\nShortcut for declaring a Type::Tiny::Intersection type constraint.\n"
                    },
                    {
                        "name": "Coercion declaration functions",
                        "content": "Many of the following are similar to the similarly named functions described in\nMoose::Util::TypeConstraints.\n\n\"coerce $target, @coercions\"\nAdd coercions to the target type constraint. The list of coercions is a list of type\nconstraint, conversion code pairs. Conversion code can be either a string of Perl code or a\ncoderef; in either case the value to be converted is $.\n\n\"from $source\"\nSugar to specify a type constraint in a list of coercions:\n\ncoerce EvenInt, from Int, via { $ * 2 };  # As a coderef...\ncoerce EvenInt, from Int, q { $ * 2 };    # or as a string!\n\n\"via { BLOCK }\"\nSugar to specify a coderef in a list of coercions.\n\n\"declarecoercion $name, \\%opts, $type1, $code1, ...\"\n\"declarecoercion \\%opts, $type1, $code1, ...\"\nDeclares a coercion that is not explicitly attached to any type in the library. For example:\n\ndeclarecoercion \"ArrayRefFromAny\", from \"Any\", via { [$] };\n\nThis coercion will be exportable from the library as a Type::Coercion object, but the\nArrayRef type exported by the library won't automatically use it.\n\nCoercions declared this way are immutable (frozen).\n\n\"totype $type\"\nUsed with \"declarecoercion\" to declare the target type constraint for a coercion, but still\nwithout explicitly attaching the coercion to the type constraint:\n\ndeclarecoercion \"ArrayRefFromAny\",\ntotype \"ArrayRef\",\nfrom \"Any\", via { [$] };\n\nYou should pretty much always use this when declaring an unattached coercion because it's\nexceedingly useful for a type coercion to know what it will coerce to - this allows it to\nskip coercion when no coercion is needed (e.g. avoiding coercing \"[]\" to \"[ [] ]\") and\nallows \"assertcoerce\" to work properly.\n"
                    },
                    {
                        "name": "Type library management",
                        "content": "\"extends @libraries\"\nIndicates that this type library extends other type libraries, importing their type\nconstraints.\n\nShould usually be executed in a \"BEGIN\" block.\n\nThis is not exported by default because it's not fun to export it to Moo, Moose or Mouse\nclasses! \"use Type::Utils -all\" can be used to import it into your type library.\n"
                    },
                    {
                        "name": "Other",
                        "content": "\"matchontype $value => ($type => \\&action, ..., \\&default?)\"\nSomething like a \"switch\"/\"case\" or \"given\"/\"when\" construct. Dispatches along different\ncode paths depending on the type of the incoming value. Example blatantly stolen from the\nMoose documentation:\n\nsub tojson\n{\nmy $value = shift;\n\nreturn matchontype $value => (\nHashRef() => sub {\nmy $hash = shift;\n'{ '\n. (\njoin \", \" =>\nmap { '\"' . $ . '\" : ' . tojson( $hash->{$} ) }\nsort keys %$hash\n) . ' }';\n},\nArrayRef() => sub {\nmy $array = shift;\n'[ '.( join \", \" => map { tojson($) } @$array ).' ]';\n},\nNum()   => q {$},\nStr()   => q { '\"' . $ . '\"' },\nUndef() => q {'null'},\n=> sub { die \"$ is not acceptable json type\" },\n);\n}\n\nNote that unlike Moose, code can be specified as a string instead of a coderef. (e.g. for\n\"Num\", \"Str\" and \"Undef\" above.)\n\nFor improved performance, try \"compilematchontype\".\n\nThis function is not exported by default.\n\n\"my $coderef = compilematchontype($type => \\&action, ..., \\&default?)\"\nCompile a \"matchontype\" block into a coderef. The following JSON converter is about two\norders of magnitude faster than the previous example:\n\nsub tojson;\n*tojson = compilematchontype(\nHashRef() => sub {\nmy $hash = shift;\n'{ '\n. (\njoin \", \" =>\nmap { '\"' . $ . '\" : ' . tojson( $hash->{$} ) }\nsort keys %$hash\n) . ' }';\n},\nArrayRef() => sub {\nmy $array = shift;\n'[ '.( join \", \" => map { tojson($) } @$array ).' ]';\n},\nNum()   => q {$},\nStr()   => q { '\"' . $ . '\"' },\nUndef() => q {'null'},\n=> sub { die \"$ is not acceptable json type\" },\n);\n\nRemember to store the coderef somewhere fairly permanent so that you don't compile it over\nand over. \"state\" variables (in Perl >= 5.10) are good for this. (Same sort of idea as\nType::Params.)\n\nThis function is not exported by default.\n\n\"my $coderef = classifier(@types)\"\nReturns a coderef that can be used to classify values according to their type constraint.\nThe coderef, when passed a value, returns a type constraint which the value satisfies.\n\nuse feature qw( say );\nuse Type::Utils qw( classifier );\nuse Types::Standard qw( Int Num Str Any );\n\nmy $classifier = classifier(Str, Int, Num, Any);\n\nsay $classifier->( \"42\"  )->name;   # Int\nsay $classifier->( \"4.2\" )->name;   # Num\nsay $classifier->( []    )->name;   # Any\n\nNote that, for example, \"42\" satisfies Int, but it would satisfy the type constraints Num,\nStr, and Any as well. In this case, the classifier has picked the most specific type\nconstraint that \"42\" satisfies.\n\nIf no type constraint is satisfied by the value, then the classifier will return undef.\n\n\"dwimtype($string, %options)\"\nGiven a string like \"ArrayRef[Int|CodeRef]\", turns it into a type constraint object,\nhopefully doing what you mean.\n\nIt uses the syntax of Type::Parser. Firstly the Type::Registry for the caller package is\nconsulted; if that doesn't have a match, Types::Standard is consulted for standard type\nconstraint names.\n\nIf none of the above yields a type constraint, and the caller class is a Moose-based class,\nthen \"dwimtype\" attempts to look the type constraint up in the Moose type registry. If it's\na Mouse-based class, then the Mouse type registry is used instead.\n\nIf no type constraint can be found via these normal methods, several fallbacks are\navailable:\n\n\"lookupviamoose\"\nLookup in Moose registry even if caller is non-Moose class.\n\n\"lookupviamouse\"\nLookup in Mouse registry even if caller is non-Mouse class.\n\n\"makeclasstype\"\nCreate a new Type::Tiny::Class constraint.\n\n\"makeroletype\"\nCreate a new Type::Tiny::Role constraint.\n\nYou can alter which should be attempted, and in which order, by passing an option to\n\"dwimtype\":\n\nmy $type = Type::Utils::dwimtype(\n\"ArrayRef[Int]\",\nfallback      => [ \"lookupviamouse\" , \"makeroletype\" ],\n);\n\nFor historical reasons, by default the fallbacks attempted are:\n\nlookupviamoose, lookupviamouse, makeclasstype\n\nYou may set \"fallback\" to an empty arrayref to avoid using any of these fallbacks.\n\nYou can specify an alternative for the caller using the \"for\" option.\n\nmy $type = dwimtype(\"ArrayRef\", for => \"Moose::Object\");\n\nWhile it's probably better overall to use the proper Type::Registry interface for resolving\ntype constraint strings, this function often does what you want.\n\nIt should never die if it fails to find a type constraint (but may die if the type\nconstraint string is syntactically malformed), preferring to return undef.\n\nThis function is not exported by default.\n\n\"is($type, $value)\"\nShortcut for \"$type->check($value)\" but also if $type is a string, will look it up via\n\"dwimtype\".\n\nThis function is not exported by default. This function is not even exported by \"use\nType::Utils -all\". You must request it explicitly.\n\nuse Type::Utils \"is\";\n\nBeware using this in test scripts because it has the same name as a function exported by\nTest::More. Note that you can rename this function if \"is\" will cause conflicts:\n\nuse Type::Utils \"is\" => { -as => \"isntnt\" };\n\n\"assert($type, $value)\"\nLike \"is\" but instead of returning a boolean, returns $value and dies if the value fails the\ntype check.\n\nThis function is not exported by default, but it is exported by \"use Type::Utils -all\".\n\n\"englishlist(\\$conjunction, @items)\"\nJoins the items with commas, placing a conjunction before the final item. The conjunction is\noptional, defaulting to \"and\".\n\nenglishlist(qw/foo bar baz/);       # \"foo, bar, and baz\"\nenglishlist(\\\"or\", qw/quux quuux/); # \"quux or quuux\"\n\nThis function is not exported by default.\n"
                    }
                ]
            },
            "EXPORT": {
                "content": "By default, all of the functions documented above are exported, except \"subtype\" and \"type\"\n(prefer \"declare\" instead), \"extends\", \"dwimtype\", \"matchontype\"/\"compilematchontype\",\n\"classifier\", and \"englishlist\".\n\nThis module uses Exporter::Tiny; see the documentation of that module for tips and tricks\nimporting from Type::Utils.\n",
                "subsections": []
            },
            "BUGS": {
                "content": "Please report any bugs to <https://github.com/tobyink/p5-type-tiny/issues>.\n",
                "subsections": []
            },
            "SEE ALSO": {
                "content": "Type::Tiny::Manual.\n\nType::Tiny, Type::Library, Types::Standard, Type::Coercion.\n\nType::Tiny::Class, Type::Tiny::Role, Type::Tiny::Duck, Type::Tiny::Enum, Type::Tiny::Union.\n\nMoose::Util::TypeConstraints, Mouse::Util::TypeConstraints.\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": []
            }
        }
    }
}