Class::Enum - typed enum
Define `Direction`,
# Direction.pm package Direction; use Class::Enum qw(Left Right);
and using.
# using use Direction qw(Left Right); # default properties print Left ->name; # 'Left' print Right->name; # 'Right print Left ->ordinal; # 0 print Right->ordinal; # 1 print Left ->is_left; # 1 print Left ->is_right; # '' print Right->is_left; # '' print Right->is_right; # 1 # compare by ordinal print Left() <=> Right; # -1 print Left() < Right; # 1 print Left() <= Right; # 1 print Left() > Right; # '' print Left() >= Right; # '' print Left() == Right; # '' print Left() != Right; # 1 # compare by name print Left() cmp Right; # -1 print Left() lt Right; # 1 print Left() le Right; # 1 print Left() gt Right; # '' print Left() ge Right; # '' print Left() eq Right; # '' print Left() ne Right; # 1 # list values print join("\n", # '0: Left map { sprintf('%d: %s', $_, $_) } Direction->values); # 1: Right' # list names print join(', ', Direction->names); # 'Left, Right' # retrieve value of name print Left() == Direction->value_of('Left'); # 1 # retrieve value of ordinal print Left() == Direction->from_ordinal(0); # 1 # type print ref Left; # 'Direction'
# Direction.pm package Direction; use Class::Enum ( Left => { delta => -1 }, Right => { delta => 1 }, ); sub move { my ($self, $pos) = @_; return $pos + $self->delta; }
# using use Direction qw(Left Right); my $pos = 5; print Left->move($pos); # 4 print Right->move($pos); # 6
# Direction.pm package Direction; use Class::Enum ( Left => { name => 'L', ordinal => -1 }, Center => { name => 'C' } Right => { name => 'R' }, );
# using use Direction qw(Left Center Right); my $pos = 5; print $pos + Left; # 4 print $pos + Center; # 5 print $pos + Right; # 6 print 'Left is ' . Left; # 'Left is L' print 'Center is ' . Center; # 'Center is C' print 'Right is ' . Right; # 'Right is R'
# Direction.pm package Direction; use Class::Enum qw(Left Right), -overload => { '""' => sub { $_[0]->ordinal } };
# using use Direction qw(Left Right); print 'Left is ' . Left; # 'Left is 0' print 'Right is ' . Right; # 'Right is 1'
# Direction.pm package Direction; use Class::Enum qw(Left Right), -install_exporter => 0; # No install 'Exporter' use parent 'Exporter::Tiny'; our @EXPORT_OK = __PACKAGE__->names();
# using use Direction Left => { -as => 'L' }, Right => { -as => 'R' }; print L->name; # 'Left' print R->name; # 'Right
Class::Enum provides behaviors of typed enum, such as a Typesafe enum in java.
Copyright (C) keita.iseki.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
keita.iseki <keita.iseki+cpan at gmail.com>
To install Class::Enum, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Class::Enum
CPAN shell
perl -MCPAN -e shell install Class::Enum
For more information on module installation, please visit the detailed CPAN module installation guide.