enum-1.11/000755 000765 000024 00000000000 12613767407 012651 5ustar00neilbstaff000000 000000 enum-1.11/Changes000644 000765 000024 00000007476 12613767135 014160 0ustar00neilbstaff000000 000000 Revision history for Perl module enum 1.11 2015-10-27 NEILB - Updated github repo URL after changing my github username 1.10 2014-08-16 NEILB - Added min perl version (5.006) to enum.pm - Improved DESCRIPTION: reworded first paragraph to be a better summary, fixed formatting, and one typo. 1.09 2014-05-26 NEILB - Added some more modules to the SEE ALSO section - Various documentation improvements - Added github repo to the pod 1.08 2014-05-06 NEILB - Changed how the constants are defined, to deal with a change in 5.19.3 "Closures of the form "sub () { $some_variable }" are no longer inlined, causing changes to the variable to be ignored by callers of the subroutine. [perl #79908]" RT#95387 - thanks to Slaven Rezic. - Changed use of \d to [0-9] 1.07 2014-04-10 NEILB - Playing with Devel::Cover, starting to improve coverage of testsuite - Added testsuite for hex and octal index values, and _ in long numbers - Added testsuite for bitmask wrap-around cases 1.06 2014-01-25 NEILB - Specified MIN_PERL_VERSION as 5.006 in Makefile.PL - Changed the COPYRIGHT section to COPYRIGHT AND LICENSE so Kwalitee / CPANTS would see it. - Added 'warnings' to PREREQ_PM 1.05 2013-09-06 NEILB - I'd got two versions wrong when merging the history information from the pod into Changes. As a result the previous release was failing conformance with CPAN::Changes::Spec 1.04 2013-09-05 NEILB - Bumped version so I can release, now the offending CORBA-IDL release has been deleted from CPAN. 1.03 2013-09-02 NEILB - Added links to enumeration modules in SEE ALSO section. - Failed to index, due to clash with Enum module in CORBA-IDL 1.02 2013-09-01 NEILB - Added links to other modules and my review, in SEE ALSO - Failed to index, due to clash with PAUSE permissions for Enum module 1.016_01 2013-08-27 NEILB - Neil Bowers (NEILB) has taken over maintenance from Byron (thanks Byron!) - Reformatted this file (Changes) as per CPAN::Changes::Spec - Some of the revision history was in this file, some was in the pod; I think I've merged them correctly. - Added licence and repository metadata to Makefile.PL 1.016 1999-05-27 ZENIN - Fixed bug that caused bitwise operators to treat enum types as strings instead of numbers. - Last release by ZENIN (Byron Brummer) 1.015 1999-05-22 ZENIN - Add support for negative values. - Added stricter hex value checks. 1.014 1999-05-13 15:58:18 ZENIN - Added support for non-decimal numeric representations ala 0x123, 0644, and 123_456. - First version committed to CVS. - Fixed bug in hex index code that broke on 0xA. 1.013 1999-05-13 10:52:30 ZENIN - Fixed auto-index bugs in new non-decimal numeric support. 1.012 1999-05-13 10:00:45 ZENIN - Added support for non-decimal numeric representations ala 0x123, 0644, and 123_456. 1.011 1998-07-18 ZENIN - Added BITMASK and ENUM directives. - Revamped documentation. 1.010 1998-06-12 ZENIN - Removed test code - Released to CPAN 1.009 1998-06-11 ZENIN - Fixed -w warning when a null tag is used 1.008 1998-06-11 ZENIN - Fixed documentation bugs - Moved A..Z case to last as it's not going to be used as much as the other cases. 1.007 1998-06-10 ZENIN - Changed interface to match original design by Tom Phoenix as implemented in an early version of enum.pm by Benjamin Holzman. - Changed tag syntax to not require the 'PREFIX' string of Tom's interface. - Allow multiple prefix tags to be used at any point. - Allowed index value changes from tags. 1.006 1998-06-10 ZENIN - Fixed superfulous -w warning 1.004 1998-06-10 ZENIN - Changed behaver to closer resemble C enum types - Changed docs to match new behaver enum-1.11/INSTALL000644 000765 000024 00000000315 06716654120 013673 0ustar00neilbstaff000000 000000 Basic perl module install: gunzip -c enum-1.014.tar.gz | tar -xvf - cd enum-1.012 perl Makefile.pl make make test make install enum-1.11/lib/000755 000765 000024 00000000000 12613767406 013416 5ustar00neilbstaff000000 000000 enum-1.11/Makefile.PL000644 000765 000024 00000001763 12270600415 014612 0ustar00neilbstaff000000 000000 use ExtUtils::MakeMaker; # See lib/ExtUtils/MakeMaker.pm for details of how to influence # the contents of the Makefile that is written. my $mm_ver = $ExtUtils::MakeMaker::VERSION; if ($mm_ver =~ /_/) { # dev version $mm_ver = eval $mm_ver; die $@ if $@; } WriteMakefile( 'NAME' => 'enum', 'VERSION_FROM' => 'lib/enum.pm', # finds $VERSION PREREQ_PM => { 'Carp' => 0, 'strict' => 0, 'warnings' => 0, }, ($mm_ver >= 6.31 ? (LICENSE => 'perl') : ()), ($mm_ver <= 6.45 ? () : (META_MERGE => { 'meta-spec' => { version => 2 }, resources => { bugtracker => 'https://rt.cpan.org/Public/Dist/Display.html?Name=enum', repository => { type => 'git', web => 'https://github.com/neilbowers/enum', url => 'git://github.com/neilbowers/enum.git', }, }, })), ($mm_ver >= 6.48 ? (MIN_PERL_VERSION => 5.006) : () ), ); enum-1.11/MANIFEST000644 000765 000024 00000000446 12613767407 014006 0ustar00neilbstaff000000 000000 Changes README INSTALL MANIFEST Makefile.PL lib/enum.pm t/dot_dot.t t/new_index.t t/new_package.t t/new_tag.t t/simple_tags.t META.yml Module YAML meta-data (added by MakeMaker) META.json Module JSON meta-data (added by MakeMaker) enum-1.11/META.json000644 000765 000024 00000002203 12613767407 014267 0ustar00neilbstaff000000 000000 { "abstract" : "unknown", "author" : [ "unknown" ], "dynamic_config" : 1, "generated_by" : "ExtUtils::MakeMaker version 7.02, CPAN::Meta::Converter version 2.150005", "license" : [ "perl_5" ], "meta-spec" : { "url" : "http://search.cpan.org/perldoc?CPAN::Meta::Spec", "version" : "2" }, "name" : "enum", "no_index" : { "directory" : [ "t", "inc" ] }, "prereqs" : { "build" : { "requires" : { "ExtUtils::MakeMaker" : "0" } }, "configure" : { "requires" : { "ExtUtils::MakeMaker" : "0" } }, "runtime" : { "requires" : { "Carp" : "0", "perl" : "5.006", "strict" : "0", "warnings" : "0" } } }, "release_status" : "stable", "resources" : { "repository" : { "type" : "git", "url" : "git://github.com/neilbowers/enum.git", "web" : "https://github.com/neilbowers/enum" } }, "version" : "1.11", "x_serialization_backend" : "JSON::PP version 2.27203" } enum-1.11/META.yml000644 000765 000024 00000001130 12613767406 014114 0ustar00neilbstaff000000 000000 --- abstract: unknown author: - unknown build_requires: ExtUtils::MakeMaker: '0' configure_requires: ExtUtils::MakeMaker: '0' dynamic_config: 1 generated_by: 'ExtUtils::MakeMaker version 7.02, CPAN::Meta::Converter version 2.150005' license: perl meta-spec: url: http://module-build.sourceforge.net/META-spec-v1.4.html version: '1.4' name: enum no_index: directory: - t - inc requires: Carp: '0' perl: '5.006' strict: '0' warnings: '0' resources: repository: git://github.com/neilbowers/enum.git version: '1.11' x_serialization_backend: 'CPAN::Meta::YAML version 0.016' enum-1.11/README000644 000765 000024 00000001752 12207217636 013527 0ustar00neilbstaff000000 000000 The 'enum' module for Perl This module is used to efine a set of symbolic constants with ordered numeric values similar to enum types in the C programming language. Now capable of creating creating ordered bitmask constants as well. See the 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 use strict. That is, if you misspell Days_Fri as Days_Fry, you'll generate a compile error. enum-1.11/t/000755 000765 000024 00000000000 12613767406 013113 5ustar00neilbstaff000000 000000 enum-1.11/t/dot_dot.t000644 000765 000024 00000001144 06573265734 014741 0ustar00neilbstaff000000 000000 use strict; use vars qw($test $ok $total); sub OK { print "ok " . $test++ . "\n" } sub NOT_OK { print "not ok " . $test++ . "\n"}; BEGIN { $test = 1; $ok=0; $| = 1 } END { NOT_OK unless $ok } use enum; $ok++; OK; use enum qw(Foo Bar Cat Dog); use enum qw( :Months_=0 Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec :Days_ Sun=0 Mon Tue Wed Thu Fri Sat :Letters_=0 A..Z :=0 : A..Z Ten=10 Forty=40 FortyOne FortyTwo Zero=0 One Two Three=3 Four :=100 ); #2 (Letters_A != 0 or Letters_Z != 25) ? NOT_OK : OK; #3 (A != 0 or Z != 25) ? NOT_OK : OK; BEGIN { $total = 3; print "1..$total\n" } enum-1.11/t/new_index.t000644 000765 000024 00000001252 06573265734 015265 0ustar00neilbstaff000000 000000 use strict; use vars qw($test $ok $total); sub OK { print "ok " . $test++ . "\n" } sub NOT_OK { print "not ok " . $test++ . "\n"}; BEGIN { $test = 1; $ok=0; $| = 1 } END { NOT_OK unless $ok } use enum; $ok++; OK; use enum qw(Foo Bar Cat Dog); use enum qw( :Months_=0 Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec :Days_ Sun=0 Mon Tue Wed Thu Fri Sat :Letters_=0 A..Z :=0 : A..Z Ten=10 Forty=40 FortyOne FortyTwo Zero=0 One Two Three=3 Four :=100 ); #2 (Zero != 0 or One != 1 or Two != 2 or Three != 3 or Four != 4) ? NOT_OK : OK; #3 (Ten != 10 or Forty != 40 or FortyOne != 41 or FortyTwo != 42) ? NOT_OK : OK; BEGIN { $total = 3; print "1..$total\n" } enum-1.11/t/new_package.t000644 000765 000024 00000001425 06573265734 015553 0ustar00neilbstaff000000 000000 use strict; use vars qw($test $ok $total @foo); sub OK { print "ok " . $test++ . "\n" } sub NOT_OK { print "not ok " . $test++ . "\n"}; BEGIN { $test = 1; $ok=0; $| = 1 } END { NOT_OK unless $ok } use enum; $ok++; OK; use enum qw(Foo Bar Cat Dog); use enum qw( :Months_=0 Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec :Days_ Sun=0 Mon Tue Wed Thu Fri Sat :Letters_=0 A..Z :=0 : A..Z Ten=10 Forty=40 FortyOne FortyTwo Zero=0 One Two Three=3 Four :=100 ); $foo[Foo] = "Foo"; $foo[Bar] = "Bar"; $foo[Cat] = "Cat"; $foo[Dog] = "Dog"; { package main::F; use enum qw(Foo Bar Cat Dog); } if ( $foo[F::Foo] ne "Foo" or $foo[F::Bar] ne "Bar" or $foo[F::Cat] ne "Cat" or $foo[F::Dog] ne "Dog" ) { NOT_OK; } else { OK; } BEGIN { $total = 2; print "1..$total\n" } enum-1.11/t/new_tag.t000644 000765 000024 00000001162 06573265734 014731 0ustar00neilbstaff000000 000000 use strict; use vars qw($test $ok $total); sub OK { print "ok " . $test++ . "\n" } sub NOT_OK { print "not ok " . $test++ . "\n"}; BEGIN { $test = 1; $ok=0; $| = 1 } END { NOT_OK unless $ok } use enum; $ok++; OK; use enum qw(Foo Bar Cat Dog); use enum qw( :Months_=0 Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec :Days_ Sun=0 Mon Tue Wed Thu Fri Sat :Letters_=0 A..Z :=0 : A..Z Ten=10 Forty=40 FortyOne FortyTwo Zero=0 One Two Three=3 Four :=100 ); #2 (Months_Apr != 3 or Months_Dec != 11) ? NOT_OK : OK; #3 (Days_Thu != 4 or Days_Sat != 6) ? NOT_OK : OK; BEGIN { $total = 3; print "1..$total\n" } enum-1.11/t/simple_tags.t000644 000765 000024 00000001403 06573265734 015612 0ustar00neilbstaff000000 000000 use strict; use vars qw($test $ok $total @foo); sub OK { print "ok " . $test++ . "\n" } sub NOT_OK { print "not ok " . $test++ . "\n"}; BEGIN { $test = 1; $ok=0; $| = 1 } END { NOT_OK unless $ok } use enum; $ok++; OK; use enum qw(Foo Bar Cat Dog); use enum qw( :Months_=0 Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec :Days_ Sun=0 Mon Tue Wed Thu Fri Sat :Letters_=0 A..Z :=0 : A..Z Ten=10 Forty=40 FortyOne FortyTwo Zero=0 One Two Three=3 Four :=100 ); $foo[Foo] = "Foo"; $foo[Bar] = "Bar"; $foo[Cat] = "Cat"; $foo[Dog] = "Dog"; #2 (Foo != 0 or Bar != 1 or Cat != 2 or Dog != 3) ? NOT_OK : OK; #3 ($foo[Foo] ne "Foo" or $foo[Bar] ne "Bar" or $foo[Cat] ne "Cat" or $foo[Dog] ne "Dog") ? NOT_OK : OK; BEGIN { $total = 3; print "1..$total\n" } enum-1.11/lib/enum.pm000644 000765 000024 00000025212 12613767176 014726 0ustar00neilbstaff000000 000000 package enum; use 5.006; use strict; use warnings; no strict 'refs'; # Let's just make this very clear right off use Carp; our $VERSION = '1.11'; 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$_"} = eval "sub () { $n }"; } ## Index change elsif (/^($Ident)=(-?)(.+)$/o) { my $name= $1; my $neg = $2; $index = $3; ## Convert non-decimal numerics to decimal if ($index =~ /^0x[0-9a-f]+$/i) { ## Hex $index = hex $index; } elsif ($index =~ /^0[0-9]/) { ## Octal $index = oct $index; } elsif ($index !~ /[^0-9_]/) { ## 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"} = eval "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[0-9a-f]+$/i) { ## Hex $index = hex $index; } elsif ($index =~ /^0[0-9]/) { ## Oct $index = oct $index; } elsif ($index !~ /[^0-9_]/) { ## 123_456 notation $index =~ s/_//g; } ## Force numeric context, but only in numeric context if ($index =~ /[^0-9]/) { $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"} = eval "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 This module is used to define a set of constants with ordered numeric values, similar to the C 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). 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 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. That is, if you misspell B as B, you'll generate a compile error. =head1 BITMASKS The B 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 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 manpage. You can set a starting index for bitmasks just as you can for normal B 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 B directive, the index is automatically set to 1. If you wish to go back to normal B mode, use the B directive. Similarly to the B directive, the B 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 equal 1, 2, 4 and 8 respectively. B equal 0 and 1. B also equal 0 and 1. And B 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, L, L, L, L, L. If you're using L, then L may be of interest. L is part of the L distribution. There are many CPAN modules related to defining constants in Perl; here are some of the best ones: L, L, L, L. Neil Bowers has written a L, which covers all such modules. =head1 REPOSITORY L =head1 AUTHOR Originally written by Byron Brummer (ZENIN), now maintained by Neil Bowers Eneilb@cpan.orgE. Based on early versions of the B 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