Some namespaces are long and annoying. Lets say that i downloaded hypothetical package called FooFoo-BarBar-BazBaz.tar.gz, and it has the following modules:
FooFoo::BarBar::BazBaz::Bill
FooFoo::BarBar::BazBaz::Bob
FooFoo::BarBar::BazBaz::Ben
FooFoo::BarBar::BazBaz::Bozo
FooFoo::BarBar::BazBaz::Brown
FooFoo::BarBar::BazBaz::Berkly
FooFoo::BarBar::BazBaz::Berkly::First
FooFoo::BarBar::BazBaz::Berkly::Second
Is there a module or technique I can use that’s similar to the C++ ‘using’ statement, i.e., is there a way I can do
using FooFoo::BarBar::BazBaz;
which would then let me do
my $obj = Brown->new();
ok $obj->isa('FooFoo::BarBar::BazBaz::Brown') ; # true
# or...
ok $obj->isa('Brown'); # also true
The aliased pragma does this:
aliasedis syntactic sugar forThe downside of this is that normal usage of package names as arguments is done with quoted strings:
But the constant subroutine needs to be a bare word:
Which just seems like a bug waiting to happen.
Alternatively, you could just use Perl’s builtin support for namespace aliasing:
Regarding the
->isa('shortname')requirement, the aliased stash method works with quoted strings as usual:The effect of a compile time alias
BEGIN {*short:: = *long::package::name::}is global across all packages and scopes. This is fine as long as you pick an empty package to alias into.