Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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.


In reply to Bundling commonly-used modules into a toolset by xdg

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others musing on the Monastery: (6)
    As of 2020-05-31 09:29 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      If programming languages were movie genres, Perl would be:















      Results (173 votes). Check out past polls.

      Notices?