我可以确保在 5.10+ 上编写的 Perl 代码可以在 5.8 上运行吗? [英] Can I make sure Perl code written on 5.10+ will run on 5.8?

查看:53
本文介绍了我可以确保在 5.10+ 上编写的 Perl 代码可以在 5.8 上运行吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Perl 5.10 和 5.12 的一些新功能(例如say")被定义为功能,您可以使用功能"编译指示明确启用或禁止这些功能.但是其他的添加,比如正则表达式的命名捕获组,是隐含的.

Some of the new features of Perl 5.10 and 5.12, such as "say", are defined as features, that you can enable or disallow explicitly using the "feature" pragma. But other additions, like the named capture groups of regexes, are implicit.

当我使用 5.10+ 解释器编写 Perl,但希望它也能在 5.8 上运行时,我可以让 Perl 抱怨使用 5.8 中没有的任何东西吗?显然,在您打算运行的所有主要版本上测试您的代码是一种很好的做法,但让 Perl 自动警告我仍然很好.

When I write Perl using a 5.10+ interpreter, but want it to also run on 5.8, can I make Perl complain about using anything that's not in 5.8? Obviously, it is good practice to test your code on all major versions you intend it to run on, but it'd still be nice to have Perl warn me automatically.

推荐答案

当我想确保程序能在特定版本的 perl 下运行时,我会在该版本的 perl 下测试它.我的 release 应用程序在实际上传之前在多个 perls 下进行测试.

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.

这要求您拥有合适的测试套件并编写足够的测试.同时维护多个单独的 perl 安装也很容易,正如我在 Effective Perl Programming 中展示的那样.

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 听起来似乎可行,但它有几个限制.它只查看你给它的文件(所以它不会检查你加载的任何东西),我认为它实际上并不在正则表达式模式中.这些报告中的每一个都报告最低版本为 5.004,这对它们中的任何一个都不正确:

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";
    }

Perl::MinimumVersion 工作的部分原因是因为它寻找提示源码已经给了,比如use 5.010use feature等等.但是,这并不是启用功能的唯一方法.而且,正如您将注意到的,它会遗漏诸如 /p 标志之类的东西,至少在有人为此添加检查之前是这样.但是,您总是会使用 PPI 解决方案来解决此类问题.

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.

这篇关于我可以确保在 5.10+ 上编写的 Perl 代码可以在 5.8 上运行吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆