in reply to Looping based on a Conditional

use File::Tail (); { local *CATLOG; if ($opt{'c'}) { open CATLOG, '<', $MAILLOG or die("Unable to open mail log: $!\n"); } else { tie *CATLOG, 'File::Tail', (name => $MAILLOG, tail => -1); } while (<CATLOG>) { ...while loop code here... } }
or (Added)
use File::Tail (); use IO::File (); { # Why doesn't File::Tail implement IO::Handle's interface? # "read" means something else in Perl! package File::MyTail; BEGIN { our @ISA = 'File::Tail'; } sub getline { my $self = shift; return $self->read(@_); } } { my $log_fh; if ($opt{'c'}) { $log_fh = IO::File->new($MAILLOG, '<') or die("Unable to open mail log: $!\n"); } else { $log_fh = File::MyTail->new(name => $MAILLOG, tail => -1); } while (defined(my $line = $log_fh->getline())) { ...while loop code here... } }
or (Added)
use File::Tail (); { my $getline; if ($opt{'c'}) { open my $fh, '<', $MAILLOG or die("Unable to open mail log: $!\n"); $getline = sub { return scalar <$fh>; } } else { my $fh = File::MyTail->new(name => $MAILLOG, tail => -1); $getline = sub { return scalar $fh->read(); } } while (defined(my $line = $getline->()) { ...while loop code here... } }

Untested.

I like the last one. It has less overhead, and allows you to use the method of your choice to get a line from the source.