Perl:如何获取“使用版本”数字

发布于 2025-01-03 13:05:15 字数 162 浏览 2 评论 0原文

是否有一个特殊的变量或函数可以在运行脚本时为我提供 use VERSION 的数量(在本例中为 5.12.0)?

#!/usr/bin/env perl 
use warnings;
use 5.12.0;

Is there a special variable or a function which gives me the number of use VERSION (in this case 5.12.0) when running the script?

#!/usr/bin/env perl 
use warnings;
use 5.12.0;

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

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

发布评论

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

评论(3

葬花如无物 2025-01-10 13:05:15

我刚刚快速检查了 feature.pm 代码 - 版本本身没有存储在任何地方。亚历克斯的回答已经展示了如何测试通话产生的特定功能。

另请注意,use VERSION 可以在多个位置调用(例如在模块中)。

一种假设的选择是覆盖 use 并在某处记录版本号以供检查。

编辑:在钩子方向上进行一些探索:

use version; # for version parsing
use subs 'require';
BEGIN {
    sub require {
        warn "use ",version->parse($_[0]);
        # ... emulate original require
    };
}

use 5.12.0;

这个有限的示例报告了指定的版本,但对于真正的使用,它必须更加强大。

I just quickly checked feature.pm code - the version itself is not stored anywhere. Alex's answer already showed how to test particular features that results from the call.

Also note that use VERSION can be called on several places (in modules for instance).

One hypothetical option would be to override use and record the version number somewhere for inspection.

Edit: Some poking in the hook direction:

use version; # for version parsing
use subs 'require';
BEGIN {
    sub require {
        warn "use ",version->parse($_[0]);
        # ... emulate original require
    };
}

use 5.12.0;

This limited example reports the version specified, but for real use it would have to be much more robust.

又爬满兰若 2025-01-10 13:05:15

您可以在编译时查看提示变量 (${^H})(龙潜伏的位置)并查看提示哈希 (%{^H})(龙潜伏的位置,但以公开记录的方式),这会让您知道启用了哪些特定功能。我不知道如何计算是否需要特定的功能包或所有给定的功能:

perl -le "use feature qw(:5.12); BEGIN{print $^H;print foreach keys %^H}"
133376
feature_unicode
feature_say
feature_state
feature_switch

perl -le "use 5.12.0; BEGIN{print $^H;print foreach keys %^H}"
134914
feature_unicode
feature_say
feature_state
feature_switch

You can, during compile time, poke about in the hints variable (${^H}) (where dragons lurk) and look into the hints hash (%{^H}) (where dragons lurk, but in a public documented sort of way), this will let you know which specific features are enabled. I don't know how to work out if specifically a feature bundle, or all given features, were requested:

perl -le "use feature qw(:5.12); BEGIN{print $^H;print foreach keys %^H}"
133376
feature_unicode
feature_say
feature_state
feature_switch

perl -le "use 5.12.0; BEGIN{print $^H;print foreach keys %^H}"
134914
feature_unicode
feature_say
feature_state
feature_switch
望笑 2025-01-10 13:05:15

其中任何一个都会为您提供解释器的版本:

$]
Perl 解释器的版本+补丁级别/1000。该变量可用于确定执行脚本的 Perl 解释器是否处于正确的版本范围内。 (助记:这个版本的perl 是在右括号中吗?)

$PERL_VERSION
$^V
Perl 解释器的修订版、版本和颠覆,表示为“版本”对象。
这个变量首先出现在perl 5.6.0中;早期版本的 perl 将看到未定义的值。在 perl 5.10.0 之前,$^V 表示为 v 字符串。

那些将是>比“使用”行上的内容更重要:如果您需要确切的字符串,请硬着头皮将其粘贴到变量中,如下所示:

 use 5.12.0; $WANTPERL='5.12.0';

Either one of these gives you the version of the interpreter:

$]
The version + patchlevel / 1000 of the Perl interpreter. This variable can be used to determine whether the Perl interpreter executing a script is in the right range of versions. (Mnemonic: Is this version of perl in the right bracket?)

$PERL_VERSION
$^V
The revision, version, and subversion of the Perl interpreter, represented as a "version" object.
This variable first appeared in perl 5.6.0; earlier versions of perl will see an undefined value. Before perl 5.10.0 $^V was represented as a v-string.

Those will be > than what you have on the "use" line: if you need that exact string, bite the bullet and stick it in a variable as in:

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