Package::Pkg(3pm)
π NAME π
Package::Pkg β Handy package munging utilities π
(it's a Perl module bestie, so it lives in the 3pm section of the manual, no cap)
β¨ VERSION β¨
version 0.0020 π
π¦ SYNOPSIS π¦
First, import a new keyword: pkg β¨
use Package::Pkg;
Package name formation:
pkg->name( 'Xy', 'A' ) # Xy::A
pkg->name( $object, qw/ Cfg / ); # (ref $object)::Cfg
Subroutine installation:
pkg->install( sub { ... } => 'MyPackage::myfunction' );
# myfunction in MyPackage is now useable
MyPackage->myfunction( ... );
Subroutine exporting:
package MyPackage;
use Package::Pkg;
sub this { ... }
# Setup an exporter (literally sub import { ... }) for
# MyPackage, exporting 'this' and 'that'
pkg->export( that => sub { ... }, 'this' );
package main;
use MyPackage;
this( ... );
that( ... );
πΈ DESCRIPTION πΈ
Package::Pkg is literally just a lil collection of useful, miscellaneous package-munging utilities π«Ά
You get at all the functionality through the imported pkg keyword β the moment β but ngl you can also just invoke the functions directly from the package itself (Package::Pkg) if that's your vibe π
π USAGE π
pkg->install( ... )
Install a subroutine, similar to Sub::Install π
This method takes a number of parameters and also has a two- and three-argument form (see below) π
# Install an anonymous subroutine as Banana::magic
pkg->install( code => sub { ... } , as => 'Banana::magic' )
pkg->install( code => sub { ... } , into => 'Banana::magic' ) # Bzzzt! Throws an error!
# Install the subroutine Apple::xyzzy as Banana::magic
pkg->install( code => 'Apple::xyzzy', as => 'Banana::magic' )
pkg->install( code => 'Apple::xyzzy', into => 'Banana', as => 'magic' )
pkg->install( from => 'Apple', code => 'xyzzy', as => 'Banana::magic' )
pkg->install( from => 'Apple', code => 'xyzzy', into => 'Banana', as => 'magic' )
# Install the subroutine Apple::xyzzy as Banana::xyzzy
pkg->install( code => 'Apple::xyzzy', as => 'Banana::xyzzy' )
pkg->install( code => 'Apple::xyzzy', into => 'Banana' )
pkg->install( from => 'Apple', code => 'xyzzy', as => 'Banana::xyzzy' )
pkg->install( from => 'Apple', code => 'xyzzy', into => 'Banana' )
With implicit from (via caller()) π
package Apple;
sub xyzzy { ... }
# Install the subroutine Apple::xyzzy as Banana::xyzzy
pkg->install( code => 'xyzzy', as => 'Banana::xyzzy' ) # 'from' is implicitly 'Apple'
pkg->install( code => \&xyzzy, as => 'Banana::xyzzy' )
Acceptable parameters are:
codeβ a subroutine reference, a package-with-name identifier, or the name of a subroutine in the calling packagefrom(optional) β a package identifier- If
:codeis an identifier, then:fromis the package where the subroutine can be found - If
:codeis an identifier and:fromis not given, then:fromis assumed to be the calling package (viacaller())
- If
asβ the name of the subroutine to install as. Can be a simple name (when paired with:into) or a full package-with-nameinto(optional) β a package identifier- If
:asis given, then the full name of the installed subroutine is(:into)::(:as) - If
:asis not given and we can derive a simple name from:code(it is a package-with-name identifier), then:aswill be the name identifier part of:code
- If
pkg->install( $code => $as )
This is the two-argument form of subroutine installation π
Install $code subroutine as $as β¨
pkg->install( sub { ... } => 'Banana::xyzzy' )
pkg->install( 'Scalar::Util::blessed' => 'Banana::xyzzy' )
pkg->install( 'Scalar::Util::blessed' => 'Banana::' )
pkg->install( sub { ... } => 'Banana::' ) # Bzzzt! Throws an error!
$code should be:
- A CODE reference
sub { ... }
- A package-with-name identifier
Scalar::Util::blessed
- The name of a subroutine in the calling package
sub xyzzy { ... }
pkg->install( 'xyzzy' => ... )
$as should be:
- A package-with-name identifier
Acme::Xyzzy::magic
- A package identifier (with a trailing
::)
Acme::Xyzzy::
pkg->install( $code => $into, $as )
This is the three-argument form of subroutine installation π
pkg->install( sub { ... } => 'Banana', 'xyzzy' )
pkg->install( sub { ... } => 'Banana::', 'xyzzy' )
pkg->install( 'Scalar::Util::blessed' => 'Banana', 'xyzzy' )
pkg->install( 'Scalar::Util::blessed' => 'Banana::', 'xyzzy' )
$code can be the same as the two argument form π
$into should be:
- A package identifier (trailing
::is optional)
Acme::Xyzzy::
Acme::Xyzzy
$as should be:
- A name (the name of the subroutine)
xyzzy
magic
$package = pkg->name( $part, [ $part, ..., $part ] )
Return a namespace composed by joining each $part with :: π§
Superfluous/redundant :: are automatically cleaned up and stripped from the resulting $package βοΈ
If the first part leads with a ::, the the calling package will be prepended to $package π
pkg->name( 'Xy', 'A::', '::B' ) # Xy::A::B
pkg->name( 'Xy', 'A::' ) # Xy::A::
{
package Zy;
pkg->name( '::', 'A::', '::B' ) # Zy::A::B
pkg->name( '::Xy::A::B' ) # Zy::Xy::A::B
}
In addition, if any part is blessed, name will resolve that part to the package that the part makes reference to πͺ
my $object = bless {}, 'Xyzzy';
pkg->name( $object, qw/ Cfg / ); # Xyzzy::Cfg
π SEE ALSO π
Sub::InstallSub::Exporter
π AUTHOR π
Robert Krimen <robertkrimen@gmail.com>
π COPYRIGHT AND LICENSE π
This software is copyright (c) 2012 by Robert Krimen.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
okay bestie that's the whole page, you ate that π
perl v5.32.0, dated 2021-01-05, and it's the Package::Pkg(3pm) manual β go forth and munge some packages, ily π«Άβ¨
π¦ where this page comes from
- debian bookworm: libpackage-pkg-perl 0.0020-1.1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libpackage-pkg-perl 0.0020-1.1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu jammy: libpackage-pkg-perl 0.0020-1.1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libpackage-pkg-perl 0.0020-1.1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms