Perl 数值变量
Perl 和我对于变量是否是数字存在分歧。我当然错了,但为什么呢?
my $bytes = 0;
# . . .
$bytes = $bytes + length($text);
# . . .
my $MB = $bytes / 1048576;
my $string = sprintf("Downloaded: %.1f MB\n", MB);
给出参数“MB”在 foo.pl 第 200 行的 sprintf 中不是数字。
。
事实上,当我使用
my $string = "Downloaded: $MB MB\n";
它将字符串设置为 Downloaded: 3.09680080413818 MB
时可以看到,它是数字。
编辑:啊,愚蠢的错误,谢谢你发现它。
Perl and I disagree as to whether a variable is a number or not. I'm wrong, of course, but why?
my $bytes = 0;
# . . .
$bytes = $bytes + length($text);
# . . .
my $MB = $bytes / 1048576;
my $string = sprintf("Downloaded: %.1f MB\n", MB);
gives Argument "MB" isn't numeric in sprintf at foo.pl line 200.
.
It is, in fact, numeric as can be seen when I use
my $string = "Downloaded: $MB MB\n";
which sets the string to Downloaded: 3.09680080413818 MB
.
Edit: Ah, silly mistake, thanks for catching it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要变量印记:
You need the variable sigil:
看起来你的意思可能是:
除此之外,这应该可行。
Looks like you probably meant:
other than that this should work.