CPAN and MooseX::Bitmask
April 23, 2009 § 2 Comments
One of the most impressive features I see about Perl, is CPAN – the Comprehensive Perl Archive Network. For those unaware, this is a massive (and I mean massive) database of Perl modules that are all packaged up ready to be used in your project. Need email validation? Then go grab Email::Valid. Need database interaction? Go grab DBI and a relevant database driver. U need 2 haz lolcat in ur app? Acme::LOLCAT is there for you (as a joke :)) Just about anything imaginable is in the CPAN, and it’s tested and package up ready for your use.
It’s hard to describe just how rapid this can make application development. My Genius program that I’m working on now needs Gnome bindings (and specifically panel-applet bindings). I thought this would be a major roadblock for me, but no – there they are, right on CPAN: Gnome2 and Gnome2::PanelApplet – sweet! I needed a robust object management system, something flexible that I could prototype with, so I just grabbed KiokuDB (more on this awesomeness later).
However, while CPAN covers 99% of my needs, there are times when things don’t do quite what I want. We use Moose at MusicBrainz, and one of our attributes represents a bitmask – I had a look on CPAN before tackling this, but non of the modules really took my fancy. So I’ve written my own, and hopefully this will be my first CPAN contribution soon 🙂 Behold, MooseX::Bitmask (name subject to change, I expect).
This module provides an attribute metaclass and attribute traits for marking an attribute in Moose as a bitmask. It will automatically create some helper methods for you to check flags, and also another helper method to toggle a flag on and off. Here’s some example usage:
has 'flags' => (
isa => 'Int',
is => 'rw'
flags => [qw( shiney hard tasty )],
traits => [qw( Bitmask )],
Now, you can easily toggle attributes:
my $object = Foo->new;
Easy! I have some changes I want to add to this still, mostly support for renaming the provided methods (_has_flag and _toggle), and adding support for inspecting a bitmask in human form. But it’s really coming a long, and I look forward to getting this onto CPAN!