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 = ( @Carp::EXPORT, @Deploy::EXPORT_OK, @File::Slurper::EXPORT_OK, @FindBin::EXPORT_OK, @Getopt::Long::EXPORT, @Table::Readable::EXPORT_OK, );
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 / &cThanks for commenting, I will be taking a close look at your module when I next need to update/add something. Today I added signatures to this Z thing, I've decided to start using them, since it seems there is some progress now at the Perl Porters on future Perl. The annoying thing was getting the imports and unimports right. I think Toby Inkster's Z module uses some kind of super-duper import module. Here it is:
https://metacpan.org/pod/Import::Into
Also check out Import::Base, which is just a wrapper around Import::Into to make this specific sort of module.