Skip to main content

Perl5, Moxie and Enumurated Data Types

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.

Entering Unicode CHaracters

If you're wondering how to enter unicode characters, I used C-x 8 hex code in emacs. Actually, you can enter either a hex code or the unicode name. Of course, if you are going to use a character frequently, you would want to assign it tp a key cord ... emacs provides a surplus of those.


Anonymous said…
What about cperl? It has builtin class support and is measurably faster.
Matthew Persico said…
How does one subscribe to this blog to be notified of new posts?
Tom Legrady said…
What about cperl? I'm writing about Moxie. You're welcome to write about cperl.
Tom Legrady said…
Matthew, I recently took advantage of the option to apply themes to the blog, replacing the boring old standard format. I missed out on the 'subscribe' link. I've now clicked the right place and it's there at the top of the screen.
Matthew Persico said…
Works! Looking forward to future posts.

Popular posts from this blog

Creating Perl5 Objects with Moxie

Having in the previous article prepared data types for car suits and card ranks, I can now combine them to provide a playing card class, using Stevan Little's Moxie module (version 0.04, so definitely early days.) The goal is to provide an object-oriented paradigm to the Perl 5 programming language which is more sophisticated, more powerful and less verbose than manually bless()-ing hashes. To achieve that goal it needs to be faster and light-weight compared to Moose. Currently, and and are add-on modules, but eventually, when they are more complete, when the wrinkles have been ironed out, and when they have gained acceptance and a community of users, they might be merged into the Perl core.

One significant feature of Moxie is that it reduces boilerplate code. You don't have to specify warnigns or strict. As well, the features or the perl you are using are enabled, among them say, state, signatures, and post_deref.
A Simple Moxie Class package Card { …

Adventures in Autovivification

Having recently started a new job, I was exposed to old code with multi-step tests against autovivification in multi-level hashes. You get used to the code you have seen, but in a new environment it‘s irritating and jarring.
Moose does not generally have the problem, first because class structure is pre-declared, because values are accessed using accessor functions rather than directly, and because responsibility is delegated down to attributes, avoiding long chains. On the other hand, Moose has it's own overhead, so hand-rolled objects, and bare structures still have their use.
If you don‘t protect against autovivification, then mis-spelling a key, or referencing keys which haven‘t been instantiated in this instance, causes those keys to instantly come into existence.
#!/usr/bin/perl use warnings; use strict; use Data::Dump 'dump'; use 5.024; my $var = {key1 => {key2 => {key3 => 'a'}}}; say dump $var; if ( $var->{key1}{key2}{key3b}[13]{foob…