Sub::W...ackages(3pm)
โจ Sub::W...ackages โจ
okay bestie, so this is Sub::W...ackages, a 3pm page from the User Contributed Perl Documentation collection ๐ buckle up because this module is lowkey unhinged (affectionate) and I'm gonna walk you through every single crumb of it.
๐ NAME ๐
Sub::WrapPackages โ add pre- and post-execution wrappers around all the subroutines in packages, or around individual subs ๐ซถ
๐ฆ SYNOPSIS ๐ฆ
use Sub::WrapPackages
packages => [qw(Foo Bar Baz::*)], # wrap all subs in Foo and Bar
# and any Baz::* packages
subs => [qw(Barf::a, Barf::b)], # wrap these two subs as well
wrap_inherited => 1, # and wrap any methods
# inherited by Foo, Bar, or
# Baz::*
except => qr/::w[oi]bble$/, # but don't wrap any sub called
# wibble or wobble
pre => sub {
print "called $_[0] with params ".
join(', ', @_[1..$#_])."\n";
},
post => sub {
print "$_[0] returned $_[1]\n";
};
๐ COMPATIBILITY ๐
so. this module does broadly the same job as the 1.x versions did, but ngl the interface may have changed incompatibly. sorry ๐ญ hopefully it'll be more maintainable and slightly less crazily magical though. also, caller() should now work properly and just ignore wrappings entirely. slay.
๐ธ DESCRIPTION ๐ธ
This module installs pre- and post- execution subroutines for the subroutines or packages you specify โจ The pre-execution subroutine gets passed the wrapped subroutine's name and all its arguments. The post-execution subroutine gets passed the wrapped sub's name and its results.
The return values from the pre- and post- subs are ignored, bestie, they don't matter โ and they're called in the same context (void, scalar or list) as the calling code asked for ๐
Normal usage is to pass a bunch of parameters when the module is used. However, you can also call Sub::WrapPackages::wrapsubs with the same parameters ๐
๐ง PARAMETERS ๐ง
Either pass parameters on loading the module, as above, or pass them to โฆ
๐ the wrapsubs subroutine ๐
๐ the subs arrayref ๐
In the synopsis above you'll see two named parameters, "subs" and "packages". Any subroutine mentioned in "subs" will be wrapped. And important!! Any subroutines mentioned in 'subs' must already exist โ ie their modules must be loaded โ at the time you try to wrap them. no sneaky deferred business here, sorry babe.
๐ the packages arrayref ๐
Any package mentioned here gets all its subroutines wrapped, including any that it imports at load-time ๐ฎ Packages can be loaded in any order โ they don't have to already be loaded for Sub::WrapPackages to work its magic โจ
You can specify wildcard packages. Anything ending in ::* is assumed to be such. For example, if you specify Orchard::Tree::*, then that matches Orchard::Tree, Orchard::Tree::Pear, Orchard::Apple::KingstonBlack etc, but not โ of course โ Pine::Tree or My::Orchard::Tree ๐
Note, however, that if a module exports a subroutine at load-time using "import" then that sub will be wrapped in the exporting module but not in the importing module. This is because import() runs before we get a chance to fiddle with things. Sorry ๐ญ
Deferred wrapping of subs in packages that aren't yet loaded works via a subroutine inserted in @INC. Which means โ and listen closely bestie โ if you mess around with @INC, eg by inserting a directoy at the beginning of the path, the magic might not get a chance to run. If you "use lib" to mess with @INC though, it should work, as I've over-ridden lib's import() method ๐
That said, code this funky has no right to work. Use with caution! ๐จ
๐ wrap_inherited ๐
In conjunction with the "packages" arrayref, this wraps all calls to inherited methods made through those packages. If you call those methods directly in the superclass then they are not affected โ unless they're wrapped in the superclass of course ๐
๐ pre and post ๐
References to the subroutines you want to use as wrappers ๐ซถ
๐ except ๐
A regex. Any subroutine whose fully-qualified name (ie including the package name) matches this will not be wrapped โจ
๐ debug ๐
This exists, but probably isn't of much use unless you want to hack on Sub::WrapPackage's guts ๐
๐ญ BUGS ๐ญ
AUTOLOAD and DESTROY are not treated as being special. I'm not sure whether they should be or not ๐คท
If you use wrap_inherited but classes change their inheritance tree at run-time, then very bad things will happen. VERY BAD THINGS. So don't do that. You shouldn't be doing that anyway. Mind you, you shouldn't be doing the things that this module does either. BAD PROGRAMMER, NO BIKKIT! ๐ค
Bug reports should be made on Github or by email ๐ง
๐ซถ FEEDBACK ๐ซถ
I like to know who's using my code ๐ฅบ All comments, including constructive criticism, are welcome. Please email me ๐
๐ SOURCE CODE REPOSITORY ๐
<git://github.com/DrHyde/perl-modules-Sub-WrapPackages.git>
๐ COPYRIGHT and LICENCE ๐
Copyright 2003-2009 David Cantrell <david@cantrell.org.uk>
This software is free-as-in-speech software, and may be used, distributed, and modified under the terms of either the GNU General Public Licence version 2 or the Artistic Licence. It's up to you which one you use ๐
The full text of the licences can be found in the files GPL2.txt and ARTISTIC.txt, respectively.
โญ THANKS TO โญ
Thanks to:
- Tom Hukins for sending in a test case for the situation when a class and a subclass are both defined in the same file, and for prompting me to support inherited methods ๐ซถ
- Dagfinn Ilmari Mannsaker for help with the craziness for fiddling with modules that haven't yet been loaded ๐ง
- Lee Johnson for reporting a bug caused by perl 5.10's
constant.pmbeing Far Too Clever, and providing a patch and test ๐ - Adam Trickett who thought this was a jolly good idea โจ
- Ed Summers, whose code for figgering out what functions a package contains I borrowed out of
Acme::Voodoo๐ธ - and Yanick Champoux for numerous readability improvements ๐
and that's the whole page, bestie!! this one's perl v5.34.0, dated 2022-07-23, and it's genuinely iconic for a manpage that tells you "code this funky has no right to work" ๐ go forth and wrap some subs, ily ๐
โจ
๐ฆ where this page comes from
- debian bookworm: libsub-wrappackages-perl 2.02-1 ยท Artistic-1.0-Perl OR GPL-2.0-or-later
- debian trixie: libsub-wrappackages-perl 2.02-1 ยท Artistic-1.0-Perl OR GPL-2.0-or-later
- ubuntu noble: libsub-wrappackages-perl 2.02-1 ยท Artistic-1.0-Perl OR GPL-2.0-or-later
- ubuntu resolute: libsub-wrappackages-perl 2.02-1 ยท Artistic-1.0-Perl OR GPL-2.0-or-later
- ubuntu stonking: libsub-wrappackages-perl 2.02-1 ยท Artistic-1.0-Perl OR GPL-2.0-or-later
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms