pkg - transparently use packages and inner packages


# standard operations
# works on either inner or normal packages
# -------------------------------       ----------------
use pkg 'A';                       #=>  use A;
use pkg 'A', 'a', 'b';             #=>  use A 'a', 'b';
use pkg 'A', [];                   #=>  use A ();

# extra operations

# default alias for a class package
use pkg -alias => 'A::B::C';
C->new(...); #equivalent to A::B::C->new();

# specific alias for a class package
use pkg 'A::B::C' => -as => 'ABC';
ABC->new( ); # equivalent to A:B::C->new;

# multiple packages
use pkg [ 'A::B::C' => -as => 'ABC'],
        [ 'A::B'    => -as => 'AB' ];


pkg extends the standard "use" statement to more transparently handle inner packages, additionally incorporating some of the functionality of the aliased and use pragmata.

An inner package is one which does not have the same name as the (fully qualified) module in which it is defined. For example, if A.pm contains

package A;

sub a { ... }

package A::B;

sub ab { ... }

package A::C;

sub ac { ... }


packages A::B and A::C are inner packages. The use statement (as well as most pragmata dealing with modules) do not handle inner packages. Some (such as parent) do, but require the user (via the -norequire option) to know if the package is inner or not.

For example, after loading the above module:

use A;

You could simply call


But, what if package A::B exported ab? Its import routine is not automatically called when A is loaded. If you try to do this

use A::B 'ab';

you'll get an error from Perl as it tries to search for a file named e.g., A/B.pm. It doesn't check to see if the A::B package has been loaded.

Instead, you'd need to do this:

A::B->import( 'ab' );

Or, using pkg:

use pkg [ 'A' ], [ 'A::B' => qw[ ab ] ];


Copyright (c) 2013 Diab Jerius

pkg is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.


Diab Jerius djerius@cpan.org