What does that module do? aliased.pm

In my first post in this series I explained how Task::Kensho provided a nice list of modules to install to give a good basis for many common actions.

When installing some of those modules I had one of those “I wonder what that does” moments when cpanm showed that it was installing “aliased.pm”. Generally all lower case module names are reserved for pragmas and modules that provide pragma like support, so I was curious to know what this module did.

aliased.pm is written by the talented Curtis “Ovid” Poe, whose name you have most likely seen if you’ve used Perl for a while. “aliased” fills a neat little spot for the programmer who is trying to reduce the clutter in their code because it allows to you “alias” one package name to another. This means that with all of those cases of:

Company::Application::Version::Module

Can now be written so much more clearly as just

Module

It comes with a built in helper routine to shorten your package name to the last section, so all you need to do is this to start using it:


use aliased "Company::Application::Version::Module";
Module->doSomething();

If you don’t like “Module” on its own then you can define the name that is used during the use call, for example:


use aliased "Company::Application::Version::Module" => "MyModule";
MyModule->doSomething();

The downside to using this module this is that you could easily mislead a reader of this code or get name clashes, especially with the automatic aliasing. Accordingly the best option is to assign it to a scalar variable and use that. For example:


use aliased;
my $MyModule = alias "Company::Application::Version::Module";
$MyModule->doSomething();

Amazingly the basis for the code is so very simple. It effectively creates a sub of the given (or calculated) name that returns the original package name. So MyModule->doSomething() is really calling MyModule()->doSomething().

This module also has use in areas were you might change out one library for another. For example CGI => CGI::Lite.

There are definitely areas where it could add confusion, but it seems to be a useful addition to keep around.

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

%d bloggers like this: