in reply to Looping based on a Conditional
Example implementation:
use strict; use warnings; use File::Tail; my %opt = (c => 0); my $MAILLOG = $ARGV[0]; # # $reader will hold a reference to an anonymous subroutine that reads +from # a source. Either a filehandle or a File::Tail object. # my $reader = undef; if ($opt{'c'}) { my $fh = undef; open $fh, "<", $MAILLOG or die "Failed to read $MAILLOG. $!"; # Create an anonymous subroutine that reads from the lexical $fh # filehandle. Once $reader goes out of scope the file will be clos +ed $reader = sub { readline($fh) }; } else { my $log = File::Tail->new( name => $MAILLOG, tail => -1, interval => 1, max_interval => 1, ); # Create an anonymous subroutine that reads from the $log # object. Once $reader goes out of scope the object will be # destroyed. $reader = sub { $log->read() }; } while (defined(my $line=$reader->())) { print $line; }
|
|---|