- 论坛徽章:
- 0
|
一定要用模块么?perl本身不能实现? 谢谢!
nathanisl 发表于 2010-11-29 13:43
http://perldoc.perl.org/functions/seek.html
Due to the rules and rigors of ANSI C, on some systems you have to do a seek whenever you switch between reading and writing. Amongst other things, this may have the effect of calling stdio's clearerr(3). A WHENCE of 1 (SEEK_CUR ) is useful for not moving the file position:
1. seek(TEST,0,1);
This is also useful for applications emulating tail -f . Once you hit EOF on your read and then sleep for a while, you (probably) have to stick in a dummy seek() to reset things. The seek doesn't change the position, but it does clear the end-of-file condition on the handle, so that the next <FILE> makes Perl try again to read something. (We hope.) - #!/bin/env perl
- use strict;
- use warnings;
- local $|;
- my $log_file = "log.txt";
- if (open(my $fp, "<", $log_file))
- {
- seek($fp, 0, 2);
- while (1)
- {
- seek($fp, 0, 1);
- if (eof($fp))
- {
- # sleep 20ms
- select(undef, undef, undef, 0.02);
- }
- else
- {
- while (my $new_line = <$fp>)
- {
- # add your code here
- print $new_line;
- }
- }
- }
- }
- else
- {
- die "$log_file: $!\n";
- }
复制代码 |
|