Build Perl script so that as data is inserted into a log file, it is obtained and processed instantly

1

Colleagues I'm doing a perl script so that as data is inserted into a log file, I instantly get it and process it but it is giving me an error (the error is in the open) and I do not know what to do. I pass the code:

#######!/usr/bin/perl 
use strict;  
use warnings;  
use Shell;   

 my $logfile = "...";  
 my $run;  

 open $run, ("tail -f $logfile") or die " El fichero $logfile no tiene datos" and exit;  

 while (<$run>) {  
    ...  
 }

close ($run);

ERROR:

  

The file ... has no data at ... line 9.

Note. The log if it contains data and if it reads it to me but it does not continue executing waiting for new data:

open $run, $logfile or die " El fichero $logfile no tiene datos" and exit; 
    
asked by RyuK 23.02.2017 в 21:18
source

2 answers

1

In CPAN you will find modules to do that, like the case of File :: Tail

Example:

use File::Tail;

my $archivo = File::Tail->new("/ruta/al/archivo.log");
while (defined(my $linea = $archivo->read)) {
    print $linea;
}
    
answered by 24.02.2017 в 08:37
0

thank you all for your help here I leave you as I stay at the end

open (my $run, "tail -F -n +1 $logfile|") or die " El fichero $logfile no tiene datos" and exit;
    
answered by 27.02.2017 в 21:40