Why aren't newlines being printed in this Perl code?

前端 未结 3 1157
清歌不尽
清歌不尽 2020-12-12 04:17

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 ..         


        
3条回答
  •  忘掉有多难
    2020-12-12 05:06

    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) . "\n" );
    

    or

    print +($val / 8050) . "\n";
    

    The statement you have prints the result of $val / 8050 and then concatenates "\n" 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.
    

提交回复
热议问题