Specify the min perl version for your distribution

It's a good idea to specify the minimum Perl version required by your distribution. It's useful information for people looking at your code, it's helpful for CPAN Testers (which will report NA for old perls, rather than failing), and it makes the requirement clear to people who are trying to install your module on an older Perl.

There are two main things you should do to specify the min perl version:

  • Put it in your code
  • Put it in the metadata for your distribution

Specifying the min perl version in your code

This is the easy part. If your module requires Perl 5.6.0 or later, then put the following at the top of your module, right next to strict and warnings:

    use 5.006;

Look at perldoc -f use for more on this.

If you're not sure what version of Perl your module requires, install Perl::MinimumVersion, then use the perlver script which comes with it. For example, with my Module::Path module:

    % cd Module-Path
    % perlver lib/Module/Path.pm

The output tells you what version is required by the syntax used, and also if there's a version explicitly required:

       -------------------------------------------------
     | file               | explicit | syntax | external |
     | ------------------------------------------------- |
     | lib/Module/Path.pm | v5.6.0   | v5.6.0 | n/a      |
     | ------------------------------------------------- |
     | Minimum explicit version : v5.6.0                        |
     | Minimum syntax version   : v5.6.0                        |
     | Minimum version of perl  : v5.6.0                        |
       -------------------------------------------------

It doesn't recognise all features from recent versions of Perl (but I'm working on that. I need to fix that broken layout too!).

If you get the version wrong, CPAN Testers will let you know.

Put the min perl version in your metadata

How you do this depends on the dist builder you're using.

ExtUtils::MakeMaker

If you're using ExtUtils::MakeMaker, you need version 6.48 or later:

    use ExtUtils::MakeMaker 6.48;
    WriteMakefile(
        # stuff
        MIN_PERL_VERSION => 5.006,
    );

Or you can only specify the min perl version if running under a recent enough version of EU::MM. This is some boilerplate I nicked from MSCHWERN:

    use ExtUtils::MakeMaker;

    my $mm_ver = $ExtUtils::MakeMaker::VERSION;
    if ($mm_ver =~ /_/) { # developer release/version
        $mm_ver = eval $mm_ver;
        die $@ if $@;
    }

    WriteMakefile(
        ...
        ($mm_ver >= 6.48
            ? (MIN_PERL_VERSION => 5.006)
            : ()
        ),
    );

This means your dist will install on older versions of Perl that come with a version of EU::MM that doesn't support MIN_PERL_VERSION.

Module::Build

If you're using Module::Build, you specify a requires dependency on 'perl':

    my $builder = Module::Build->new(
        module_name => 'Foo::Bar',
        # other stuff
        requires => {
            'perl' => 5.006,
            # other dependencies
        },
    );
    $builder->create_build_script();

Dist::Zilla

There are various ways you can do this (of course!). The way I do it is by making sure I've specified the min perl version in the code, and then use the AutoPrereqs plugin in my dist.ini:

    [AutoPrereqs]

This finds the use 5.006; line in the code, and converts that to a perl dependency.

You can manually add the dependency:

    [Prereqs]
    perl = 5.006

And there's also the MinimumPerl plugin, which uses Perl::MinimumVersion

2 Comments

If you are too lazy to remember or look up what Perl version supports // and \p{Unicode}, you might like Syntax::Construct.

With ExtUtils::MakeMaker don't you also have to specify ExtUtils::MakeMaker => 6.48 in CONFIGURE_REQUIRES? And since that option is available starting in 6.52, you actually need to bump the requirement up to that.

Leave a comment

About Neil Bowers

user-pic Perl hacker since 1992.