« Assignment 1 | Main | class 03 »
February 07, 2005
Assignment 2
Since I missed class, this is more my style of who I would do it, using the example code.
Code is in read more
This is the cgi:
#!/usr/bin/perl
#########################################################
# class02-file1.pl - Basic File I/O
#########################################################
# Use CGI Perl module to output any error msgs to browser
use CGI::Carp qw(fatalsToBrowser);
# Define the name of the script just for reference
$scriptName = "test.cgi";
$pageTitle = "thoughts";
# Obtain any variables submitted either in the URL string
# or by an HTML form via the GET or POST method, and
# place in the %INPUT_VARS hash array
%INPUT_VARS = {};
&input_vars_receive;
&input_vars_parse;
#########################################################
# Define which file we will be using on the server
#########################################################
$myFile = "/home/dimitri/public_html/blog/dweb/file1.txt";
#########################################################
# Define our widgets here and collect values from INPUT_VARS array
#########################################################
$textWidgetLabel = "Penny for a thought:";
$textWidgetName = "Thought";
$textWidgetValue = $INPUT_VARS{$textWidgetName};
$submitWidgetName = "dataAction";
$submitWidgetValue = "Enter";
$commentWasSubmitted = 0;
#########################################################
# Check our inputs and perform any file content updates
#########################################################
if ($INPUT_VARS{$submitWidgetName} eq "$submitWidgetValue")
{ # Form was submitted so check if the comment field actually had text
if ($textWidgetValue ne "")
{ # This is how you write text to a file in Perl
open INPTR, ">>$myFile";
print INPTR "
";
print INPTR "$textWidgetValue\n";
print INPTR "
close INPTR;
# Set a flag so that we know that we actually had a valid comment
$commentWasSubmitted = 1;
}
}
# Tell the browser what type of data to expect
print "Content-type: text/html\n\n";
#########################################################
# Print our initial HTML
#########################################################
print qq^
^;
print qq^
^;
# Give the user feedback if they submitted something
if ($commentWasSubmitted==1)
{ print qq^
Its good to think! |
read the thoughts |
^;
}
print qq^