CPAN is your friend. There are a number of modules available that automatically prepend or append standard code to a set of functions. There source code should give you some ideas about how to go about this. Better yet, download them and try them out yourself:
- Sub::WrapPackages wraps all subs in a package with pre/post processing.
- Attribute::Handlers lets you assign attributes to subs. The attributes can be mapped to routines that prepend or postpend code to a subroutine. Complex and quirky and prone to lots of redundant coding. Perl also has limited built-in support for defining custom subroutine attributes, but it doesn't work well for subroutine definition. See these these threads: Support for attributes:get in Attribute::Handlers? and its follow-up for more information if you are interested. A newer module Sub::Attribute claims to be a slimmed down version of AttributeHandlers. You might want to check that out too.
- Sub::Override subroutines and syntactic sugar for overriding individual functions
- Sub::Versive another modules for automatically prepending and appending code to subroutines
- Sub::Prepend provides simple support for prepending code to existing subs.
This is just for starters. There are several more: just search CPAN on the word "sub" to see what is out there.
Best, beth