Click to See Complete Forum and Search --> : basic perl : printing a file to an html page
Hi, I've got a .pl writing a page of html, and I need to have it read a file onto the page, the print works fine in DOS, but displays nothing in the HTML, here is a code snippet...
$FN="8_2003.txt";
open(fIN,$FN);
@info=<fIN>;
close(fIN);
foreach $line (@info)
{
$lines[$i]=$line;
$i++;
}
print $lines[0];
Any Ideas on why this wouldn't work?
Thanks,
Sam
...if you want to see HTML output, then, you'll need to print a header and then write your html code
i did... here's entire .pl src
#!/usr/bin/perl
print "Content-type: text/html\n\n";
print "<html>\n<head>\n<title>\nMedford School District Message Board\n</title>\n<link rel=\"stylesheet\" type=\"text/css\" href=\"style.css\">\n";
print "<script type=\"text/javascript\">\nfunction rowCols(ref, col, stat)\n{\n var tds = ref.children?ref.children:(ref.childNodes?ref.childNodes:null);\n if (tds) for (var td = 0; td < tds.length; td++)\n if (tds[td].style && tds[td].tagName.toLowerCase()=='td') with (tds[td].style)\n {\n backgroundColor = col;\n cursor = document.all?'hand':'pointer';\n }\n}\n</script>";
print "</head>\n<body bgcolor=\"#efefff\">";
$ipAddress = $ENV{'REMOTE_HOST'};
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$y=$year;
$m=$mon;
($mon1,$year1)=split(/&/,$ENV{'QUERY_STRING'});
($name1,$mon)=split(/=/,$mon1);
($name2,$year)=split(/=/,$year1);
#$FN=sprintf("%d_%d.cal",$mon,$year);
$FN="8_2003.txt";
open(fIN,$FN);
@info=<fIN>;
close(fIN);
foreach $line (@info)
{
$lines[$i]=$line;
$i++;
}
print $lines[0];
$response=~s/\+/ /g;
$response=~s/\Q%0D%0A\E/<br>/g;
#$response=~s/\%27/'/g;
#$response=~s/\%22/"/g;
$response=~s/\%7E/`/g;
$response=~s/\%21/!/g;
$response=~s/\%23/#/g;
$response=~s/\%24/\$/g;
$response=~s/\%25/\%/g;
$response=~s/\%5E/^/g;
$response=~s/\%26/&/g;
$response=~s/\%28/(/g;
$response=~s/\%29/)/g;
$response=~s/\%3D/=/g;
$response=~s/\%2B/+/g;
$response=~s/\%3C/<\;/g;
$response=~s/\%3E/>\;/g;
$response=~s/\%2F/\//g;
open(fOut,"responses.txt");
flock(fOut,2);
seek(fOut,0,2);
$totDays=0;
$daysThisMonth=$mday+1;
$y+=1900;
if($m!=$mon||$y!=$year)
{
do
{
dIM($m);
if($totDays==0)
{
$totDays=$daysInMonth-$daysThisMonth+1;
$m++;
if($m==12)
{
$m=0;
$y++;
}
}
elsif($m!=$mon&&$totDays>0)
{
$totDays+=$daysInMonth+2;
$m++;
if($m==12)
{
$m=0;
$y++;
}
}
}
until($y==$year&&$m==$mon);
$totDays++;
print "\n\n$totDays\n\n";
$startDay=$totDays%7+$wday;
}
else
{
#change this Month's startday monthly
$startDay=2;
}
dIM($mon);
print fOut "<td>\n$response\n</td>\n<td>\n$logTime\n</td>\n<td>\n $ipAddress\n</td>\n";
print "\n<div style=\"position:absolute;top:0;left:0\"><table width=156 height=120 cellpadding=0 cellspacing=0>\n<tr style=\"background:#006699\">\n";
print "<td align=center colspan=7 style=\"font-size:9pt\">\n$month $year\n</td>\n";
print "</tr><tr><td width=22 style=\"font-size:9pt\">S</td><td width=22 style=\"font-size:9pt\">M</td><td width=22 style=\"font-size:9pt\">T</td><td width=22 style=\"font-size:9pt\">W</td><td width=22 style=\"font-size:9pt\">T</td><td width=22 style=\"font-size:9pt\">F</td><td width=22 style=\"font-size:9pt\">S</td></tr>";
$count=0;
$datecount=1;
for($i=0;$i<5;$i++)
{
if($i%2==1)
{
print "<tr>";
}
else
{
print "<tr style=\"background:#006699\">"
}
for($j=0;$j<7;$j++)
{
$count++;
if($count>=$startDay)
{
$text=$datecount;
$datecount++;
}
else
{
$text=" ";
}
if($datecount>=$daysInMonth+2)
{
$text=" ";
}
print "<td width=22 style=\"font-size:9pt\">$text</td>";
}
</tr>
}
print "</tr></table></div>";
print "</body></html>";
close(fOut);
sub dIM($mon)
{
if($mon==0)
{
$month="January";
$daysInMonth=31;
}
if($mon==1)
{
$month="February";
if($year%4!=0&&$year%1000!=0)
{
$daysInMonth=28;
}
else
{
$daysInMonth=29;
}
}
if($mon==2)
{
$month="March";
$daysInMonth=31;
}
if($mon==3)
{
$month="April";
$daysInMonth=30;
}
if($mon==4)
{
$month="May";
$daysInMonth=31;
}
if($mon==5)
{
$month="June";
$daysInMonth=30;
}
if($mon==6)
{
$month="July";
$daysInMonth=31;
}
if($mon==7)
{
$month="August";
$daysInMonth=31;
}
if($mon==8)
{
$month="September";
$daysInMonth=30;
}
if($mon==9)
{
$month="October";
$daysInMonth=31;
}
if($mon==10)
{
$month="November";
$daysInMonth=30;
}
if($mon==11)
{
$month="December";
$daysInMonth=31;
}
return $daysInMonth;
}
GaijinPunch
09-10-2003, 03:30 AM
What are you using to view the output?
Why not print out to a file, then open it up in notepad, or a terminal if you're on Linux.
You can at least see the HTML code and make sure you've got the headers in the right place... I've had that problem a number of times, and it was always header problem.
I've ran this through activePerl, (in cmd for windows) and it is producing the correct output, but for some reason none of the text grabbed from the file shows up...
let's see this file:
8_2003.txt
I can't get this thing to run either...
ok, thanx...twice, huh...?
the purpose of this script is to be an event calender, so there will eventually be more info in the text file, but for now, it is just the day of the week that the month starts on...
Scriptage
09-10-2003, 07:50 PM
Changing:
$FN="8_2003.txt";
open(fIN,$FN);
@info=<fIN>;
close(fIN);
foreach $line (@info)
{
$lines[$i]=$line;
$i++;
}
print $lines[0];
to:
open(FIN, "8_2003.txt");
@info = <FIN>;
close(FIN);
print @info;
No luck there... it seems to be a permissions error, but I can't seem to get it fixed with the chmod function... I put an || die on the open function and it doesn't work at all... the program won't write files either... any ideas?