我可以确保 Perl 代码是在 5.10+ 上编写的吗?会在5.8上运行吗?

发布于 2024-09-10 15:55:01 字数 228 浏览 2 评论 0原文

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

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

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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

桃酥萝莉 2024-09-17 15:55:01

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

这要求您拥有适当的测试套件并编写足够的测试。同时维护多个单独的 Perl 安装也很容易,正如我在《有效的 Perl 编程》中所展示的那样。

Test::MinimumVersion 听起来似乎可行,但它有一些限制。它只查看您提供的文件(因此它不会检查您加载的任何内容),而且我认为它实际上并不查看正则表达式模式。这些报告中的每一个都报告最低版本是 5.004,这对它们中的任何一个来说都是不正确的:

#!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 解决方案来追求类似的目标。

编译它、运行测试并找出答案会更容易。

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.

帥小哥 2024-09-17 15:55:01

我不认为你可以在运行代码时进行 Perl 检查,但是
查看 Perl::MinimumVersionTest::MinimumVersion。 (后者只是前者的 Test::Builder 包装器。)

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.)

若相惜即相离 2024-09-17 15:55:01

好吧,通过阅读其他答案,您的答案是否定的,但也许应用程序: :perlbrew 可以帮助您安装和管理多个版本的 perl 进行测试。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文