[Wellington-pm] 'enum' for Perl?
Grant McLean
grant at mclean.net.nz
Sun Apr 16 03:58:17 PDT 2006
On Sun, 2006-04-16 at 20:20 +1200, Cliff Pratt wrote:
> Is there a way of doing enums in Perl?
>
> eg so that using the string ONE in a program has the same effect as using 1.
>
> exit ONE ; # exit 1
You could say:
use constant ONE => 1;
Or you could simply use a variable:
my $ONE = 1;
Possibly with the addition of the Readonly module from CPAN:
Readonly my $ONE => 1;
When you have a number of possible values that you want to assign
symbolic names to, variables are easiest, eg:
my($IDLE, $WAITING, $CONNECTED, $RECEIVING, $DISCONNECTING) = (1..10);
In that example, I declared an overly large range on the right of the
assignment, so that I could easily add new states to the left as I
needed them.
Cheers
Grant
More information about the Wellington-pm
mailing list