#!/usr/bin/perl
use strict;
my %hash = read_file('test.txt');
print 'test: ' . $hash{'test'} . "\n";
$hash{'test'}++;
print 'test: ' . $hash{'test'} . "\n";
write_file('test.txt', %hash);
sub read_file {
my $file = shift;
my %hash = ();
if(!-e $file)
{
warn "file '" . $file . "' does not exist!\n";
return ();
}
open(FILE, '<', $file) || die "Error opening '" . $file . "': " . $!;
while(my $line = <FILE>)
{
chomp($line);
next if(!$line);
print $line . "\n";
my ($key, $value) = split(/\s*=\s*/, $line, 2);
$hash{ $key } = $value;
}
close(FILE);
return %hash;
}
sub write_file {
my $file = shift;
my %hash = @_;
open(FILE, '>', $file) || die "Error opening '" . $file . "': " . $!;
for my $key (keys %hash)
{
print $key . " = " . $hash{ $key } . "\n";
print FILE $key . ' = ' . $hash{ $key } . "\n";
}
close(FILE);
}
#!/usr/bin/perl
#final.cgi - saves form data to a file, and creates a dynamic
#Web page that displays a message and survey statistics
print "Content-type: text/html\n\n";
use CGI qw(:standard -debug);
use strict;
#declare variables
my ($college, $mascot, $size, @records, @errors);
my %college_count = ("Baker", 0,
"Michigan", 0,
"State", 0,
"Eastern", 0,
"Central", 0,
"Wayne", 0,
"Lawrence", 0,
"Mercy", 0);
my %mascot = ("Baker", "Bears",
"Michigan","Wolverines",
"State", "Spartans",
"Eastern", "Eagles",
"Central", "Chippewas",
"Wayne", "Warriors",
"Lawrence", "Devils",
"Mercy", "Titans");
#assign input items to variables
$college = param('College');
#validate input data
if($college ne "Baker" and $college ne "Michigan" and $college ne "State" and $college ne "Eastern" and $college ne "Central" and $college ne "Wayne" and $college ne "Lawrence" and $college ne "Mercy"){
push(@errors, "Select a college");
}
#determine size of @errors array
$size = @errors;
#process input data or display error page
if($size == 0){
#process input data
#save form data to a file
open(OUTFILE, ">>", "survey.txt")
or die "Error opening survey.txt. #!, stopped";
print OUTFILE "$college\n";
close(OUTFILE);
#calculate survey statistics
open(INFILE, "<", "survey.txt")
or die "Error opening survey.txt. $!, stopped";
@records = <INFILE>;
close(INFILE);
foreach my $rec (@records) {
chomp($rec);
($college) = split(/,/, $rec);
$college_count{$college} = $college_count{$college} + 1;
}
#generate HTML acknowledgment
print "<HTML><HEAD><TITLE>Name that Mascot</TITLE></HEAD>\n";
print "<BODY>\n";
print "<H2>Thank you for visiting the American College web site!</H2>\n";
print "<H2>You selected $college.</H2>\n";
foreach $key ("Baker", "Michigan", "State", "Eastern", "Central", "Wayne", "Lawrence", "Mercy"){
print "$key is home of the $mascot{$key}\n";
}
print "<EM><B>See the current college results below:</EM></B>\n";
print "<TABLE>\n";
foreach my $key ("Baker", "Michigan", "State", "Eastern", "Central", "Wayne", "Lawrence", "Mercy") {
print "<TR><TD>$key</TD> <TD>$college_count{$key}</TD></TR>\n";
}
print "</TABLE>\n";
print "</BODY></HTML>\n";
}
else {
#display error page
print "<HTML><HEAD><TITLE>Name that Mascot</TITLE></HEAD>\n";
print "<BODY>\n";
print "<H2>You did not make a valid selection. Please click the BACK button and your browser \n";
print "and complete the survey. Thank you! </H2><BR>\n";
for(my $x = 0; $x < $size; $x = $x +1){
print "$errors[$x]<BR>\n";
}
print "</BODY></HTML>\n";
}
foreach $key ("Baker", "Michigan", "State", "Eastern", "Central", "Wayne", "Lawrence", "Mercy") { foreach my $key ("Baker", "Michigan", "State", "Eastern", "Central", "Wayne", "Lawrence", "Mercy") { use CGI::Carp qw(fatalsToBrowser);
(offline mode: enter name=value pairs on standard input; press ^D or ^Z when done)
College=Baker
Use of uninitialized value in addition (+) at final.cgi line 55.
Use of uninitialized value in addition (+) at final.cgi line 55.
<HTML><HEAD><TITLE>Name that Mascot</TITLE></HEAD>
<BODY>
<H2>Thank you for visiting the American College web site!</H2>
<H2>You selected Baker.</H2>
Baker is home of the Bears
Michigan is home of the Wolverines
State is home of the Spartans
Eastern is home of the Eagles
Central is home of the Chippewas
Wayne is home of the Warriors
Lawrence is home of the Devils
Mercy is home of the Titans
<EM><B>See the current college results below:</EM></B>
<TABLE>
<TR><TD>Baker</TD> <TD>1</TD></TR>
<TR><TD>Michigan</TD> <TD>0</TD></TR>
<TR><TD>State</TD> <TD>0</TD></TR>
<TR><TD>Eastern</TD> <TD>0</TD></TR>
<TR><TD>Central</TD> <TD>0</TD></TR>
<TR><TD>Wayne</TD> <TD>0</TD></TR>
<TR><TD>Lawrence</TD> <TD>0</TD></TR>
<TR><TD>Mercy</TD> <TD>0</TD></TR>
</TABLE>
</BODY></HTML>
#!/usr/bin/perl
#final.cgi - saves form data to a file, and creates a dynamic
#Web page that displays a message and survey statistics
print "Content-type: text/html\n\n";
use CGI qw(:standard -debug);
use strict;
#declare variables
my ($college, $mascot, $size, @records, @errors);
my %college_count = ("Baker", 0,
"Michigan", 0,
"State", 0,
"Eastern", 0,
"Central", 0,
"Wayne", 0,
"Lawrence", 0,
"Mercy", 0);
my %mascot = ("Baker", "Bears",
"Michigan","Wolverines",
"State", "Spartans",
"Eastern", "Eagles",
"Central", "Chippewas",
"Wayne", "Warriors",
"Lawrence", "Devils",
"Mercy", "Titans");
#assign input items to variables
$college = param('College');
#validate input data
if($college ne "Baker" and $college ne "Michigan" and $college ne "State" and $college ne "Eastern" and $college ne "Central" and $college ne "Wayne" and $college ne "Lawrence" and $college ne "Mercy"){
push(@errors, "Select a college");
}
#determine size of @errors array
$size = @errors;
#process input data or display error page
if($size == 0){
#process input data
#save form data to a file
open(OUTFILE, ">>", "survey.txt")
or die "Error opening survey.txt. #!, stopped";
print OUTFILE "$college\n";
close(OUTFILE);
#calculate survey statistics
open(INFILE, "<", "survey.txt")
or die "Error opening survey.txt. $!, stopped";
@records = <INFILE>;
close(INFILE);
foreach my $rec (@records) {
chomp($rec);
($college) = split(/,/, $rec);
$college_count{$college} = $college_count{$college} + 1;
}
#generate HTML acknowledgment
print "<HTML><HEAD><TITLE>Name that Mascot</TITLE></HEAD>\n";
print "<BODY>\n";
print "<H2>Thank you for visiting the American College web site!</H2>\n";
print "<H2>You selected $college.</H2>\n";
foreach my $key ("Baker", "Michigan", "State", "Eastern", "Central", "Wayne", "Lawrence", "Mercy"){
print "$key is home of the $mascot{$key}\n";
}
print "<EM><B>See the current college results below:</EM></B>\n";
print "<TABLE>\n";
foreach my $key ("Baker", "Michigan", "State", "Eastern", "Central", "Wayne", "Lawrence", "Mercy") {
print "<TR><TD>$key</TD> <TD>$college_count{$key}</TD></TR>\n";
}
print "</TABLE>\n";
print "</BODY></HTML>\n";
}
else {
#display error page
print "<HTML><HEAD><TITLE>Name that Mascot</TITLE></HEAD>\n";
print "<BODY>\n";
print "<H2>You did not make a valid selection. Please click the BACK button and your browser \n";
print "and complete the survey. Thank you! </H2><BR>\n";
for(my $x = 0; $x < $size; $x = $x +1){
print "$errors[$x]<BR>\n";
}
print "</BODY></HTML>\n";
}
foreach $key (@mascot){
print "$key is home of the $mascot{$key}\n";
}
ln -s /usr/bin/perl "/usr/bin/perl$(echo -e "\r")"