felwithe
felwithe

Reputation: 2744

Perl: can you make a "use"d module or setting apply to the including script?

For example, every single one of my scripts has this at the top:

use warnings; use strict; use v5.10;
use Tools;

My "Tools" package has a bunch of functions that I use all the time. I would rather just include it and have it use warnings, strict, and 5.10 for the including script, since I have to use those for every script anyway. Is there a way to do that?

Upvotes: 3

Views: 86

Answers (1)

friedo
friedo

Reputation: 66967

You can use Import::Into with a custom import method to turn stuff on in the importing class. For example:

package Tools;

use strict;
use warnings;
use feature ':5.10';

use Import::Into;

sub import {
    my $target = caller;
    strict->import::into( $target );
    warnings->import::into( $target );
    feature->import::into( $target, ':5.10' );

    # other imports, etc
}

I wrote a more detailed post about using Import::Into here: Removing Perl Boilerplate with Import::Into.

Upvotes: 4

Related Questions