Created
August 7, 2010 12:07
-
-
Save hakobe/512742 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| use strict; | |
| use warnings; | |
| use AnyEvent; | |
| use AnyEvent::Handle; | |
| # 引数に指定したファイルすべてに対して tail -f する | |
| # busy loop になっていてCPUを食う | |
| $| = 1; | |
| my $cv = AnyEvent->condvar; | |
| my @files = @ARGV; | |
| my %handles; | |
| sub create_handle { | |
| my ($key, $fh) = @_; | |
| $handles{$key} = AnyEvent::Handle->new( | |
| fh => $fh, | |
| on_error => sub { | |
| my ($hdl, $fatal, $msg) = @_; | |
| warn "got error $msg\n"; | |
| $hdl->destroy; | |
| $cv->send; | |
| }, | |
| on_eof => sub { | |
| my $hdl = shift; | |
| $hdl->destroy; | |
| create_handle($key, $fh); # loop | |
| }, | |
| on_read => sub { | |
| my $hdl = shift; | |
| $hdl->push_read(line => sub { | |
| my ($hdl, $line) = @_; | |
| print "$line\n"; | |
| }) | |
| }, | |
| ); | |
| } | |
| my @fhs; | |
| for my $file (@files) { | |
| open my $fh, '<', $file; | |
| push @fhs, $fh; | |
| create_handle($file, $fh); | |
| } | |
| my $int; $int = AnyEvent->signal( | |
| signal => 'INT', | |
| cb => sub { | |
| for my $fh (@fhs) { | |
| close $fh; | |
| } | |
| undef %handles; | |
| undef $int; | |
| $cv->send; | |
| } | |
| ); | |
| $cv->recv; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment