Enumerations

This page is under construction. Please come back later.
The
enum construct is not a built-in part of Perl. You will need to install the CPAN module Class::Enum to use it.
$ sudo CPAN install Class::Enum
#!/usr/bin/env perl
use utf8;
use Utils;
use strict;
use warnings;
package CompassPoint {
use Class::Enum qw(
NORTH EAST SOUTH WEST
);
}
sub compass_point_to_string {
my ($c) = @_;
my $result = "";
if ($c == CompassPoint::NORTH) {
$result = "North";
} elsif ($c == CompassPoint::EAST) {
$result = "East";
} elsif ($c == CompassPoint::SOUTH) {
$result = "South";
} elsif ($c == CompassPoint::WEST) {
$result = "West";
}
return $result;
}
sub turn_right {
my ($c) = @_;
if ($c == CompassPoint::NORTH) {
return CompassPoint::EAST;
} elsif ($c == CompassPoint::EAST) {
return CompassPoint::SOUTH;
} elsif ($c == CompassPoint::SOUTH) {
return CompassPoint::WEST;
} else {
return CompassPoint::NORTH;
}
}
MAIN:
{
binmode(STDOUT, ":utf8");
binmode(STDERR, ":utf8");
binmode(STDIN, ":utf8");
my $cp1 = CompassPoint::NORTH;
print "cp1: ", Utils::enumToString($cp1), "\n";
print "SOUTH: ", compass_point_to_string(CompassPoint::SOUTH), "\n";
print "turnRight(cp1): ", compass_point_to_string(turn_right($cp1)), "\n";
my $CP2 = CompassPoint::EAST;
if ($cp1 == $CP2) {
print "cp1 == cp2\n";
} else {
print "cp1 != cp2\n";
}
$cp1 = $CP2;
if ($cp1 == $CP2) {
print "cp1 == cp2\n";
} else {
print "cp1 != cp2\n";
}
}
Output
$ perl Enums1.pl
cp1: NORTH
SOUTH: South
turnRight(cp1): East
cp1 != cp2
cp1 == cp2
#!/usr/bin/env perl
use utf8;
use Utils;
use strict;
use warnings;
package Color {
use Class::Enum (
"RED" => { ordinal => 1 },
"GREEN" => { ordinal => 2 },
"YELLOW" => { ordinal => 3 },
"BLUE" => { ordinal => 4 },
"MAGENTA" => { ordinal => 5 },
"CYAN" => { ordinal => 6 }
);
}
sub combine_colors {
my ($c1, $c2) = @_;
my $i1 = $c1->ordinal;
my $i2 = $c2->ordinal;
my $RESULT = Utils::bitwiseOr32($i1, $i2);
my $RETURN_COLOR = Color->from_ordinal($RESULT);
return $RETURN_COLOR;
}
MAIN:
{
binmode(STDOUT, ":utf8");
binmode(STDERR, ":utf8");
binmode(STDIN, ":utf8");
my $c1 = Color::BLUE;
print "c1: ", Utils::enumToString($c1), "\n";
print "RED: ", Utils::enumToString(Color::RED), "\n";
print "combineColors(c1, RED): ", Utils::enumToString(combine_colors($c1, Color::RED)), "\n";
my $c2 = Color::GREEN;
print "combineColors(c1, c2): ", Utils::enumToString(combine_colors($c1, $c2)), "\n";
if ($c1 == $c2) {
print "c1 == c2\n";
} else {
print "c1 != c2\n";
}
$c1 = $c2;
if ($c1 == $c2) {
print "c1 == c2\n";
} else {
print "c1 != c2\n";
}
my $BAD = Color->from_ordinal(7);
print "bad: ", Utils::enumToString($BAD), "\n";
}
Output
$ perl Enums2.pl
Use of uninitialized value in print at Enums2.pl line 50.
c1: BLUE
RED: RED
combineColors(c1, RED): MAGENTA
combineColors(c1, c2): CYAN
c1 != c2
c1 == c2
bad:
Questions
- {{Who's on first?}}
- {{Who's on second?}}
- {{Who's on third?}}
Projects
More ★'s indicate higher difficulty level.
References
- [[Perl Language Reference]]
- [[Comprehensive Perl Archive Network (CPAN)]]
- [[Beginning Perl]], Simon Cozens
- [[Perl Monks Tutorials]]
Pure Programmer


