Hi I'm trying to print to an HTML file but I'm having some issues calling a variable because it doesn't get defined until later in the script. I have a log file and I need to calculate the number of unique visitors as well as its start date (the earliest date) and then print that information at the top of the HTML file. My problem is those variables aren't defined yet! How can I make this work?
#! /usr/bin/perl
# CSC 310 Project # 4
use 5.010;
use Time::Local;
#opening log and html file
open (LOG, '<', 'IN-access.log');
open (HTML, '>', 'OUT-access.html');
#printing header/title/format of html
print HTML "<HTML><TITLE>Visitors Log</TITLE><BODY>\n";
print HTML "The log file start date is: $startDate <BR>\n";
print HTML "There were $IPcount unique visitors in the logfile.<BR>\n"
+;
print HTML "There were visits yesterday<BR>\n";
print HTML "<TABLE border=1><TR><TD>IP</TD><TD>LOGFILE</TD></TR>\n";
##############
#replaces the month with its corresponding number
sub convert{
($day,$month,$year) = split '/', $formattedDate;
#print "$day $month $year\n";
%dates = (
'Jan' => '00','Feb' => '01','Mar' => '02',
'Apr' => '03','May' => '04','Jun'=> '05',
'Jul' => '06','Aug' => '07','Sep' => '08',
'Oct' => '09','Nov' => '10','Dec' => '11',
);
foreach $char ($month){
$char =~ s/.../$dates{$month}/;
}
$modified = "$day/$month/$year";
$modified;
}
###############
#reading log file
while ($lines = <LOG>){
#assigning values
($remoteIP,$rfc,$userID,$dateTime,$timeZone,$requestType,$fileRequ
+ested,$requestProtocol,$statusCode,$sizeOfFile) = split ' ', $lines;
####### converting month to a number, then the date to epoch time
$formattedDate = substr($dateTime, 1, 11);
#gets hrs,mins,secs from $dateTime
($hr,$min,$sec) = split ':', substr($dateTime, 13,8);
#converts month to corresponding number
$modifiedDate = &convert($formattedDate);
#open (HEAD, '>>', 'ipDate.txt');
#print HEAD "$remoteIP,$modifiedDate\n";
($DAY,$MONTH,$YEAR) = split '/', $modifiedDate;
$logDate = timelocal($sec,$min,$hr,$DAY,$MONTH,$YEAR);
push(@listOfDates, $logDate);
@sortedDates = sort {$a <=> $b} @listOfDates;
#start date of log file
my $startDate = $sortedDates[0];
print scalar(localtime($startDate))."\n";
#keeps track of unique visitors
#checks if remoteIP is already in the array. If not, counter increa
+ses
#and remoteIP gets added to array
if ($remoteIP ~~ @listOfIPs){
$IPcount = $IPcount;
}else{
$IPcount += 1;
push(@listOfIPs, $remoteIP);
}
#verifies values were assigned properly
print "\nUnique Visitors: $IPcount\n";
print "$remoteIP\n"."$rfc\n"."$userID\n"."$dateTime "."$timeZone\n
+"."$requestType $fileRequested $requestProtocol\n"."$statusCode\n"."$
+sizeOfFile\n\n";
#printing data to HTML file
print HTML "<TR><TD>$remoteIP</TD><TD>$remoteIP $rfc $userID $date
+Time $timeZone $requestType $fileRequested $requestProtocol $statusCo
+de $sizeOfFile</TD></TR>\n";
print "\n\nNEXT\n\n";
}