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;}

In reply to array scoping by Mark.Allan

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.