/usr/share/perl5/enum.pm is in libenum-perl 1.06-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 | package enum;
use strict;
use warnings;
no strict 'refs'; # Let's just make this very clear right off
use Carp;
our $VERSION = '1.06';
my $Ident = '[^\W_0-9]\w*';
sub ENUM () { 1 }
sub BITMASK () { 2 }
sub import {
my $class = shift;
@_ or return; # Ignore 'use enum;'
my $pkg = caller() . '::';
my $prefix = ''; # default no prefix
my $index = 0; # default start index
my $mode = ENUM; # default to enum
## Pragmas should be as fast as they can be, so we inline some
## pieces.
foreach (@_) {
## Plain tag is most common case
if (/^$Ident$/o) {
my $n = $index;
if ($mode == ENUM) {
$index++;
}
elsif ($mode == BITMASK) {
$index ||= 1;
$index *= 2;
if ( $index & ($index - 1) ) {
croak (
"$index is not a valid single bitmask "
. " (Maybe you overflowed your system's max int value?)"
);
}
}
else {
confess qq(Can't Happen: mode $mode invalid);
}
*{"$pkg$prefix$_"} = sub () { $n };
}
## Index change
elsif (/^($Ident)=(-?)(.+)$/o) {
my $name= $1;
my $neg = $2;
$index = $3;
## Convert non-decimal numerics to decimal
if ($index =~ /^0x[\da-f]+$/i) { ## Hex
$index = hex $index;
}
elsif ($index =~ /^0\d/) { ## Octal
$index = oct $index;
}
elsif ($index !~ /[^\d_]/) { ## 123_456 notation
$index =~ s/_//g;
}
## Force numeric context, but only in numeric context
if ($index =~ /\D/) {
$index = "$neg$index";
}
else {
$index = "$neg$index";
$index += 0;
}
my $n = $index;
if ($mode == BITMASK) {
($index & ($index - 1))
and croak "$index is not a valid single bitmask";
$index *= 2;
}
elsif ($mode == ENUM) {
$index++;
}
else {
confess qq(Can't Happen: mode $mode invalid);
}
*{"$pkg$prefix$name"} = sub () { $n };
}
## Prefix/option change
elsif (/^([A-Z]*):($Ident)?(=?)(-?)(.*)/) {
## Option change
if ($1) {
if ($1 eq 'ENUM') { $mode = ENUM; $index = 0 }
elsif ($1 eq 'BITMASK') { $mode = BITMASK; $index = 1 }
else { croak qq(Invalid enum option '$1') }
}
my $neg = $4;
## Index change too?
if ($3) {
if (length $5) {
$index = $5;
## Convert non-decimal numerics to decimal
if ($index =~ /^0x[\da-f]+$/i) { ## Hex
$index = hex $index;
}
elsif ($index =~ /^0\d/) { ## Oct
$index = oct $index;
}
elsif ($index !~ /[^\d_]/) { ## 123_456 notation
$index =~ s/_//g;
}
## Force numeric context, but only in numeric context
if ($index =~ /\D/) {
$index = "$neg$index";
}
else {
$index = "$neg$index";
$index += 0;
}
## Bitmask mode must check index changes
if ($mode == BITMASK) {
($index & ($index - 1))
and croak "$index is not a valid single bitmask";
}
}
else {
croak qq(No index value defined after "=");
}
}
## Incase it's a null prefix
$prefix = defined $2 ? $2 : '';
}
## A..Z case magic lists
elsif (/^($Ident)\.\.($Ident)$/o) {
## Almost never used, so check last
foreach my $name ("$1" .. "$2") {
my $n = $index;
if ($mode == BITMASK) {
($index & ($index - 1))
and croak "$index is not a valid single bitmask";
$index *= 2;
}
elsif ($mode == ENUM) {
$index++;
}
else {
confess qq(Can't Happen: mode $mode invalid);
}
*{"$pkg$prefix$name"} = sub () { $n };
}
}
else {
croak qq(Can't define "$_" as enum type (name contains invalid characters));
}
}
}
1;
__END__
=head1 NAME
enum - C style enumerated types and bitmask flags in Perl
=head1 SYNOPSIS
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't
## actually do this. Import from Fnctl instead.
=head1 DESCRIPTION
Defines a set of symbolic constants with ordered numeric values ala B<C> B<enum> types.
Now capable of creating creating ordered bitmask constants as well. See the B<BITMASKS>
section for details.
What are they 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 localtime():
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's January!\n";
}
if ((localtime)[LC_WDay] == Days_Fri) {
print "It's Friday!\n";
}
This not only reads easier, but can also be typo-checked at compile time when
run under B<use strict>. That is, if you misspell B<Days_Fri> as B<Days_Fry>,
you'll generate a compile error.
=head1 BITMASKS, bitwise operations, and bitmask option values
The B<BITMASK> option allows the easy creation of bitmask constants such as
functions like flock() and sysopen() 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.
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\n";
}
if ($foo & (MY_BAR | MY_DOG)) {
print "foo has either the MY_BAR or MY_DOG option set\n"
}
$foo ^= MY_DOG; ## Turn MY_DOG option off (set its bit to false)
When using bitmasks, remember that you must use the bitwise operators, B<|>, B<&>, B<^>,
and B<~>. If you try to do an operation like C<$foo += MY_DOG;> and the B<MY_DOG> 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 B<perlop> manpage.
You can set a starting index for bitmasks just as you can for normal B<enum> values,
but if the given index isn't a power of 2 it won't resolve to a single bit and therefor
will generate a compile error. Because of this, whenever you set the B<BITFIELD:>
directive, the index is automatically set to 1. If you wish to go back to normal B<enum>
mode, use the B<ENUM:> directive. Similarly to the B<BITFIELD> directive, the B<ENUM:>
directive resets the index to 0. Here's an example:
use enum qw(
BITMASK:BITS_ FOO BAR CAT DOG
ENUM: FALSE TRUE
ENUM: NO YES
BITMASK: ONE TWO FOUR EIGHT SIX_TEEN
);
In this case, B<BITS_FOO, BITS_BAR, BITS_CAT, and BITS_DOG> equal 1, 2, 4 and
8 respectively. B<FALSE and TRUE> equal 0 and 1. B<NO and YES> also equal
0 and 1. And B<ONE, TWO, FOUR, EIGHT, and SIX_TEEN> equal, you guessed it, 1,
2, 4, 8, and 16.
=head1 BUGS
Enum names can not be the same as method, function, or constant names. This
is probably a Good Thing[tm].
No way (that I know of) to cause compile time errors when one of these enum names get
redefined. IMHO, 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.
Enumerated types are package scoped just like constants, not block scoped as some
other pragma modules are.
It supports A..Z nonsense. Can anyone give me a Real World[tm] reason why anyone would
ever use this feature...?
=head1 SEE ALSO
There are a number of modules that can be used to define enumerations:
L<Class::Enum>, L<enum::fields>, L<enum::hash>, L<Readonly::Enum>, L<Object::Enum>.
There are many CPAN modules related to defining constants in Perl;
here are some of the best ones:
L<constant>, L<Const::Fast>, L<constant::lexical>, L<constant::our>.
Neil Bowers has written a
L<review of CPAN modules for definining constants|http://neilb.org/reviews/constants.html>,
which covers all such modules.
=head1 AUTHOR
Originally written by Byron Brummer (ZENIN),
now maintained by Neil Bowers E<lt>neilb@cpan.orgE<gt>.
Based on early versions of the B<constant> module by Tom Phoenix.
Original implementation of an interface of Tom Phoenix's
design by Benjamin Holzman, for which we borrow the basic
parse algorithm layout.
=head1 COPYRIGHT AND LICENSE
Copyright 1998 (c) Byron Brummer.
Copyright 1998 (c) OMIX, Inc.
Permission to use, modify, and redistribute this module granted under
the same terms as Perl itself.
=cut
|