为什么在这个 Perl 代码中没有打印换行符? [英] Why aren't newlines being printed in this Perl code?
问题描述
我有一些简单的 Perl 代码:
#!/usr/bin/perl使用严格;# 不在 OP 中,推荐使用警告;# 不在 OP 中,推荐我的 $val = 1;对于 ( 1 .. 100 ) {$val = ($val * $val + 1) % 8051;打印 ($val/8050) ."
";}
但是当我运行它时,输出是:
bash-3.2$ perl ./rand.pl0.0002484472049689440.000621118012422360.003229813664596270.08409937888198760.92... <为简洁起见剪下>...2919250.9284472049689440.3526708074534160.1081987577639750.2295652173913040.1839751552795030.433540372670807bash-3.2$
我做错了吗?
C:>
perldoc -f 打印
:
还要注意不要遵循带左括号的print关键字除非你想要相应的右括号终止打印的参数——插入一个 +或者在所有的周围加上括号参数.
因此,您需要的是:
print( ($val/8050) . "
" );
或
print +($val/8050) ."
";
你的语句打印了 $val/8050
的结果,然后将 "
"
连接到 print
的返回值和然后丢弃结果值.
顺便说一下,如果您:
使用警告;
然后 perl
会告诉你:
I have some simple Perl code:
#!/usr/bin/perl
use strict; # not in the OP, recommended
use warnings; # not in the OP, recommended
my $val = 1;
for ( 1 .. 100 ) {
$val = ($val * $val + 1) % 8051;
print ($val / 8050) . "
";
}
But when I run it, the output is:
bash-3.2$ perl ./rand.pl
0.0002484472049689440.000621118012422360.003229813664596270.08409937888198760.92
... <snipped for brevity> ...
2919250.9284472049689440.3526708074534160.1081987577639750.2295652173913040.1839
751552795030.433540372670807bash-3.2$
Am I doing something wrong?
C:>
perldoc -f print
:
Also be careful not to follow the print keyword with a left parenthesis unless you want the corresponding right parenthesis to terminate the arguments to the print--interpose a + or put parentheses around all the arguments.
Therefore, what you need is:
print( ($val / 8050) . "
" );
or
print +($val / 8050) . "
";
The statement you have prints the result of $val / 8050
and then concatenates "
"
to the return value of print
and then discards the resulting value.
Incidentally, if you:
use warnings;
then perl
will tell you:
print (...) interpreted as function at t.pl line 5. Useless use of concatenation (.) or string in void context at t.pl line 5.
这篇关于为什么在这个 Perl 代码中没有打印换行符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!