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:

package Foo;
use Moose;
use MooseX::Bitmask;

has 'flags' => (
isa => 'Int',
is => 'rw'
flags => [qw( shiney hard tasty )],
traits => [qw( Bitmask )],

Now, you can easily toggle attributes:

my $object = Foo->new;
$object->flags_has_flag('shiney') #true

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!


§ 2 Responses to CPAN and MooseX::Bitmask

  • Tomas Doran says:

    Ohh, cute idea, shiney.

    One of the other things that I’d want is to be able to have additive bitmasks.

    The canonical example I can think of wanting this is for a logger. You have debug/info/warn/error/fatal, and setting one of those turns all the rest on.

    I recently implemented this (as two bitmask hashes) for Catalyst::Log, to finally make it sane(ish), but it’d be nice to pull all that grunge out in some way and not have to deal with it πŸ™‚

    • acidcycles says:

      Interesting – I hadn’t thought of that one! I’ll add a set_inclusive method (can’t think of a better now) and see if I can do that πŸ™‚ I’ve also really got to get this pushed out to CPAN – I was waiting for trait support in MX::AH, but I think I’ll release this first – then patch MX::AH

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

What’s this?

You are currently reading CPAN and MooseX::Bitmask at Cycles.


%d bloggers like this: