22 Sep 2022 15:44:28 UTC
- Distribution: Data-Enum
- Module version: v0.2.6
- Source (raw)
- Browse (raw)
- How to Contribute
- Issues (0)
- KwaliteeBus factor: 1
- 100.00% Coverage
- License: artistic_2
- Perl: v5.10.0
- Download (18KB)
- MetaCPAN Explorer
- Subscribe to distribution
- This version
- Latest version++ed by:1 non-PAUSE user
- SEE ALSO
- COPYRIGHT AND LICENSE
Data::Enum - immutable enumeration classes
use Data::Enum; my $color = Data::Enum->new( qw[ red yellow blue green ] ); my $red = $color->new("red"); $red->is_red; # "1" $red->is_yellow; # "" (false) $red->is_blue; # "" (false) $red->is_green; # "" (false) say $red; # outputs "red" $red eq $color->new("red"); # true $red eq "red"; # true
This module will create enumerated constant classes with the following properties:
Any two classes with the same elements are equivalent.
The following two classes are the same:
my $one = Data::Enum->new( qw[ foo bar baz ] ); my $two = Data::Enum->new( qw[ foo bar baz ] );
All class instances are singletons.
my $one = Data::Enum->new( qw[ foo bar baz ] ); my $a = $one->new("foo") my $b = $one->new("foo"); refaddr($a) == refaddr($b); # they are the same thing
Methods for checking values are fast.
$a->is_foo; # constant time $a eq $b; # compares refaddr
Values are immutable (read-only).
This is done by creating a unique internal class name based on the possible values. Each value is actually a subclass of that class, with the appropriate
is_method returning a constant.
my $class = Data::Enum->new( @values );
This creates a new anonymous class. Values can be instantiated with a constructor:
my $instance = $class->new( $value );
Calling the constructor with an invalid value will throw an exception.
Each instance will have an
is_method for each value.
Each instance stringifies to its value.
my @values = $class->values;
Returns a list of valid values, stringified and sorted with duplicates removed.
This was added in v0.2.0.
my @predicates = $class->predicates;
Returns a list of predicate methods for each value.
A hash of predicates to values is roughly
use List::Util 1.56 'mesh'; my %handlers = mesh [ $class->values ], [ $class->predicates ];
This was added in v0.2.1.
This method adds support for match::simple.
The overheard of creating a new class instance and resolving methods may actually take more time than comparing simple strings. When using this in production code, you may want to benchmark performance.
Please report any bugs or feature requests on the bugtracker website https://github.com/robrwo/perl-Data-Enum/issues
When submitting a bug or request, please include a test-file or a patch to an existing test-file that illustrates the bug or desired feature.
Robert Rothenberg <firstname.lastname@example.org>
This software is Copyright (c) 2021-2022 by Robert Rothenberg.
This is free software, licensed under:
The Artistic License 2.0 (GPL Compatible)
|s||Focus search bar|
|?||Bring up this help dialog|
|g p||Go to pull requests|
|g i||go to github issues (only if github is preferred repository)|
|g a||Go to author|
|g c||Go to changes|
|g i||Go to issues|
|g d||Go to dist|
|g r||Go to repository/SCM|
|g s||Go to source|
|g b||Go to file browse|