Handy module of the day: aliased

December 6, 2009 § Leave a comment

A recent hacker on MusicBrainz showed me this module in a recent patch he submitted, and I’m baffled I hadn’t discovered it earlier!

aliased is a very simple module that allows you to alias long namespaces into a shorter one, or rename packages. This has a ton of uses, and here’s a very basic use:

class MyLibrary::SomePath::GodThisIsLong::Person {
    has name => ( is => 'rw' );
};

package MyApp;
use aliased 'MyLibrary::SomePath::GodThisIsLong::Person';
 
my $person = Person->new( name => 'Enlightened Developer' );

This alone enhances readability; in the cases where you are creating a lot of object, you remove a lot of the noise in your code.

You can be a bit more specific with your aliasing, and alias stuff into a new name. Here’s a very contrived example to demonstrate this too:

class MyLibrary::Person::V2 {
   has [qw( first_name surname )] => ( is => 'rw' )
};

package MyApp;
use aliased 'MyLibrary::Person::V2' => 'Person';

my $person = Person->new( first_name => 'Enlightened', surname => 'Hacker' );

Extremely simple module, but I love it already!

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

What’s this?

You are currently reading Handy module of the day: aliased at Cycles.

meta

%d bloggers like this: