Can I make sure Perl code written on 5.10+ will run on 5.8?

淺唱寂寞╮ 提交于 2019-12-04 04:26:05

When I want to ensure that a program will run under particular versions of perl, I test it under that version of perl. A feature of my release application tests under multiple perls before it actually uploads.

This requires that you have a proper test suite and write enough tests. It's easy to maintain several separate perl installations at the same time, too, as I show in Effective Perl Programming.

Test::MinimumVersion almost sounds like it might work, but it has several limitations. It only looks at the file you give it (so it will not check anything you load), and I don't think it actually looks inside regex patterns. Each of these report that the minimum version is 5.004, which is not true for any of them:

#!perl

use Perl::MinimumVersion;

my $p_flag = <<'SOURCE';
'123' =~ m/[123]/p; # 5.10 feature
SOURCE

my $named_capture = <<'SOURCE';
'123' =~ m/(?<num>[123])/; # 5.10 feature
SOURCE

my $r_line_ending = <<'SOURCE';
'123' =~ m/[123]\R/p; # 5.12 feature
SOURCE

my $say = <<'SOURCE';
say 'Hello';
SOURCE

my $smart_match = <<'SOURCE';
$boolean = '123' ~~ @array;
SOURCE

my $given = <<'SOURCE';
given( $foo ) {
    when( /123/ ) { say 'Hello' }
    };

SOURCE

foreach my $source ( $p_flag, $named_capture, $r_line_ending, $say, $smart_match, $given ) {
    print "----Source---\n$source\n-----";
    my $version = Perl::MinimumVersion->new( \$source  )->minimum_version;
    print "Min version is $version\n";
    }

Part of the reason Perl::MinimumVersion works is because it looks for hints that the source gives it already, such as use 5.010, and use feature so on. However, that's not the only way to enable features. And, as you'll note, it misses things like the /p flag, at least until someone adds a check for that. However, you'll always be chasing things like that with a PPI solution.

It's easier just to compile it, run the tests, and find out.

I don't think you can make Perl check as you run the code, but check out Perl::MinimumVersion and Test::MinimumVersion. (The latter is just a Test::Builder wrapper around the former.)

Well from reading other answers your answer is no, but perhaps App::perlbrew can help you install and manage multiple versions of perl for testing.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!