1Type::Tiny::Enum(3)   User Contributed Perl Documentation  Type::Tiny::Enum(3)
2
3
4

NAME

6       Type::Tiny::Enum - string enum type constraints
7

STATUS

9       This module is covered by the Type-Tiny stability policy.
10

DESCRIPTION

12       Enum type constraints.
13
14       This package inherits from Type::Tiny; see that for most documentation.
15       Major differences are listed below:
16
17   Attributes
18       "values"
19           Arrayref of allowable value strings. Non-string values (e.g.
20           objects with overloading) will be stringified in the constructor.
21
22       "constraint"
23           Unlike Type::Tiny, you cannot pass a constraint coderef to the
24           constructor.  Instead rely on the default.
25
26       "inlined"
27           Unlike Type::Tiny, you cannot pass an inlining coderef to the
28           constructor.  Instead rely on the default.
29
30       "parent"
31           Parent is always Types::Standard::Str, and cannot be passed to the
32           constructor.
33
34       "unique_values"
35           The list of "values" but sorted and with duplicates removed. This
36           cannot be passed to the constructor.
37
38       "coercion"
39           If "coercion => 1" is passed to the constructor, the type will have
40           a coercion using the "closest_match" method.
41
42   Methods
43       "as_regexp"
44           Returns the enum as a regexp which strings can be checked against.
45           If you're checking a lot of strings, then using this regexp might
46           be faster than checking each string against
47
48             my $enum  = Type::Tiny::Enum->new(...);
49             my $check = $enum->compiled_check;
50             my $re    = $enum->as_regexp;
51
52             # fast
53             my @valid_tokens = grep $enum->check($_), @all_tokens;
54
55             # faster
56             my @valid_tokens = grep $check->($_), @all_tokens;
57
58             # fastest
59             my @valid_tokens = grep /$re/, @all_tokens;
60
61           You can get a case-insensitive regexp using
62           "$enum->as_regexp('i')".
63
64       "closest_match"
65           Returns the closest match in the enum for a string.
66
67             my $enum = Type::Tiny::Enum->new(
68               values => [ qw( foo bar baz quux ) ],
69             );
70
71             say $enum->closest_match("FO");   # ==> foo
72
73           It will try to find an exact match first, fall back to a case-
74           insensitive match, if it still can't find one, will try to find a
75           head substring match, and finally, if given an integer, will use
76           that as an index.
77
78             my $enum = Type::Tiny::Enum->new(
79               values => [ qw( foo bar baz quux ) ],
80             );
81
82             say $enum->closest_match(  0 );  # ==> foo
83             say $enum->closest_match(  1 );  # ==> bar
84             say $enum->closest_match(  2 );  # ==> baz
85             say $enum->closest_match( -1 );  # ==> quux
86
87   Overloading
88       •   Arrayrefification calls "values".
89

BUGS

91       Please report any bugs to
92       <https://github.com/tobyink/p5-type-tiny/issues>.
93

SEE ALSO

95       Type::Tiny::Manual.
96
97       Type::Tiny.
98
99       Moose::Meta::TypeConstraint::Enum.
100

AUTHOR

102       Toby Inkster <tobyink@cpan.org>.
103
105       This software is copyright (c) 2013-2014, 2017-2021 by Toby Inkster.
106
107       This is free software; you can redistribute it and/or modify it under
108       the same terms as the Perl 5 programming language system itself.
109

DISCLAIMER OF WARRANTIES

111       THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
112       WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
113       MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
114
115
116
117perl v5.34.0                      2022-01-21               Type::Tiny::Enum(3)
Impressum