Perl 版本字符串:为什么使用 EVAL EXPR?
我刚刚注意到这个由 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
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 number0.01001
correctly.来自 perlmodstyle:版本编号
From perlmodstyle: Version numbering
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 number0.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'
我可能记错了,但我认为一些自动代码解析器喜欢查看代码行:
但是您确实希望 $VERSION 保存浮点数而不是字符串。
您可能想阅读这篇文章 ,我知道我要去。
哦,天哪,现在我记得为什么要使用
样式版本号了。这太疯狂了。我喜欢 Perl,但那是纯粹、精致、集中的疯狂。我不会尝试总结 David Golden 的文章。你只需要读完它并哭泣即可。
I may be misremembering this, but I think some automated code parsers like to see the line of code:
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
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.