#!/usr/local/bin/perl5 -w
#  Simple Basic Read a File in array context Example
#
$today =`date`;
chomp($today); # Today's date captured from UNIX
#
print <<EOF; # Generate the HTML responding page starting with HTTP line
Content-Type: text/html

<html>
<head>
<title>This is Result of my Second Basic Perl Input CGI Script </title>
</head>
<body bgcolor="white" >
<h1>This Script reads and Writes a cannedfile on a great $today</h1>
<font color=red size=7 > 
EOF
#
open(CANNEDFILE,'<cannedfile') || die("Cannot open designated input file\n"); # Open file or die
@cannedfile =<CANNEDFILE>; # Read in whole file
if( defined($cannedfile[0]) )	{
	print "First line of file is defined!<br>\n";
}
$cannedfile = @cannedfile; # Length of file in lines
print "File has $cannedfile lines in it<br>\n";
unless( defined($cannedfile[$cannedfile]) )	{
	print "But last line plus one is undefined<br>\n";
}
#
# Loop over input lines
foreach $line (@cannedfile) { # Could use for loop running from 0 ... $cannedfile-1
	chomp($line); # remove end of line character(s)
	$line = &tohtml($line); # Convert any special HTML characters
	print "$line<br>\n"; # Note for HTML need <br> to get a newline!
}
print <<EOF; # Generate End of HTML Page
</font>
</body>
</html>
EOF
exit; # End Program
#
sub tohtml      { # Convert HTML magic characters  & < > "in current line
        my($convline)=@_;

        return($convline)       unless $convline;
        $convline =~ s/&/&amp;/g;
        $convline =~ s/</&lt;/g;
        $convline =~ s/>/&gt;/g;
        $convline =~ s/"/&quot;/g;
        $convline; # do not need a return
}
