这是perl 5.8.8
#!/usr/bin/perl -wuse strict;use IO::Select;use IO::Handle; use Symbol qw(qualify_to_ref); open my $inf,"<","foo.txt" or dIE "hey! Can't open foo.txt!\n";my $sel = IO::Select->new();$sel->add($inf);while(my @ready = $sel->can_read()){ foreach my $handle (@ready){ my $line = sysreadline($handle); print $line; }}#### deal with buffering for select. from perl cookbook 7.23sub sysreadline { my($handle,$timeout) = @_; $handle = qualify_to_ref($handle,caller( )); my $infinitely_patIEnt = (@_ == 1 || $timeout new( ); $selector->add($handle); my $line = "";SLEEP: until (at_eol($line)) { unless ($infinitely_patIEnt) { return $line if time( ) > ($start_time + $timeout); } # sleep only 1 second before checking again next SLEEP unless $selector->can_read(1.0);input_READY: while ($selector->can_read(0.0)) { my $was_blocking = $handle->blocking(0);CHAR: while (sysread($handle,my $nextbyte,1)) { $line .= $nextbyte; ##print "next: [$nextbyte]\n"; last CHAR if $nextbyte eq "\n"; } $handle->blocking($was_blocking); # if incomplete line,keep trying next SLEEP unless at_eol($line); last input_READY; } } return $line;}sub at_eol($) { $_[0] =~ /\n\z/ }解决方法 select的返回意味着“read不会阻塞(即永远等待某些外部事件发生)”,而不是“数据可用”.从磁盘读取文件永远不会阻塞,它会在EOF时立即返回0.
所以你可能会更好地使用@TLP建议的File::Tail.
@H_403_2@ 总结以上是内存溢出为你收集整理的当没有东西要读时,perl can_read返回句柄全部内容,希望文章能够帮你解决当没有东西要读时,perl can_read返回句柄所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)