1enum(3)               User Contributed Perl Documentation              enum(3)
2
3
4

NAME

6       enum - C style enumerated types and bitmask flags in Perl
7

SYNOPSIS

9         use enum qw(Sun Mon Tue Wed Thu Fri Sat);
10         # Sun == 0, Mon == 1, etc
11
12         use enum qw(Forty=40 FortyOne Five=5 Six Seven);
13         # Yes, you can change the start indexs at any time as in C
14
15         use enum qw(:Prefix_ One Two Three);
16         ## Creates Prefix_One, Prefix_Two, Prefix_Three
17
18         use enum qw(:Letters_ A..Z);
19         ## Creates Letters_A, Letters_B, Letters_C, ...
20
21         use enum qw(
22             :Months_=0 Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
23             :Days_=0   Sun Mon Tue Wed Thu Fri Sat
24             :Letters_=20 A..Z
25         );
26         ## Prefixes can be changed mid list and can have index changes too
27
28         use enum qw(BITMASK:LOCK_ SH EX NB UN);
29         ## Creates bitmask constants for LOCK_SH == 1, LOCK_EX == 2,
30         ## LOCK_NB == 4, and LOCK_UN == 8.
31         ## NOTE: This example is only valid on FreeBSD-2.2.5 however, so don't
32         ## actually do this.  Import from Fnctl instead.
33

DESCRIPTION

35       This module is used to define a set of constants with ordered numeric
36       values, similar to the "enum" type in the C programming language.  You
37       can also define bitmask constants, where the value assigned to each
38       constant has exactly one bit set (eg 1, 2, 4, 8, etc).
39
40       What are enumerations good for?  Typical uses would be for giving
41       mnemonic names to indexes of arrays.  Such arrays might be a list of
42       months, days, or a return value index from a function such as
43       localtime():
44
45         use enum qw(
46             :Months_=0 Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
47             :Days_=0   Sun Mon Tue Wed Thu Fri Sat
48             :LC_=0     Sec Min Hour MDay Mon Year WDay YDay Isdst
49         );
50
51         if ((localtime)[LC_Mon] == Months_Jan) {
52             print "It's January!\n";
53         }
54         if ((localtime)[LC_WDay] == Days_Fri) {
55             print "It's Friday!\n";
56         }
57
58       This not only reads easier, but can also be typo-checked at compile
59       time when run under use strict.  That is, if you misspell Days_Fri as
60       Days_Fry, you'll generate a compile error.
61

BITMASKS

63       The BITMASK option allows the easy creation of bitmask constants such
64       as functions like flock() and sysopen() use.  These are also very
65       useful for your own code as they allow you to efficiently store many
66       true/false options within a single integer.
67
68           use enum qw(BITMASK: MY_ FOO BAR CAT DOG);
69
70           my $foo = 0;
71           $foo |= MY_FOO;
72           $foo |= MY_DOG;
73
74           if ($foo & MY_DOG) {
75               print "foo has the MY_DOG option set\n";
76           }
77           if ($foo & (MY_BAR | MY_DOG)) {
78               print "foo has either the MY_BAR or MY_DOG option set\n"
79           }
80
81           $foo ^= MY_DOG;  ## Turn MY_DOG option off (set its bit to false)
82
83       When using bitmasks, remember that you must use the bitwise operators,
84       |, &, ^, and ~.  If you try to do an operation like "$foo += MY_DOG;"
85       and the MY_DOG bit has already been set, you'll end up setting other
86       bits you probably didn't want to set.  You'll find the documentation
87       for these operators in the perlop manpage.
88
89       You can set a starting index for bitmasks just as you can for normal
90       enum values.  But if the given index isn't a power of 2, then it won't
91       resolve to a single bit and therefore will generate a compile error.
92       Because of this, whenever you set the BITFIELD: directive, the index is
93       automatically set to 1.  If you wish to go back to normal enum mode,
94       use the ENUM: directive.  Similarly to the BITFIELD directive, the
95       ENUM: directive resets the index to 0.  Here's an example:
96
97         use enum qw(
98             BITMASK:BITS_ FOO BAR CAT DOG
99             ENUM: FALSE TRUE
100             ENUM: NO YES
101             BITMASK: ONE TWO FOUR EIGHT SIX_TEEN
102         );
103
104       In this case, BITS_FOO, BITS_BAR, BITS_CAT, and BITS_DOG equal 1, 2, 4
105       and 8 respectively.  FALSE and TRUE equal 0 and 1.  NO and YES also
106       equal 0 and 1.  And ONE, TWO, FOUR, EIGHT, and SIX_TEEN equal, you
107       guessed it, 1, 2, 4, 8, and 16.
108

BUGS

110       Enum names can not be the same as method, function, or constant names.
111       This is probably a Good Thing[tm].
112
113       No way (that I know of) to cause compile time errors when one of these
114       enum names get redefined.  IMHO, there is absolutely no time when
115       redefining a sub is a Good Thing[tm], and should be taken out of the
116       language, or at least have a pragma that can cause it to be a compile
117       time error.
118
119       Enumerated types are package scoped just like constants, not block
120       scoped as some other pragma modules are.
121
122       It supports A..Z nonsense.  Can anyone give me a Real World[tm] reason
123       why anyone would ever use this feature...?
124

SEE ALSO

126       There are a number of modules that can be used to define enumerations:
127       Class::Enum, enum::fields, enum::hash, Readonly::Enum, Object::Enum,
128       Enumeration.
129
130       If you're using Moose, then MooseX::Enumeration may be of interest.
131       Type::Tiny::Enum is part of the Type-Tiny
132       <https://metacpan.org/release/Type-Tiny> distribution.
133
134       There are many CPAN modules related to defining constants in Perl; here
135       are some of the best ones: constant, Const::Fast, constant::lexical,
136       constant::our.
137
138       Neil Bowers has written a review of CPAN modules for definining
139       constants <http://neilb.org/reviews/constants.html>, which covers all
140       such modules.
141

REPOSITORY

143       <https://github.com/neilb/enum>
144

AUTHOR

146       Originally written by Byron Brummer (ZENIN), now maintained by Neil
147       Bowers <neilb@cpan.org>.
148
149       Based on early versions of the constant module by Tom Phoenix.
150
151       Original implementation of an interface of Tom Phoenix's design by
152       Benjamin Holzman, for which we borrow the basic parse algorithm layout.
153
155       Copyright 1998 (c) Byron Brummer.  Copyright 1998 (c) OMIX, Inc.
156
157       Permission to use, modify, and redistribute this module granted under
158       the same terms as Perl itself.
159
160
161
162perl v5.30.0                      2019-07-26                           enum(3)
Impressum