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

SYNOPSIS

9       Using via Types::Standard:
10
11         package Horse {
12           use Moo;
13           use Types::Standard qw( Str Enum );
14
15           has name    => ( is => 'ro', isa => Str );
16           has status  => ( is => 'ro', isa => Enum[ 'alive', 'dead' ] );
17
18           sub neigh {
19             my ( $self ) = @_;
20             return if $self->status eq 'dead';
21             ...;
22           }
23         }
24
25       Using Type::Tiny::Enum's export feature:
26
27         package Horse {
28           use Moo;
29           use Types::Standard qw( Str );
30           use Type::Tiny::Enum Status => [ 'alive', 'dead' ];
31
32           has name    => ( is => 'ro', isa => Str );
33           has status  => ( is => 'ro', isa => Status, default => STATUS_ALIVE );
34
35           sub neigh {
36             my ( $self ) = @_;
37             return if $self->status eq STATUS_DEAD;
38             ...;
39           }
40         }
41
42       Using Type::Tiny::Enum's object-oriented interface:
43
44         package Horse {
45           use Moo;
46           use Types::Standard qw( Str );
47           use Type::Tiny::Enum;
48
49           my $Status = Type::Tiny::Enum->new(
50             name   => 'Status',
51             values => [ 'alive', 'dead' ],
52           );
53
54           has name    => ( is => 'ro', isa => Str );
55           has status  => ( is => 'ro', isa => $Status, default => $Status->[0] );
56
57           sub neigh {
58             my ( $self ) = @_;
59             return if $self->status eq $Status->[0];
60             ...;
61           }
62         }
63

STATUS

65       This module is covered by the Type-Tiny stability policy.
66

DESCRIPTION

68       Enum type constraints.
69
70       This package inherits from Type::Tiny; see that for most documentation.
71       Major differences are listed below:
72
73   Constructors
74       The "new" constructor from Type::Tiny still works, of course. But there
75       is also:
76
77       "new_union( type_constraints => \@enums, %opts )"
78           Creates a new enum type constraint which is the union of existing
79           enum type constraints.
80
81       "new_intersection( type_constraints => \@enums, %opts )"
82           Creates a new enum type constraint which is the intersection of
83           existing enum type constraints.
84
85   Attributes
86       "values"
87           Arrayref of allowable value strings. Non-string values (e.g.
88           objects with overloading) will be stringified in the constructor.
89
90       "constraint"
91           Unlike Type::Tiny, you cannot pass a constraint coderef to the
92           constructor.  Instead rely on the default.
93
94       "inlined"
95           Unlike Type::Tiny, you cannot pass an inlining coderef to the
96           constructor.  Instead rely on the default.
97
98       "parent"
99           Parent is always Types::Standard::Str, and cannot be passed to the
100           constructor.
101
102       "unique_values"
103           The list of "values" but sorted and with duplicates removed. This
104           cannot be passed to the constructor.
105
106       "coercion"
107           If "coercion => 1" is passed to the constructor, the type will have
108           a coercion using the "closest_match" method.
109
110   Methods
111       "as_regexp"
112           Returns the enum as a regexp which strings can be checked against.
113           If you're checking a lot of strings, then using this regexp might
114           be faster than checking each string against
115
116             my $enum  = Type::Tiny::Enum->new(...);
117             my $check = $enum->compiled_check;
118             my $re    = $enum->as_regexp;
119
120             # fast
121             my @valid_tokens = grep $enum->check($_), @all_tokens;
122
123             # faster
124             my @valid_tokens = grep $check->($_), @all_tokens;
125
126             # fastest
127             my @valid_tokens = grep /$re/, @all_tokens;
128
129           You can get a case-insensitive regexp using
130           "$enum->as_regexp('i')".
131
132       "closest_match"
133           Returns the closest match in the enum for a string.
134
135             my $enum = Type::Tiny::Enum->new(
136               values => [ qw( foo bar baz quux ) ],
137             );
138
139             say $enum->closest_match("FO");   # ==> foo
140
141           It will try to find an exact match first, fall back to a case-
142           insensitive match, if it still can't find one, will try to find a
143           head substring match, and finally, if given an integer, will use
144           that as an index.
145
146             my $enum = Type::Tiny::Enum->new(
147               values => [ qw( foo bar baz quux ) ],
148             );
149
150             say $enum->closest_match(  0 );  # ==> foo
151             say $enum->closest_match(  1 );  # ==> bar
152             say $enum->closest_match(  2 );  # ==> baz
153             say $enum->closest_match( -1 );  # ==> quux
154
155       "is_word_safe"
156           Returns true if none of the values in the enumeration contain a
157           non-word character. Word characters include letters, numbers, and
158           underscores, but not most punctuation or whitespace.
159
160   Exports
161       Type::Tiny::Enum can be used as an exporter.
162
163         use Type::Tiny::Enum Status => [ 'dead', 'alive' ];
164
165       This will export the following functions into your namespace:
166
167       "Status"
168       is_Status( $value )
169       assert_Status( $value )
170       to_Status( $value )
171       "STATUS_DEAD"
172       "STATUS_ALIVE"
173
174       Multiple enumerations can be exported at once:
175
176         use Type::Tiny::Enum (
177           Status    => [ 'dead', 'alive' ],
178           TaxStatus => [ 'paid', 'pending' ],
179         );
180
181   Overloading
182       •   Arrayrefification calls "values".
183

BUGS

185       Please report any bugs to
186       <https://github.com/tobyink/p5-type-tiny/issues>.
187

SEE ALSO

189       Type::Tiny::Manual.
190
191       Type::Tiny.
192
193       Moose::Meta::TypeConstraint::Enum.
194

AUTHOR

196       Toby Inkster <tobyink@cpan.org>.
197
199       This software is copyright (c) 2013-2014, 2017-2023 by Toby Inkster.
200
201       This is free software; you can redistribute it and/or modify it under
202       the same terms as the Perl 5 programming language system itself.
203

DISCLAIMER OF WARRANTIES

205       THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
206       WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
207       MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
208
209
210
211perl v5.36.0                      2023-04-24               Type::Tiny::Enum(3)
Impressum