【问题标题】:Perl - while writing file to Standard Output last line is missingPerl - 将文件写入标准输出时缺少最后一行
【发布时间】:2014-12-25 00:12:33
【问题描述】:

我在空闲时间学习 perl,我正在尝试编写简单的脚本来打开和刷新某些文件。 下面的 sn-p 应该打开文件并将其写入标准输出。它确实如此,但不幸的是,它打印的文件没有最后一行。

有什么想法吗?

use strict; 
use warnings;

my $filesizeold = 0;

while(1){
    my $filesize = -s "input.txt";

    if($filesize != $filesizeold) {
        system $^O eq 'MSWin32' ? 'cls' : 'clear';
        open INPUT, "<input.txt";
        while ( <INPUT> ) {
            print;
        }
        close INPUT;
    }

    $filesizeold = $filesize;
}

【问题讨论】:

  • $filesize 不能在其中更改时,有一个while 循环似乎很奇怪......为什么会有一个while 循环?
  • true 无法被 Perl 识别。总是use strict; use warnings;除此之外,你的脚本对我来说很好。
  • 我做了一些更正。似乎是在最后一行之后没有 newline 符号时出现问题......也许有类似 println 而不是 print 的东西?
  • 看来你运行了一个无限循环,最后一行肯定被缓冲了。删除while(1) 循环。
  • 不,当我更新 input.txt 时,立即显示结果

标签: perl


【解决方案1】:

也许有类似 println 而不是 print 的东西?

是的,你可以使用say

http://perldoc.perl.org/functions/say.html

它打印没有最后一行的文件

我想你是suffering from buffering

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-02-07
    • 2017-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-06
    • 2012-06-01
    相关资源
    最近更新 更多