http://qs1969.pair.com?node_id=502654

A minor pet peeve for me when programming Perl is the degree to which I wind up repeating code for things that I use over and over again. At the subroutine level, it's my $self = shift. At the file level, a prime example is use strict and use warnings, but it includes importing utility modules like Carp and Scalar::Util as well.

I was hopeful when I heard about TheDamian's Toolkit, but was then disappointed to see that it uses config files in PERL5LIB -- which works fine for my own programs but is not necessarily easily shared with others or with end users of things.

I decided to pick up and dust off an old idea of mine -- a module that bundles other modules into a toolset that imports all my "usual" stuff in one shot. In essence, I want this:

use My::Tools; # turns strict on # turns warnings on # makes all my usual imports available

This is now possible with a new module I wrote called ToolSet. (The alpha version is now on CPAN.) For example:

package My::Tools; use base 'ToolSet'; # handles the import() ToolSet->set_strict(1); ToolSet->set_warnings(1); ToolSet->export( 'Carp' => undef, # all the Carp defaults 'Scalar::Util' => 'refaddr blessed weaken', 'List::Util' => 'first reduce' ); 1; # return true

One of the more interesting discoveries I made along the way is how to get strict and warnings to be "contagious":

use My::Tools; $var = 1; # causes a compile error because we're using strict

The breakthrough came from reading the source for strict and reading about $^H in perlvar. I realized that all I had to do was call strict's import sub without creating a new BEGIN block during ToolSet's own import routine:

# simple example of strict contagion sub import { require strict; strict->import; }

This applies the strictures when the BEGIN block for use My::Tools finishes -- i.e. to the code that called use My::Tools. The same principle applied for warnings.

The rest so far is just using eval to import the defined export modules into the calling package. In subsequent versions, I'll look to add the equivalent of @EXPORT_OK and @EXPORT_TAGS. I'd also like to add lazy exporting -- where a stub is installed that, when called, will require a module and replace the stub with the real function.

I hope some people may find this useful. As always, I'd appreciate any feedback or ideas.

Update: As another example of how this might be used, imagine a Perl6::Now ToolSet, that turned on strict and loaded all of the relevant Perl6::* modules.

-xdg

Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.