Skip to content

Instantly share code, notes, and snippets.

@hakobe
Created August 7, 2010 12:07
Show Gist options
  • Select an option

  • Save hakobe/512742 to your computer and use it in GitHub Desktop.

Select an option

Save hakobe/512742 to your computer and use it in GitHub Desktop.
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