.\" Automatically generated by Pod::Man 4.14 (Pod::Simple 3.42) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "enum 3pm" .TH enum 3pm "2022-10-13" "perl v5.34.0" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" enum \- C style enumerated types and bitmask flags in Perl .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 2 \& use enum qw(Sun Mon Tue Wed Thu Fri Sat); \& # Sun == 0, Mon == 1, etc \& \& use enum qw(Forty=40 FortyOne Five=5 Six Seven); \& # Yes, you can change the start indexs at any time as in C \& \& use enum qw(:Prefix_ One Two Three); \& ## Creates Prefix_One, Prefix_Two, Prefix_Three \& \& use enum qw(:Letters_ A..Z); \& ## Creates Letters_A, Letters_B, Letters_C, ... \& \& use enum qw( \& :Months_=0 Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec \& :Days_=0 Sun Mon Tue Wed Thu Fri Sat \& :Letters_=20 A..Z \& ); \& ## Prefixes can be changed mid list and can have index changes too \& \& use enum qw(BITMASK:LOCK_ SH EX NB UN); \& ## Creates bitmask constants for LOCK_SH == 1, LOCK_EX == 2, \& ## LOCK_NB == 4, and LOCK_UN == 8. \& ## NOTE: This example is only valid on FreeBSD\-2.2.5 however, so don\*(Aqt \& ## actually do this. Import from Fnctl instead. .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" This module is used to define a set of constants with ordered numeric values, similar to the \f(CW\*(C`enum\*(C'\fR type in the C programming language. You can also define bitmask constants, where the value assigned to each constant has exactly one bit set (eg 1, 2, 4, 8, etc). .PP What are enumerations good for? Typical uses would be for giving mnemonic names to indexes of arrays. Such arrays might be a list of months, days, or a return value index from a function such as \fBlocaltime()\fR: .PP .Vb 5 \& use enum qw( \& :Months_=0 Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec \& :Days_=0 Sun Mon Tue Wed Thu Fri Sat \& :LC_=0 Sec Min Hour MDay Mon Year WDay YDay Isdst \& ); \& \& if ((localtime)[LC_Mon] == Months_Jan) { \& print "It\*(Aqs January!\en"; \& } \& if ((localtime)[LC_WDay] == Days_Fri) { \& print "It\*(Aqs Friday!\en"; \& } .Ve .PP This not only reads easier, but can also be typo-checked at compile time when run under \fBuse strict\fR. That is, if you misspell \fBDays_Fri\fR as \fBDays_Fry\fR, you'll generate a compile error. .SH "BITMASKS" .IX Header "BITMASKS" The \fB\s-1BITMASK\s0\fR option allows the easy creation of bitmask constants such as functions like \fBflock()\fR and \fBsysopen()\fR use. These are also very useful for your own code as they allow you to efficiently store many true/false options within a single integer. .PP .Vb 1 \& use enum qw(BITMASK:MY_ FOO BAR CAT DOG); \& \& my $foo = 0; \& $foo |= MY_FOO; \& $foo |= MY_DOG; \& \& if ($foo & MY_DOG) { \& print "foo has the MY_DOG option set\en"; \& } \& if ($foo & (MY_BAR | MY_DOG)) { \& print "foo has either the MY_BAR or MY_DOG option set\en" \& } \& \& $foo ^= MY_DOG; ## Turn MY_DOG option off (set its bit to false) .Ve .PP When using bitmasks, remember that you must use the bitwise operators, \&\fB|\fR, \fB&\fR, \fB^\fR, and \fB~\fR. If you try to do an operation like \f(CW\*(C`$foo += MY_DOG;\*(C'\fR and the \fB\s-1MY_DOG\s0\fR bit has already been set, you'll end up setting other bits you probably didn't want to set. You'll find the documentation for these operators in the \fBperlop\fR manpage. .PP You can set a starting index for bitmasks just as you can for normal \fBenum\fR values. But if the given index isn't a power of 2, then it won't resolve to a single bit and therefore will generate a compile error. Because of this, whenever you set the \fB\s-1BITFIELD:\s0\fR directive, the index is automatically set to 1. If you wish to go back to normal \fBenum\fR mode, use the \fB\s-1ENUM:\s0\fR directive. Similarly to the \fB\s-1BITFIELD\s0\fR directive, the \fB\s-1ENUM:\s0\fR directive resets the index to 0. Here's an example: .PP .Vb 6 \& use enum qw( \& BITMASK:BITS_ FOO BAR CAT DOG \& ENUM: FALSE TRUE \& ENUM: NO YES \& BITMASK: ONE TWO FOUR EIGHT SIX_TEEN \& ); .Ve .PP In this case, \fB\s-1BITS_FOO, BITS_BAR, BITS_CAT,\s0 and \s-1BITS_DOG\s0\fR equal 1, 2, 4 and 8 respectively. \fB\s-1FALSE\s0 and \s-1TRUE\s0\fR equal 0 and 1. \fB\s-1NO\s0 and \s-1YES\s0\fR also equal 0 and 1. And \fB\s-1ONE, TWO, FOUR, EIGHT,\s0 and \s-1SIX_TEEN\s0\fR equal, you guessed it, 1, 2, 4, 8, and 16. .SH "BUGS" .IX Header "BUGS" Enum names can not be the same as method, function, or constant names. This is probably a Good Thing[tm]. .PP No way (that I know of) to cause compile time errors when one of these enum names get redefined. \s-1IMHO,\s0 there is absolutely no time when redefining a sub is a Good Thing[tm], and should be taken out of the language, or at least have a pragma that can cause it to be a compile time error. .PP Enumerated types are package scoped just like constants, not block scoped as some other pragma modules are. .PP It supports A..Z nonsense. Can anyone give me a Real World[tm] reason why anyone would ever use this feature...? .SH "SEE ALSO" .IX Header "SEE ALSO" There are a number of modules that can be used to define enumerations: Class::Enum, enum::fields, enum::hash, Readonly::Enum, Object::Enum, Enumeration. .PP If you're using Moose, then MooseX::Enumeration may be of interest. Type::Tiny::Enum is part of the Type-Tiny distribution. .PP There are many \s-1CPAN\s0 modules related to defining constants in Perl; here are some of the best ones: constant, Const::Fast, constant::lexical, constant::our. .PP Neil Bowers has written a review of \s-1CPAN\s0 modules for definining constants , which covers all such modules. .SH "REPOSITORY" .IX Header "REPOSITORY" .SH "AUTHOR" .IX Header "AUTHOR" Originally written by Byron Brummer (\s-1ZENIN\s0), now maintained by Neil Bowers . .PP Based on early versions of the \fBconstant\fR module by Tom Phoenix. .PP Original implementation of an interface of Tom Phoenix's design by Benjamin Holzman, for which we borrow the basic parse algorithm layout. .SH "COPYRIGHT AND LICENSE" .IX Header "COPYRIGHT AND LICENSE" Copyright 1998 (c) Byron Brummer. Copyright 1998 (c) \s-1OMIX,\s0 Inc. .PP Permission to use, modify, and redistribute this module granted under the same terms as Perl itself.