【问题标题】:Perl Windows - watch a file and read last linePerl Windows - 观察文件并读取最后一行
【发布时间】:2012-06-03 15:12:25
【问题描述】:

我正在尝试在 Windows Perl 中查看文件。我正在使用 Win32::ChangeNotify

这是我的代码:

use strict;
use warnings;


require Win32::ChangeNotify;
use Data::Dumper;

my $Path="C:\\Eamorr\\";
my $WatchSubTree=0;
my $Events="FILE_NAME";

my $notify=Win32::ChangeNotify->new($Path,$WatchSubTree,$Events);
while(1){
    $notify->reset;
    $notify->wait;
    print "File changed\n";
}

但是“文件已更改”永远不会被打印出来!我意识到这是非常基本的东西,但我真的在这个 Windows 平台上苦苦挣扎。

我在“C:\Eamorr\Eamorr.out”中有一个文件,我想监控它的变化(另一个程序每十分钟将一行新数据附加到该文件)。

当 Eamorr.out 更新时,我希望能够运行一些 Perl 并填充 MySQL 表。

请帮助我查看文件 Eamorr.out 并将最后一行打印到控制台。

附言我在 Windows Server 2003 上

非常感谢,

【问题讨论】:

  • 我刚刚安装了 Cygwin。我现在有 tail -f Eamorr.out... 似乎可以解决问题。
  • 您应该回答自己的问题并接受,如果您找到了解决方案,那么每个人都知道问题已解决。

标签: windows perl file notify watch


【解决方案1】:

这适用于我的 Windows 7,Activestate Perl 5.16。

use feature ":5.16";
use warnings FATAL => qw(all);
use strict;
use Data::Dump qw(dump);
use Win32::ChangeNotify;

my $Path='C:\Phil\z\Perl\changeNotify\\';
my $WatchSubTree = 0;
my $Events = "SIZE";

say STDERR "Exists=", -e $Path;

my $notify=Win32::ChangeNotify->new($Path,$WatchSubTree,$Events) or say("Error=", Win32::GetLastError());
while(1)
 {$notify->reset;
  $notify->wait;
  say STDERR "File changed";
 }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-09
    • 2022-01-16
    • 2021-06-27
    • 1970-01-01
    相关资源
    最近更新 更多