Type::Tiny::Enum(3)
π Type::Tiny::Enum π
okay bestie, listen. this is the manual page for Type::Tiny::Enum β it's a Perl module, it came to you via the User Contributed Perl Documentation crew, and it's literally the most iconic way to say "this string can only be one of these specific strings" π
it's all about string enum type constraints. periodt.
β¨ NAME β¨
Type::Tiny::Enum β string enum type constraints π¦
β¨ SYNOPSIS β¨
so there's like, three whole ways to use this. pick your fighter π
Way #1: via Types::Standard:
package Horse {
use Moo;
use Types::Standard qw( Str Enum );
has name => ( is => 'ro', isa => Str );
has status => ( is => 'ro', isa => Enum[ 'alive', 'dead' ] );
sub neigh {
my ( $self ) = @_;
return if $self->status eq 'dead';
...;
}
}
Way #2: using Type::Tiny::Enum's export feature:
package Horse {
use Moo;
use Types::Standard qw( Str );
use Type::Tiny::Enum Status => [ 'alive', 'dead' ];
has name => ( is => 'ro', isa => Str );
has status => ( is => 'ro', isa => Status, default => STATUS_ALIVE );
sub neigh {
my ( $self ) = @_;
return if $self->status eq STATUS_DEAD;
...;
}
}
Way #3: using Type::Tiny::Enum's object-oriented interface:
package Horse {
use Moo;
use Types::Standard qw( Str );
use Type::Tiny::Enum;
my $Status = Type::Tiny::Enum->new(
name => 'Status',
values => [ 'alive', 'dead' ],
);
has name => ( is => 'ro', isa => Str );
has status => ( is => 'ro', isa => $Status, default => $Status->[0] );
sub neigh {
my ( $self ) = @_;
return if $self->status eq $Status->[0];
...;
}
}
β¨ STATUS β¨
this module is covered by the Type-Tiny stability policy. so like, it's stable, it's not going anywhere, it's committed to you π«Ά
β¨ DESCRIPTION β¨
Enum type constraints.
this package inherits from Type::Tiny; go read that for most of the documentation, bestie. the major differences are listed below:
πΈ Constructors πΈ
the new constructor from Type::Tiny still works, obviously, no cap. but there's ALSO:
new_union( type_constraints => \@enums, %opts )β makes a brand new enum type constraint that's the union of existing enum type constraints. it's giving merge, but make it type theory β¨new_intersection( type_constraints => \@enums, %opts )β makes a new enum type constraint that's the intersection of existing enum type constraints. only the overlap survives, iconic behaviour π
πΈ Attributes πΈ
valuesβ an arrayref of the allowable value strings. non-string values (like objects with overloading, fancy) get stringified in the constructor. no exceptions, she's a string enum and she stays a string enum πconstraintβ unlike Type::Tiny, you cannot pass a constraint coderef to the constructor. instead, just rely on the default, okay? trust the process π§inlinedβ unlike Type::Tiny, you cannot pass an inlining coderef to the constructor either. again: rely on the default, bestie π§parentβ parent is always Types::Standard::Str, and it cannot be passed to the constructor. it's not a phase, it's her whole identity.sorterβ sorters passed to the constructor. instead, enum types can sort values based on the order the enum was originally defined in. so the order you wrote them in is the order you get them back. cute, right?unique_valuesβ the list ofvaluesbut sorted and with duplicates removed. this cannot be passed to the constructor. it's read-only and she likes it that way π₯coercionβ ifcoercion => 1is passed to the constructor, the type will have a coercion using theclosest_matchmethod. yes, it'll guess what you meant. she's helpful like that.use_eqβ when generating Perl type checking code, Type::Tiny::Enum will traditionally test incoming strings for being valid using a single regular expression, unless Type::Tiny::XS is available and a faster XS check is possible.
from version 2.008006 onwards, if Type::Tiny::XS is unavailable, and the enum is "small" (five possible values or less), Type::Tiny::Enum will instead generate code like:
( $_ eq "foo" or $_ eq "bar" or $_ eq "baz" )
... which benchmarks around 5% to 20% faster than /(?:ba[rz]|foo)/.
however, it is possible to manually indicate whether you prefer it to generate code using eq or regexps by setting use_eq to a boolean value in the constructor. (if use_eq is not passed to the constructor at all, Type::Tiny::Enum will try to guess the most efficient technique. she's doing her best π«Ά)
if you know that certain values in your enumeration are more common than others, you can "front load" your enumeration with the most common values so that eq checks those first. this may allow you to speed up certain checks.
has car_colour => (
is => 'rw',
isa => Type::Tiny::Enum->new( use_eq => 1, values => [qw/
blue
red
grey
white
black
green
yellow
orange
purple
pink
/] );
);
πΈ Methods πΈ
as_regexpβ returns the enum as a regexp which strings can be checked against. if you're checking a LOT of strings, then using this regexp might be faster than checking each string against... well, watch:
my $enum = Type::Tiny::Enum->new(...);
my $check = $enum->compiled_check;
my $re = $enum->as_regexp;
# fast
my @valid_tokens = grep $enum->check($_), @all_tokens;
# faster
my @valid_tokens = grep $check->($_), @all_tokens;
# fastest
my @valid_tokens = grep /$re/, @all_tokens;
you can get a case-insensitive regexp using $enum->as_regexp('i'). lowercase i, capital energy π
closest_matchβ returns the closest match in the enum for a string.
my $enum = Type::Tiny::Enum->new(
values => [ qw( foo bar baz quux ) ],
);
say $enum->closest_match("FO"); # ==> foo
it will try to find an exact match first, fall back to a case-insensitive match, if it still can't find one, will try to find a head substring match, and finally, if given an integer, will use that as an index.
my $enum = Type::Tiny::Enum->new(
values => [ qw( foo bar baz quux ) ],
);
say $enum->closest_match( 0 ); # ==> foo
say $enum->closest_match( 1 ); # ==> bar
say $enum->closest_match( 2 ); # ==> baz
say $enum->closest_match( -1 ); # ==> quux
look at her go π so smart.
is_word_safeβ returns true if none of the values in the enumeration contain a non-word character. word characters include letters, numbers, and underscores, but not most punctuation or whitespace. so basically: is this enum safe to embed in code without it doing crimes? that's the vibe check π
πΈ Exports πΈ
Type::Tiny::Enum can be used as an exporter. slay.
use Type::Tiny::Enum Status => [ 'dead', 'alive' ];
this will export the following functions into your namespace:
Statusis_Status( $value )assert_Status( $value )to_Status( $value )STATUS_DEADSTATUS_ALIVE
multiple enumerations can be exported at once:
use Type::Tiny::Enum (
Status => [ 'dead', 'alive' ],
TaxStatus => [ 'paid', 'pending' ],
);
two enums, one use statement. efficiency queen π
πΈ Overloading πΈ
- Arrayrefification calls
values. so if you go all$enum->[0]on her, she gives you the first value. main character behaviour, honestly π
π BUGS π
please report any bugs to <https://github.com/tobyink/p5-type-tiny/issues>. be nice, be specific, include a repro, and the maintainers will love you forever π«Ά
β¨ SEE ALSO β¨
- Type::Tiny::Manual β the big book, start here.
- Type::Tiny β the parent, the blueprint, the blueprint-mother.
- Moose::Meta::TypeConstraint::Enum β for when you're in Moose-land.
β¨ AUTHOR β¨
Toby Inkster <tobyink@cpan.org>. legend. icon. the moment. π
β¨ COPYRIGHT AND LICENCE β¨
this software is copyright (c) 2013-2014, 2017-2025 by Toby Inkster.
this is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. so like, go off, remix it, share it, just keep the same terms fr fr π
β¨ DISCLAIMER OF WARRANTIES β¨
THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
translation: she's beautiful, she's brilliant, she will not legally cover you if your production server catches fire. no cap π
and that's the whole page, bestie!! this was perl v5.42.0, dated 2026-01-17, and it's the Type::Tiny::Enum(3) manpage β go forth and enumerate responsibly π¦πβ¨
π¦ where this page comes from
- fedora 44: perl-Type-Tiny 2.010001-2.fc44 Β· GPL-1.0-or-later OR Artistic-1.0-Perl
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms