Switch lots of things on at once

Many people already have codes like

use strict;
use warnings;

and so on at the top of each script they write. For scripts which I don't intend to publish anywhere, I have a module (which I accidentally called Z not knowing there was already a module of the same name on CPAN), which switches on lots of things at once just by saying

use Z;

The top bit goes like this:

package Z;
use warnings;
use strict;
use utf8;

use Carp;
use Deploy 'do_system';
use File::Slurper qw!read_text write_text read_lines!;
use FindBin '$Bin';
use Getopt::Long;
use Table::Readable ':all';
use v5.32;
no warnings qw(experimental::signatures);
use feature qw(signatures);

So far that is all fairly standard stuff, but what Z does is also to import all of the above things into my script using the EXPORT variables from the above modules:

our $VERSION = '0.01';

require Exporter;
our @ISA = qw(Exporter);

our @EXPORT = (

This requires a special import method:

sub import
    my ($class) = @_;

    strict->import ();
    utf8->import ();
    warnings->import ();
    warnings->unimport (qw(experimental::signatures));
    feature->import ('signatures');

    Carp->import ();
    File::Slurper->import (qw!read_text write_text!);
    FindBin->import ('$Bin');
    Getopt::Long->import ();
    Deploy->import ('do_system');
    Table::Readable->import (':all');

    Z->export_to_level (1);

To save another bit of boilerplate I also have

binmode STDOUT, ":encoding(utf8)";

at the end of the module.

This is for personal convenience so it's not something I would use publicly, but perhaps people who want to save themselves a bit of boilerplate might find this useful for making their own "personal module".


I like this pattern so much, that some time ago I release a library to make it easier: Code::Style::Kit

It's essentially the same thing you're describing, but as re-usable components.

As an intermediate step, import::into hides the different ways of importing modules / pragmata / &c

Also check out Import::Base, which is just a wrapper around Import::Into to make this specific sort of module.

Leave a comment

About Ben Bullock

user-pic Perl user since about 2006, I have also released some CPAN modules.