nixnut has asked for the wisdom of the Perl Monks concerning the following question:

Originally posted under this thread - ed.

hi guys, newbie here :)
i have a logging script that i need this feature added to but i can't figure out how, at the kindergarten stage i'm afraid lol

https://nixnut.nz/home/accounts/logs.cgi

#!/usr/bin/perl use Time::Local; use CGI::Carp qw(fatalsToBrowser); use vars qw(%config %form %02d); local %config; $font = '<font face=verdana,helvetica size=1>'; $file = './weblog.log'; $root = 'nixnut.nz'; $config{'nztime'} = '12'; require "./seconds.cfg"; require "./howmany.cfg"; local %form = &get_form_data; print "Cache-control: no-cache\nPragma: no-cache\nContent-type: text/h +tml\n\n"; if ($form{'action'} eq '') { &display; } elsif ($form{'action'} eq 'seconds') { &seconds; } elsif ($form{'action'} eq 'howmany') { &howmany; } else { &display; } ########## this below has no effect use File::ReadBackwards ; # Object interface $bw = File::ReadBackwards->new( '$file' ) or die "can't read '$file' $!" ; while( defined( $log_line = $bw->readline ) ) { print $log_line ; } # ... or the alternative way of reading until ( $bw->eof ) { print $bw->readline ; } # Tied Handle Interface tie *BW, 'File::ReadBackwards', '$file' or die "can't read '$file' $!" ; while( <BW> ) { print ; } ########## ########## sub display { @days = ('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday',' +Saturday'); @months = ('January','February','March','April','May','June','July','A +ugust','September','October','November','December'); ($sec,$min,$hour,$mday,$mon,$year,$wday) = (gmtime(time + $config{'nzt +ime'}*3600))[0,1,2,3,4,5,6]; $time = sprintf("%02d:%02d:%02d",$hour,$min,$sec); $year += 1900; if ($hour eq "12") { $hour = "12"; $pm = "y"; } if ($hour eq "13") { $hour = "1"; $pm = "y"; } if ($hour eq "14") { $hour = "2"; $pm = "y"; } if ($hour eq "15") { $hour = "3"; $pm = "y"; }if ($hour eq "16") { $hour = "4"; $pm = "y"; } if ($hour eq "17") { $hour = "5"; $pm = "y"; } if ($hour eq "18") { $hour = "6"; $pm = "y"; } if ($hour eq "19") { $hour = "7"; $pm = "y"; } if ($hour eq "20") { $hour = "8"; $pm = "y"; } if ($hour eq "21") { $hour = "9"; $pm = "y"; } if ($hour eq "22") { $hour = "10"; $pm = "y"; } if ($hour eq "23") { $hour = "11"; $pm = "y"; } if ($hour eq "24") { $hour = "12"; $pm = "n"; } $time = sprintf("%02d:%02d",$hour,$min); $tdate = "$days[$wday], $months[$mon] $mday, $year"; $mon = $mon + 1; if ($pm eq "y") { $tm = "am"; # pm swapped to allow gmt in script (not needed anyway) } else { $tm = "pm"; # am swapped to allow gmt in script (not needed anyway) } &oops('cant open seconds') unless (open(SECONDS, "seconds.cfg")); ($form{'seconds'}) = <SECONDS>; close SECONDS; &oops('cant open howmany') unless (open(HOWMANY, "howmany.cfg")); ($form{'howmany'}) = <HOWMANY>; close HOWMANY; print "<html><head><meta http-equiv=\"refresh\" content=\"$form{'secon +ds'};url=logs.cgi\"><title>Tracking for $root</title></head><body bgc +olor=white text=\"#36078d\" alink=\"yellow\" link=\"#0000ff\" vLink=\ +"#0000ff\" leftMargin=\"0\" rightMargin=\"0\" topMargin=\"0\" MARGINW +IDTH=\"0\" MARGINHEIGHT=\"0\">"; print "\n\n$tdate<center><table bgcolor=#000000 width=\"100%\" border= +\"0\" cellpadding=3 cellspacing=1><tr bgcolor=black><th align=\"left\ +" bgcolor=white>$font Time</th><th bgcolor=white>$font User</th><th b +gcolor=white>$font Host</th><th bgcolor=white>$font Page</th> <th bgc +olor=white>$font Browser</th></tr>\n"; open (FILE, "tail -$form{'howmany'} $file | tail -$form{'howmany'} |") + or die "Opening $file: $!"; while (<FILE>) { chomp; s/^\"//; s/\"$//; my ($date, $user, $host, $page, $browser) = split(' ', $_); print "<tr bgcolor=black><td bgcolor=white align=\"left\" valign=\"top +\">$font $date</td><td bgcolor=white align=\"left\" valign=\"top\"><a + href=\"http://nixnut.nz/~$user\" target=\"_blank\">$font $user</a></ +td><td bgcolor=white align=\"left\" valign=\"top\">$font <a href=\"ht +tps://www.iptolocation.io/ip-lookup/$host\">$host</a></td><td bgcolor +=white align=\"left\" valign=\"top\"><a href=\"$page\" target=\"_blan +k\">$font $page</a></td> <td bgcolor=white align=\"left\" valign=\"to +p\">$font $browser</td></tr>\n"; } close (FILE); print "</table></center>"; print "<table border=\"0\" cellpadding=0 cellspacing=0><tr>"; print "<td align=\"left\" valign=\"bottom\">$font Last refresh: <b>$ti +me</b></td>"; print "<td width=30></td>"; print "<form action=logs.cgi?action=seconds method=post><td align=\"le +ft\" valign=\"top\">$font<INPUT TYPE=HIDDEN NAME=seconds VALUE=\"$for +m{'seconds'}\">Refresh rate: <input style=\"background-color:#000000; +font:verdana;color:#ffffff;font-size:xx-small;\" type=text name=secon +ds size=4 value=\"$form{'seconds'}\"> <input style=\"background-color +:#000000;font:verdana;color:#ffffff;font-size:xx-small;\" type=submit + value=\"change\"></form></td>"; print "<td width=30></td>"; print "<form action=logs.cgi?action=howmany method=post><td align=\"le +ft\" valign=\"top\">$font<INPUT TYPE=HIDDEN NAME=howmany VALUE=\"$for +m{'howmany'}\">\# of entries: <input style=\"background-color:#000000 +;font:verdana;color:#ffffff;font-size:xx-small;\" type=text name=howm +any size=4 value=\"$form{'howmany'}\"> <input style=\"background-colo +r:#000000;font:verdana;color:#ffffff;font-size:xx-small;\" type=submi +t value=\"change\"></form></td>"; print "</tr></table>"; print "</body></html>"; exit; } #-##################### Sub: change refresh sub seconds { $form{'seconds'} = $seconds if !($form{'seconds'}); &oops('cant open file.') unless (open SECONDS, ">seconds.cfg"); print SECONDS "$form{'seconds'}"; close SECONDS; &display; } #-##################### Sub: change howmany sub howmany { $form{'howmany'} = $howmany if !($form{'howmany'}); &oops('cant open file.') unless (open HOWMANY, ">howmany.cfg"); print HOWMANY "$form{'howmany'}"; close HOWMANY; &display; } #-##################### Sub: Get Form Data This gets data from a post. sub get_form_data { my $temp; my $buffer; my @data; read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'}); foreach $temp (split(/&|=/,$buffer)) { $temp =~ tr/+/ /; $temp =~ s/%([0-9a-fA-F]{2})/pack("c",hex($1))/ge; $temp =~ s/[\r\n]/ /g; push @data, $temp; } foreach $temp (split(/&|=/,$ENV{'QUERY_STRING'})) { $temp =~ tr/+/ /; $temp =~ s/%([0-9a-fA-F]{2})/pack("c",hex($1))/ge; $temp =~ s/[\r\n]/ /g; push @data, $temp; } return @data; }
thx

Replies are listed 'Best First'.
Re: Need help using File::ReadBackwards
by hippo (Archbishop) on Sep 16, 2024 at 09:18 UTC
    use File::ReadBackwards ; # Object interface $bw = File::ReadBackwards->new( '$file' ) or die "can't read '$file' $!" ;

    This won't have the desired effect because you have enclosed the variable holding the file name inside single quotes and so it won't interpolate. Just lose the quotes entirely - they are serving no purpose here. eg:

    #!/usr/bin/env perl use strict; use warnings; use utf8; use File::ReadBackwards; my $file = 'weblog.log'; my $bw = File::ReadBackwards->new ($file) or die "can't read '$file' $!"; while (defined (my $log_line = $bw->readline)) { print $log_line; }
    $ cat weblog.log first line second line line the third $ ./rb.pl line the third second line first line $

    The script you have shown us looks to have been written a very long time ago. There are many ways in which it would be different if written today but here are two fairly simple but effective tips:

    1. Use strict and warnings as I have done in the example above. These will save you time and again.
    2. Replace all those repetitive if ($hour eq "18") ... blocks with a loop or some broader logic. When there is an obvious pattern like that there is no need to go repeating yourself over and over. Better yet, use one of the many good time-handling modules so you don't have to bother with any of that low-level stuff in the first place.

    Good luck!


    🦛

      thank you, i will try to implement what you have provided, much appreciated