madmax
madmax

Reputation: 15

Perl: work with log file

Please help me, how after every 100 lines,my cycle sleep on 15 seconds, and in a specified time reopen log.

open(LOGFILE,$logfile) or die "error";
for(;;){
      if($logfile=<LOGFILE>){
          ......................
    }
}

Upvotes: 1

Views: 77

Answers (3)

Sinan &#220;n&#252;r
Sinan &#220;n&#252;r

Reputation: 118166

Use File::Tail:

use File::Tail;
my $file = File::Tail->new(name => $name, maxinterval => 300, adjustafter => 7);
while (defined(my $line = $file->read)) {
    print "$line";
}

If that does not satisfy your needs, see tell and seek.

Upvotes: 2

Borodin
Borodin

Reputation: 126772

Like this perhaps

open my $log_fh, '<', $logfile or die $!;

while ( <$log_fh> ) {
    print;
    sleep 15 unless $. % 100;
}

Upvotes: 0

choroba
choroba

Reputation: 242373

Just use two loops:

while () {   # Same as for (;;).
    for (1 .. 100) {
        open my $LOGFILE, '<', $logfile or die $!;
        if ($logline = <LOGFILE>) {
            # 條件
        }
        sleep 15;
    }
}

Upvotes: 1

Related Questions