Okay, most important comment first:

What you have there will only ever read one line of your logfile, and then exit. That is because you finish each of your conditional blocks with a last;.

I suspect that you are somewhat confused about what the while ($_ = <LOG>) { line does. From the code you have written, it appears that you are under the impression that it will read the entire file in a single iteration of the while loop. This is not the case. It reads one line at a time. Therefore - because you've included a last in each conditional block - it's going to exit the while loop after reading one line only!

To make the above work like you want it to, you simply need to remove the last from the 2nd and 3rd conditionals. ie. Only use last to exit the loop if you find an "$error". Update: actually, even then it wont work - because you'll be printing your HTML header and a button for EVERY line in your logfile. So you really need to make some changes as I've outlined below.

Now, a couple of other comments:

So, re-writing your code and implementing those few changes:

use strict; use warnings; use CGI qw(standard); my $logfile="log.txt"; my $error="DOWN"; my $warn="PROBLEM"; my $imagedir = 'default_files'; my ($redbutton, $greenbutton, $yellowbutton) = q(perlredblink.gif perlyellowblink.gif perlgreenblink.gif); my $button = $greenbutton; print header(); open LOG, '<', $logfile or die "Cannot open $logfile for read :$!"; while (my $line = <LOG>) { if ($line =~ /$error/i) { $button = $redbutton; last; } elsif ($line =~ /$warn/i) { $button = $yellowbutton; } } close LOG; print qq(<img src="$imagedir/$button">);

Disclaimer: The above is untested, and has been written at 3am after I've just gotten off a flight from Hong Kong to Manila - so it is almost certainly buggy. But I hope it helps anyway ;)

Cheers,
Darren :)


In reply to Re^5: trouble parsing log file... by McDarren
in thread trouble parsing log file... by perl_geoff

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.