Perl 版本字符串:为什么使用 EVAL EXPR?

发布于 2024-09-18 08:48:32 字数 167 浏览 3 评论 0原文

我刚刚注意到这个由 Catalyst.pl 生成的。这显然是某种未注释的黑客行为。这样设置版本字符串有什么好处呢?我什至无法弄清楚他们想做什么。

our $VERSION = '0.01';
$VERSION = eval $VERSION;

I just took notice to this generated by Catalyst.pl. It is obviously some sort of unannotated hack. What is the advantage of setting up a version string like this? I can't even figure out what they're trying to do.

our $VERSION = '0.01';
$VERSION = eval $VERSION;

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

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

发布评论

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

评论(4

半夏半凉 2024-09-25 08:48:32

Perl 中的版本号很复杂。 对于那些正在寻找血淋淋的细节。您可能会惊讶于有多少微妙的方式会导致错误......

不过,您问题的直接答案是,不同的事物需要不同的格式。对于 CPAN,您关心开发版本,例如作为字符串。对于运行时,您将它们视为数字。

考虑 $VERSION = "0.01_001" 的情况。 eval 正确地将其转换为数字 0.01001

Version numbers are complex in Perl. Here's an excellent overview for those looking for the gory details. It might surprise you how many subtle ways there are to get things wrong...

The direct answer to your question though, is that different things expect different formats. For CPAN, you care about development versions for example, as a string. For runtime, you care about them as a number.

Consider the case of $VERSION = "0.01_001". eval converts it to the number 0.01001 correctly.

负佳期 2024-09-25 08:48:32

来自 perlmodstyle:版本编号

如果您想发布“测试版”或
模块的“alpha”版本,但没有
希望 CPAN.pm 将其列为最新的
在常规版本后使用“_”
数字后跟至少 2 位数字,
例如。 1.20_01。如果您这样做,
推荐使用以下习语:

  1. $VERSION = "1.12_01";
  2. $XS_VERSION = $VERSION; # 仅当您有 XS 代码时才需要
  3. $VERSION = 评估 $VERSION;

有了这个技巧,MakeMaker 只会
阅读第一行,从而阅读
下划线,而 perl 解释器
将评估 $VERSION 并转换
将字符串转换为数字。之后
将 $VERSION 视为
然后号码就可以这样做
不会引起警告
$VERSION 不是数字。

From perlmodstyle: Version numbering

If you want to release a 'beta' or
'alpha' version of a module but don't
want CPAN.pm to list it as most recent
use an '_' after the regular version
number followed by at least 2 digits,
eg. 1.20_01. If you do this, the
following idiom is recommended:

  1. $VERSION = "1.12_01";
  2. $XS_VERSION = $VERSION; # only needed if you have XS code
  3. $VERSION = eval $VERSION;

With that trick MakeMaker will only
read the first line and thus read the
underscore, while the perl interpreter
will evaluate the $VERSION and convert
the string into a number. Later
operations that treat $VERSION as a
number will then be able to do so
without provoking a warning about
$VERSION not being a number.

何止钟意 2024-09-25 08:48:32

eval 将字符串 "0.001_001" 转换为数字,遵循 Perl 数字文字的规则(允许使用下划线以提高可读性)。结果是数字0.001001

如果没有 eval,字符串将按照字符串转换规则转换为数字,并在第一个非数字字符处停止。

例如:perl -e 'print "0.001_001" + 0'

The eval converts the string "0.001_001" to a number, following the rules for Perl numeric literals (which allow underscores for legibility). The result is the number 0.001001.

Without the eval, the string is converted to a number following the rule for converting strings, which stops at the first non-numeric character.

E.g.: perl -e 'print "0.001_001" + 0'

马蹄踏│碎落叶 2024-09-25 08:48:32

我可能记错了,但我认为一些自动代码解析器喜欢查看代码行:

 our $VERSION = '0.01';

但是您确实希望 $VERSION 保存浮点数而不是字符串。

您可能想阅读这篇文章 ,我知道我要去。

哦,天哪,现在我记得为什么要使用

our $VERSION = 20100903;

样式版本号了。这太疯狂了。我喜欢 Perl,但那是纯粹、精致、集中的疯狂。我不会尝试总结 David Golden 的文章。你只需要读完它并哭泣即可。

I may be misremembering this, but I think some automated code parsers like to see the line of code:

 our $VERSION = '0.01';

But you really want $VERSION to hold a float instead of a string.

You may want to read this article, I know I am going to.

Oh, dear god, now I remember why I use

our $VERSION = 20100903;

style version numbers. That is just insane. I love Perl, but that is pure, refined, concentrated insanity. I won't try to summarize David Golden's article. You just have to read it and cry.

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