Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Re^5: trouble parsing log file...

by McDarren (Abbot)
on Nov 22, 2006 at 19:19 UTC ( #585599=note: print w/replies, xml ) Need Help??

in reply to Re^4: trouble parsing log file...
in thread trouble parsing log file...

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:

  • The $_ in while ($_ = <LOG>) { is redundant. That could be written as while (<LOG>) { and the effect would be exactly the same. Personally, I prefer to use an explicitly named variable, and so I would write it as: while (my $line = <LOG>) {, and then operate on $line within the loop.
  • Because you only need to print once, you can remove your print statements from the loop and save yourself a few lines of code.
  • print "<!--Content-type: text/html-->\n\n" sounds a bit of warning bell. If this is part of a larger script that is outputting HTML, then you really should be using the CGI module. Or at the very least, move that line (you only need to do it once) to near the top of your code.
  • In your assignments to the "buttons", you are again repeating code un-necessarily. Also, you can use the quoting operators (q,qq), which will save you having to do all that escaping.
  • It is generally a much better practise to use the three argument form of open

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

Darren :)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://585599]
[LanX]: It seems that PHP's official docs use "closure" and "anonymous function" interchangeably, even though their closures don't capture surrounding state without using the 'use' language construct.
LanX explains much cargo bla ...
[Eily]: what? I thought PHP was known for its reliable and consistent behaviour and documentation!
[LanX]: ... and a lot of pseudo Perl experts
[Eily]: though honestly, I'm sure I have seen people use closure to mean anonysub for perl as well

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (13)
As of 2018-02-23 17:11 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (303 votes). Check out past polls.