Chapter 4. Working with Modules
Hacks 28-42
Perhaps the greatest invention of Perl 5 is the idea of modules. They allow people to modify the language and reuse code far beyond what Larry and the Perl 5 porters ever envisioned. (Who could have predicted CPAN or Acme::*, for example?)
If you're doing any serious work with Perl, you'll spend a lot of time working with modules: installing them, upgrading them, loading them, working around weird and unhelpful features, and even distributing them. It makes a lot of sense to understand how Perl and modules interact and how to work with them effectively.
Here are several ideas that show off the varied ways that you can extend your programs. CPAN is only an arm's length away. Be ready.
Hack #28. Shorten Long Class Names
Type only what you need to type. You know what you mean.
Are you tired of using Perl
classes with Really::Long::Package::Names::You::Cant::Remember? Use aliased and
forget about them. This handy CPAN module creates short, easy-to-remember aliases for long class names.
The Hack
Given the hypothetical example just cited, use aliased to load the class and create an alias all at once:
use aliased 'Really::Long::Package::Names::You::Cant::Remember'; my $rem = Remember->new( );
When aliased loads a class, it automatically creates a constant subroutine, in the local name space named after the final part of the package name. This subroutine returns the full package name. Because it's a constant, it's actually very efficient; Perl will ...