Removing Boilerplate with Import::Into

I recently started a new project and wanted to take advantage of some cool new Perl 5.20 features. In particular, I wanted to use the experimental subroutine signatures feature and postfix dereferencing. That requires the following incantation:


use feature 'signatures';
use feature 'postderef';
no warnings 'experimental::signatures';
no warnings 'experimental::postderef';

That can be abbreviated a bit by passing a list to feature and just turning off the entire experimental class of warnings:

	
use feature 'signatures', 'postderef';
no warnings 'experimental';

But it's still a couple lines of boilerplate that need to be pasted atop every file in my project. I hate pasting things.

Read more on my blog: Removing Boilerplate with Import::Into

6 Comments

I had the same problem, and I kept repeating the solution, so I wrote a module: Import::Base

I use Import::Into for avoiding boilerplate in my tests:

# t/lib/MyApp/TestKit.pm
package MyApp::TestKit;

use strict;
use warnings;
use Import::Into;
use FindBin qw/$Bin/;
use lib "$Bin/../lib";
use MyApp::Util;
use Test::More;
use Test::Warnings;
use Test::Fatal;
use utf8;

BEGIN {
# load config file epplication_testing.pl
# instead of epplication_local.pl
$ENV{ EPPLICATION_CONFIG_LOCAL_SUFFIX } = 'testing';
}

sub import {
my $target = caller;
MyApp::Util->import::into($target);
Test::More->import::into($target);
Test::Warnings->import::into($target, qw/ :all /);
Test::Fatal->import::into($target, qw/ exception /);
strict->import::into($target);
warnings->import::into($target);
utf8->import::into($target);
}

1;


# and in each test file.

#!/usr/bin/env perl
use FindBin qw/$Bin/;
use lib "$Bin/lib";
use EPPlication::TestKit;
# test code
done_testing();

hi ether? what do you want to express with your comment? Did Jimmy fall in the well? ;)

@davewood: use experimental qw< signatures postderef >;

Leave a comment

About Mike Friedman

user-pic Mike Friedman is a professional computer programmer living and working in the New York City area.