If I have a really long package name, I can alias that package by making an entry in the symbol table:
BEGIN {
# Make "Alias" be an alias for "Some::Really::Long::Package";
*Alias:: = \*Some::Really::Long::Package::;
# Equivalent to:
# *main::Alias:: = \*Some::Really::Long::Package::;
}
This is what something like Package::Alias
does for you internally. However, this stinks because it mucks with the main
package. How can I make the alias only affect the current package, and be able to use just the alias within the package? I tried changing the alias definition to
*Short::Alias:: = \*Some::Really::Long::Package::;
But then I have to use Short::Alias->myMethod()
instead of just Alias->myMethod()
.
use strict;
use warnings;
package Some::Really::Long::Package;
sub myMethod {
print "myMethod\n";
}
package Short;
BEGIN {
# Make "Alias" be an alias for "Some::Really::Long::Package";
*Short::Alias:: = \*Some::Really::Long::Package::;
}
# I want this to work
Alias->myMethod();
package main;
# I want this to not work
Alias->myMethod();
Bonus points if both Alias->myMethod()
and Alias::myMethod()
work in the Short
package, and not in main
.