Class:...e::Enum(3pm)
π Class::Type::Enum β the manual, but make it cute π
okay so bestie, welcome to the Class:...e::Enum(3pm) page from the User Contributed Perl Documentation. sit down, grab a strawberry π, and let's build some enum classes together.
β¨ NAME β¨
Class::Type::Enum β literally builds you Enum-like classes. that's the whole vibe. π
πΈ VERSION πΈ
version 0.014 β that's the one we're talking about, no cap.
π¦ SYNOPSIS π¦
okay here's the whole thing in action, take it in:
package Toast::Status {
use Class::Type::Enum values => ['bread', 'toasting', 'toast', 'burnt'];
}
package Toast {
use Moo;
has status => (
is => 'rw',
isa => Toast::Status->type_constraint,
coerce => 1,
handles => [ Toast::Status->list_is_methods ],
);
}
my @toast = map { Toast->new(status => $_) } qw( toast burnt bread bread toasting toast );
my @trashcan = grep { $_->is_burnt } @toast;
my @plate = grep { $_->is_toast } @toast;
my $ready_status = Toast::Status->new('toast');
my @eventual_toast = grep { $_->status < $ready_status } @toast;
# or:
@eventual_toast = grep { $_->status lt 'toast' } @toast;
# or:
@eventual_toast = grep { $_->status->none('toast', 'burnt') } @toast;
it's giving toast. it's giving enumerated toast. π
π DESCRIPTION π
So Class::Type::Enum is a class builder for type-like classes that represent your enumerated values. the way it was built? to scratch an itch with DBIx::Class value inflation. relatable honestly. π
and ngl, the author says the interface isn't stable yet β they'd love feedback on this dist. so if you have thoughts, speak up, bestie. π«Ά
here's what happens when you use Class::Type::Enum:
β Required β
values => [@symbols]β the list of symbolic values in your enum, in ascending order if that's relevant to you. πvalues => {symbol => ordinal, ...}β the list of symbols and their ordinal values in your enum. heads up: there is no check that a given ordinal isn't reused. π
π Custom Ordinal Values π
if you wanna build an enum that works like a bitfield, or some other custom setup, you literally just pass a more explicit hashref to Class::Type::Enum. that's it. iconic behaviour.
package BitField {
use Class::Type::Enum values => {
READ => 1,
WRITE => 2,
EXECUTE => 4,
};
}
π METHODS π
π $class->import(values => ...)
Sets up the consuming class as a subclass of Class::Type::Enum and installs functions that are unique to the class. β¨
π $class->new($value)
Your basic constructor, bestie. expects only a value corresponding to a symbol in the enum type. and plot twist β it also works as an instance method for enums of the same class. π
π $class->inflate_symbol($symbol)
Does the actual work of "$class->new($value)", and it's also used when inflating values for DBIx::Class::InflateColumn::ClassTypeEnum. π
π $class->inflate_ordinal($ord)
Used when inflating ordinal values for DBIx::Class::InflateColumn::ClassTypeEnum, or if you need to work with ordinals directly. π’
π $class->sym_to_ord
Returns a hashref keyed by symbol, with ordinals as values. π«
π $class->ord_to_sym
Returns a hashref keyed by ordinal, with symbols as values. π
π $class->values
Returns an arrayref of valid symbolic values, in order. π
π $class->list_is_methods
Returns a list of "is_" methods defined for each symbolic value for the class. so many little helpers, so cute. π
π $class->type_constraint
This method requires the optional dependency Type::Tiny. π
Returns a type constraint suitable for use with Moo and friends. π«Ά
π $class->test_symbol($value)
Test whether or not the given value is a valid symbol in this enum class. β
π $class->test_ordinal($value)
Test whether or not the given value is a valid ordinal in this enum class. β
π $class->coerce_symbol($value)
If the given value is already a $class, return it β otherwise try to inflate it as a symbol. Dies on invalid value. π
π $class->coerce_ordinal($value)
If the given value is already a $class, return it β otherwise try to inflate it as an ordinal. Dies on invalid value. π
π $class->coerce_any($value)
If the given value is already a $class, return it β otherwise try to inflate it first as an ordinal, then as a symbol. Dies on invalid value. π
π $o->is($value)
Given a test symbol, test that the enum instance's value is equivalent.
An exception is thrown if an invalid symbol is provided. β οΈ
π $o->is_$value
Shortcut for "$o->is($value)". lazy girl coding, and honestly? valid. π
π $o->stringify
Returns the symbolic value. π€
π $o->numify
Returns the ordinal value. π’
π $o->cmp($other, $reversed = undef)
The string-compare implementation used by overloading. Returns the same values as "cmp". the optional third argument is an artifact of overload β it's set to true if the order of $o and $other have been reversed in order to make the overloaded method call work. π§
π $o->any(@cases)
True if "$o->is(..)" for any of the given cases. π
π $o->none(@cases)
True if "$o->is(..)" for none of the given cases. π
π«Ά SEE ALSO π«Ά
Object::EnumClass::EnumEnumeration
go read them, iykyk. πβ¨
π AUTHOR π
Meredith Howard <mhoward@cpan.org> π
π COPYRIGHT AND LICENSE π
This software is copyright (c) 2018 by Meredith Howard.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
and that's the whole page, bestie β perl v5.32.1, dated 2021-08-02, the Class:...e::Enum(3pm) manual. slay, periodt. π
β¨ππ¦
π¦ where this page comes from
- debian bookworm: libclass-type-enum-perl 0.014-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libclass-type-enum-perl 0.014-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu jammy: libclass-type-enum-perl 0.014-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libclass-type-enum-perl 0.014-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libclass-type-enum-perl 0.014-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libclass-type-enum-perl 0.014-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms