http://www.perlmonks.org?node_id=1057852

Mark.Allan has asked for the wisdom of the Perl Monks concerning the following question:

Hello monks

I have a question about scoping. I have a logfile which is like this

The job started at Fri Oct 11 07:01:10 BST 2013 log4j:WARN No appenders could be found for logger log4j:WARN Please initialize the log4j system properly. Error checking credentials The job completed at Fri Oct 11 08:01:10 BST 2013

From this logfile I need to perform various check at 9am. One is to check job started on correct date, if it doesn’t print that is hasn’t, check that job has completed on correct date, again if it hasn’t then print it hasn’t. This check is done 9am every day. The script is working but the problem I am having is I want to store the last known output value even outside the time window, so if during the time window the error message is"The job failed to complete", I need to store this value outside the time window until the next days check. As it stands and I understand why but my output is getting overwritten with the default output when it completes the time block

Do I need to write this to a logfile or can I use some special array/variable retainer?

Script as is.

#!/usr/bin/perl
use strict; use warnings; my $joblog = "/usr/logs/app.log"; my $M1 = "The job completed successfully"; my $M2 = "The job failed to start"; my $M3 = "The job failed to complete"; my $M4 = "The log does not exist"; my @tm = localtime; my ($started,$completed); chomp(my $jobdate=`date +"%a %b %d"`); my @output; if (($tm[2] == 9) && ($tm[1] == 00)) { if (-e $joblog) { local $/; open(my $fh, '<', $joblog); my @content = <$fh>; for(@content) { next unless (/^The job/); if (m/^The job started at $jobdate/){ $started = 1;} push (@output,"$M2") if ! $started; print @output and exit if ! $started; if (/^The job ended at $jobdate/){ $completed = 1; push (@output,"$M1"); print @output and exit if $started;} } @output="$M3" if ! $completed; print @output and exit if ! $completed; } else{print "$M4";} } else { print @output if defined;}