Moxie - a new object system for Perl5
Stevan Little created the Moose multiverse to upgrade the Perl 5 programming language's object-oriented system more in line with the wonderfull world of Perl 6. Unfortunately, it's grown into a bloated giant, which has inspired light-weight alternatives Moos, Moo, Mo, and others. Now he's trying to create a modern, efficient OO system that can become built into the language.I've seen a few of his presentations at YAPC (Yet Another Perl Conference, now known as TPC, The Perl Conference), among them p5 mop final final v5 this is the last one i promise tar gz<. So I was delighted to recently see an announcement of the module Moxie, and decided to try implementing a card game.
While the package provides some POD documentation about the main module, Moxie, it doesn't actually explain the enum package, Moxie::Enum. But delving into the tests directory reveals its secrets.
Creating an Enum
package Ranks {
use Moxie::Enum;
enum by_ARRAY => qw( unused 2 3 4 5 6 7 8 9 10 J Q K A );
enum by_HASH => { 2 => 2, 3 => 3, 4 => 4, 5 => 5,
6 => 6, 7 => 7, 8 => 8, 9 => 9,
10 => 10, J => 11, Q => 12, K => 13,
A => 14 };
}
In languages that provide enumerated types, the enum keyword precedes an identifier or tag, which is followed by an enumeration list. You can think of the enumeration list as being like a hash, each identifier maps to a value. If you declare the enum using a list such as qw(), the first element is assigned the value 1, with successive elements getting successive numbers. Note I wasted an enum so that the values would match up with the card values, at least until the face cards. If you want greater control over the values, you can declare the enum using a hash and explicitly specify the value for each tag. Once declared, you can access the keys and values of the enum, or the combination as hash, or you can check or access the value for a particular enum identifier.
# my %hash = Moxie::Enum::get_enum_for( PACKAGE => 'enum_TAG');
my %cards = Moxie::Enum::get_enum_for( Ranks => 'by_ARRAY' );
say "$_, $cards{$_}" for keys %cards
7, 7
unused, unused
6, 6
Q, Q
3, 3
2, 2
8, 8
5, 5
10, 10
J, J
4, 4
9, 9
K, K
A, A
say("$_, ", 0+$cards{$_}) for keys %cards
7, 7
unused, 1
6, 6
Q, 12
3, 3
2, 2
8, 8
5, 5
10, 10
J, 11
4, 4
9, 9
K, 13
A, 14
It's interesting to note that the hash value is actually a dualvar; in a string context, it's the same as the key; in a numeric context, it's a number.
say join " - ", Moxie::Enum::get_keys_for( Ranks => RANKS )
4 - A - 9 - K - 6 - Q - 7 - unused - 8 - 10 - 5 - J - 3 - 2
say join " - ", Moxie::Enum::get_values_for( Ranks => RANKS )
6 - Q - 7 - unused - 8 - 5 - J - 10 - 2 - 3 - 4 - A - 9 - K
say join " - ", map {0+$_} Moxie::Enum::get_values_for( Ranks => RANKS )
4 - 14 - 9 - 13 - 6 - 12 - 7 - 1 - 8 - 11 - 5 - 10 - 2 - 3
say Moxie::Enum::has_value_for( Ranks => RANKS, 'K' )
1
say Moxie::Enum::get_value_for( Ranks => RANKS, 'K' )
K
say 0+ Moxie::Enum::get_value_for( Ranks => RANKS, 'K' )
13
You can get the enum keys, i.e. tags, or the values, which you can force into a string or numeric context. You can check whether or not Package => enum contains a particular identifier, and what the associated value is. The only problem is the keys and values are returned in a non-deterministic order, while I would like cards to be sorted in some way until they are shuffled, and also to be able to compare and sort cards.To handle that, I add a routine to compare pairs of cards and another routine to return the list of keys sorted according to the values. Note that using the numeric comparison operator causes the enum values to be interpreted in a numeric context.
sub ranks {
return sort { compare( $a, $b ) }
grep { $_ ne 'unused' }
Moxie::Enum::get_keys_for( Ranks => 'RANKS');
}
sub compare {
return Moxie::Enum::get_value_for( Ranks => RANKS, $_[0])
<=> Moxie::Enum::get_value_for( Ranks => RANKS, $_[1]);
}
Creating Another Enum
A deck of cards needs suits as well as ranks. And unicode makes it easy to use pretty pictures, though I haven't yet figures out how to make diamonds and hearts displayed in red, rather than black. Unfortunately, unicode provides the characters in a different order than the one I consider natural, but that can be dealt with.
# U+2660 ♠ Black Spade Suit
# U+2665 ♥ Black Heart Suit
# U+2666 ♦ Black Diamond Suit
# U+2663 ♣ Black Club Suit
package Suits {
use Moxie::Enum;
enum SUITS => qw( ♣ ♦ ♥ ♠ );
sub suits {
return sort { compare( $b, $a ) }
grep { $_ ne 'unused' }
Moxie::Enum::get_keys_for( Suits => 'SUITS');
}
sub compare {
return Moxie::Enum::get_value_for( Suits => 'SUITS', $_[0])
<=> Moxie::Enum::get_value_for( Suits => 'SUITS', $_[1]);
}
}
The suits have to be declared in increasing order, clubs are least and spades are highest, so the numbers get assigned right. Then it's a simple matter to compare the values in compare(). Note the suits() routine uses compare( $b, $a) to get the cards with the highest ranked suit first.
Comments