namespace-alias
view release on metacpan or search on metacpan
lib/namespace/alias.pm view on Meta::CPAN
use strict;
use warnings;
package namespace::alias;
use 5.008001;
use XSLoader;
use Class::MOP;
use B::Hooks::OP::Check;
use B::Hooks::EndOfScope;
our $VERSION = '0.02';
XSLoader::load(__PACKAGE__, $VERSION);
sub import {
my ($class, $package, $alias) = @_;
Class::MOP::load_class($package);
($alias) = $package =~ /(?:::|')(\w+)$/
unless defined $alias;
my $file = (caller)[1];
my $hook = $class->setup($file => sub {
my ($str) = @_;
if ($str =~ s/^$alias\b/$package/) {
return $str;
}
return;
});
on_scope_end {
$class->teardown($hook);
};
}
1;
__END__
=head1 NAME
namespace::alias - Lexical aliasing of namespaces
=head1 SYNOPSIS
use namespace::alias 'My::Company::Namespace::Customer';
# plain aliasing of a namespace
my $cust = Customer->new; # My::Company::Namespace::Customer->new
# namespaces relative to the alias
my $pref = Customer::Preferred->new; # My::Company::Namespace::Customer::Preferred->new
# getting the expansion of an alias
my $customer_class = Customer;
# also works for packages relative to the alias
my $preferred_class = Customer::Preferred;
# calling a function in an aliased namespace
Customer::some_func()
=head1 DESCRIPTION
This module allows you to load packages and use them with a shorter name within
a lexical scope.
This is how you load a module and install an alias for it:
use namespace::alias 'Some::Class';
This will load C<Some::Class> and install the alias C<Class> for it. You may
also specify the name of the alias explicitly:
use namespace::alias 'Some::Class', 'MyAlias';
This will load C<Some::Class> and install the alias C<MyAlias> for it.
After installing the alias, every method or function call using it will be
( run in 0.612 second using v1.01-cache-2.11-cpan-aadc1410aed )