NAME
Class::Enum - typed enum
SYNOPSIS
Simple usage.
Define `Direction`,
and using.
# using
# default properties
Left ->name;
# 'Left'
Right->name;
# 'Right
Left ->ordinal;
# 0
Right->ordinal;
# 1
Left ->is_left;
# 1
Left ->is_right;
# ''
Right->is_left;
# ''
Right->is_right;
# 1
# compare by ordinal
Left() <=> Right;
# -1
Left() < Right;
# 1
Left() <= Right;
# 1
Left() > Right;
# ''
Left() >= Right;
# ''
Left() == Right;
# ''
Left() != Right;
# 1
# compare by name
Left() cmp Right;
# -1
Left() lt Right;
# 1
Left() le Right;
# 1
Left() gt Right;
# ''
Left() ge Right;
# ''
Left() eq Right;
# ''
Left() ne Right;
# 1
# list values
join
(
"\n"
,
# '0: Left
map
{
sprintf
(
'%d: %s'
,
$_
,
$_
) } Direction->
values
);
# 1: Right'
# list names
join
(
', '
, Direction->names);
# 'Left, Right'
# retrieve value of name
Left() == Direction->value_of(
'Left'
);
# 1
# retrieve value of ordinal
Left() == Direction->from_ordinal(0);
# 1
# type
ref
Left;
# 'Direction'
Advanced usage.
Define `Direction`,
# Direction.pm
package
Direction;
use
Class::Enum (
Left
=> {
delta
=> -1 },
Right
=> {
delta
=> 1 },
);
sub
move {
my
(
$self
,
$pos
) =
@_
;
return
$pos
+
$self
->delta;
}
and using.
# using
my
$pos
= 5;
Left->move(
$pos
);
# 4
Right->move(
$pos
);
# 6
Override default properties. (Unrecommended)
Define `Direction`,
# Direction.pm
package
Direction;
use
Class::Enum (
Left
=> {
name
=>
'L'
,
ordinal
=> -1 },
Center
=> {
name
=>
'C'
}
Right
=> {
name
=>
'R'
},
);
and using.
# using
my
$pos
= 5;
$pos
+ Left;
# 4
$pos
+ Center;
# 5
$pos
+ Right;
# 6
'Left is '
. Left;
# 'Left is L'
'Center is '
. Center;
# 'Center is C'
'Right is '
. Right;
# 'Right is R'
Override overload
Define `Direction`,
# Direction.pm
package
Direction;
and using.
# using
'Left is '
. Left;
# 'Left is 0'
'Right is '
. Right;
# 'Right is 1'
Use alternate exporter.
Define `Direction`,
and using.
# using
Right
=> {
-as
=>
'R'
};
L->name;
# 'Left'
R->name;
# 'Right
DESCRIPTION
Class::Enum provides behaviors of typed enum, such as a Typesafe enum in java.
LICENSE
Copyright (C) keita.iseki.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
AUTHOR
keita.iseki <keita.iseki+cpan at gmail.com>