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!
Leave a Reply