如何显示在Perl服务器上不断更新的文件行?

时间:2017-01-18 14:38:27

标签: perl

我正在尝试显示不断更新的文件的更新。每次在必须打印的文件中有更新时,都会从服务器访问此文件。这是我到目前为止构建的代码:

    if (ModelState.IsValid)
    {
        int perfromanceId = Convert.ToInt32(TempData.Peek("CurrentPerformanceId"));

        // There is no need to use Where. FirstOrDefault has an overload using predicates.
        var savedPerformance = db.Performances.FirstOrDefault(x => x.PerformanceId == perfromanceId); 

        // If the performance couldn't be found, then you could add the error to the model state and return it to the view.
        if(savedPerformance == null)
            return View(customPerformancePerformersModel.performanceObj);

        // Update properties from performance in database with new performance.
        savedPerformance.someProperty = customPerformancePerformersModel.performanceObj.someProperty;

        db.Performances.Attach(savedPerformance);
        db.Entry(savedPerformance ).State = EntityState.Modified;
        db.SaveChanges();
        return RedirectToAction("Index");
    }

代码读取文件,打印所有现有行并保持循环。不幸的是,代码不会打印新添加的行。对我的问题有任何帮助吗?

1 个答案:

答案 0 :(得分:3)

不要试图重新发明轮子;使用File::Tail

use File::Tail qw( );

my $tail = File::Tail->new('log_file.txt');
while (defined( my $line = $file->read() )) {
  print($line);
}

与您的不同,它不会返回部分行,它会检测日志文件的旋转等。