This is pretty clean, put your private stuff into a sub-package and alias that long package path it to a shorter package name at compile time.
(of course these subs won't be able to call imported subs in the parent package without full qualification or inheritance ;-)
use strict;
use warnings;
package X::Y::Z::my; # sub package
sub xyz {warn "xyz called" }
package Y::Y::Z;
BEGIN {
*::my:: = *X::Y::Z::my:: ; # alias sub package to my::
}
# END { # delete alias at file's end
# delete $::{"my::"};
# }
my::xyz();
BEGIN { # delete alias here for demo
delete $::{"my::"};
}
package Other;
my::xyz(); # fails alias already gone
C:/Perl_524/bin\perl.exe d:/exp/local_package.pl
xyz called at d:/exp/local_package.pl line 6.
Undefined subroutine &my::xyz called at d:/exp/local_package.pl line 3
+0.
update
the magic in the BEGIN/END blocks could be done in the importer of a module called with
use PackageAlias "X::Y:Z::my" => "my" and
no PackageAlias "my"
Update
I just realised that Package::Alias already exists, even with a pretty similar interface.
Skipped colons in my example to avoid confusion.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|