When I develop a module, it's boring to repeat the long namespace everywhere, when I call a submodule.
For example :
tree lib
|--- My
|--- Module
|--- Long
|--- Long
|--- NameSpace.pm
|--- NameSpace
|--- Database
|--- Main.pm
|--- Main
|--- Result
|--- User.pm
|--- Utils
|--- Params.pm
tree bin
|--- test.pl
For example in "test.pl" :
use strict; use warnings; use 5.012; use My::Module::Long::Long::NameSpace::Utils::Params; use My::Module::Long::Long::NameSpace::Database::Main; my $params = use My::Module::Long::Long::NameSpace::Utils::Params->new(user => 1); my $schema = use My::Module::Long::Long::NameSpace::Database::Main->connect($dsn); my ($user) = $schema->find($params->user); say "Username : ",$user->name;
Well you can have a lots of this kind of call in any submodule in your namespace.
To simplify the all things, I do this in my main module namespace :
package My::Module::Long::Long::NameSpace;
use strict;
use warnings;
use Module::Load;
#export function that will be use in every submodule of the namespace
use base qw(Exporter);
use vars qw(@EXPORT);
@EXPORT = qw(ns_name ns_load);
#return the real full name of your module, relative to this package
sub ns_name {
return join('::', __PACKAGE__, @_);
}
#use the load of Module::Load by changing the first arg of the module. I use goto to switch to the real load function after the modification of params. It preserve the original call and don't add this function in the context.
sub ns_load {
unshift @_, ns_name(shift);
goto &load;
}
1;
So now your "test.pl" look like this :
use strict;
use warnings;
use 5.012;
use My::Module::Long::Long::NameSpace;
ns_load('Utils::Params');
ns_load('Database::Main');
my $params = ns_name('Utils::Params')->new(user => 1);
my $schema = ns_name('Database::Main')->connect($dsn);
my ($user) = $schema->find($params->user);
say "Username : ",$user->name;
Well, having fun with perl !