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 };
 }
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
  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
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]);
    }
}
Comments