Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Perl form/post question

Perl form/post question

Perl form/post question

The first PERL program that I want to write solo will allow my students to answer questions over the web via a form/post method. Getting the below book example program to work is sort of fundamental to writing my student submittal program (I know programs most likely already exist that allow students to submit answers to a teacher over the internet...but I'd like to learn PERL and how to do it myself).

The below file called "write.cgi" comes straight from a book on PERL5. The program, when combined with a HTML file called "write.html" should allow a web user to enter in some data in various fields, submit the date to ther web server, write the data to a file called 1.html located in a subfolder called 'numbers' located in the folder called 'cgi-bin', and then allow the web user to review the data submitted.

File "write.cgi" is located in a folder called "cgi-bin". I've confirmed that the file is functional by using the command 'perl write.cgi' which works great. It actually creates a file called 1.html in the 'numbers' subdirectory.

I've also confirmed that the file is being 'activated' via the web by renaming the file "write.cgi" to "write1.cgi". When I do this I get the 404 file not found html error when I hit the submit key. When the file is called "write.cgi" I don't get a 404 file not found error.

The web server is a unix system.
write.cgi permissions are set to 755
write.html permissions are set to 744
numbers folder within cgi-bin is set to 755

Now for the problem. When I access the html file "write.html" from the web I get the form as expected. When I fill in the form and hit the submit button I get the next html screen that allows me to select the file "1.html" which should allow me to go read the file that was created by the "write.cgi" script called "1.html". When I select "1.html" on the web browser, I get a 404 error: file doesn't exist.

I've checked via telnet and the file 1.html was never created/written on the server.

Sooo, the error appears to be that I'm not able to write the data on the form via the post method to a file on the server side.

Can anyone help me here.

FYI: I have worked this one for awhile. I've double checked the permissions on the folders, etc.

Thanks again...and below are the two files discussed above.


-----"write.html" is shown below without any changes from the book-----------

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<body bgcolor="#FFFFFF">
<form method="POST" action="cgi-bin/write.cgi">
<input type="text" name="Student Name" size"20">
<input type="text" name="Date" size="20">
<textarea rows="4" name="Answer" cols="20" wrap="virtual"></textarea>
<input type="submit" value="Submit" name="submit">
<input type="reset" value="Reset" name="reset">

---"write.cgi" is shown below without any changes fromt the book I'm using to learn PERL------

print "Content-type: text/html\n\n";
opendir(DIR, "./numbers");
while($name = readdir(DIR))
next if $name !~ /^\d*.html/;
push(@files, $name);
if($#files == 0)
$nextfile = "1.html";


$lastfile = $files[$#files];
$lastfile =~ s/.html//g;
$nextfile = $lastfile + 1;
$nextfile .= ".html";

open(OUT, ">numbers/$nextfile");
print OUT "<HTML>\n<HEAD>\n ";
print OUT "<TITLE>\n";
print OUT "$FORM{'title'}\n";
print OUT "</TITLE>\n";
print OUT "</HEAD>\n";
print OUT "<BODY BGCOLOR=\"#FFFFFF\">\n";
print OUT "<H1>\n";
print OUT "$FORM{'heading'}\n";
print OUT "</H1>\n";
print OUT "<P>\n";
print OUT "$FORM{'body'}\n";
push(@files, $nextfile);
print "<HTML>\n<BODY>\n";
foreach $file (@files)
print "<A HREF=\"numbers/$file\">$file</A>\n";
print "<BR>\n";
print "</BODY>\n</HTML>\n";

sub get_form_data
# Get the input
read(STDIN, $buffer, $ENV{ 'CONTENT_LENGTH' } );
# Split the name-value pairs
@pairs = split(/&/, $buffer);
foreach $pair (@pairs)
($name, $value) = split(/=/, $pair);
# Un-Webify plus signs and %-encoding
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$value =~ s/<!--(.¦\n)*-->//g;

$FORM{ $name } = $value;

RE: Perl form/post question

I cut and pasted the code into my htdocs and cgi-bin dirs, changed the 'action' in write.html to my cgi-bin dir, chmod +x on write.cgi, created the 'numbers' dir. and the code ran. There are few wrinkles with the way it runs, but that is not your problem (yet).

?Who owns the numbers directory? If you do (and you are not the httpd daemon), and you set the permissions to 755, then the httpd daemon ( any web server process) can not write to that dir. Try opening the dir up to 777 just to see if this is the problem. If it is, decide to leave it that way or chown on the dir to 'httpd' and set the permissions to 755 or whatever you like to remedy the problem.

Another view (of maybe the same problem)......
There is no error checking in the example from the book. Assuming a plain vanilla web server 'htdocs' directory and 'cgi-bin' directory, my suspicion ( is that spelled correctly?) is that you are not successfully opening your output file. Try adding a little simple error checking to the cgi......like this.....

'open(OUT, ">numbers/$nextfile") ¦¦ print "Failed to open $nextFile, $! <BR>\n";'

'"$!" - contains the most recent error. If the 'open' did not work, "$!" will show you what the OS said about the problem.'

If the 'open' fails, and since you have already printed the Content header to the browser, you should get the error statement in the resulting html page.

RE: Perl form/post question

Thank you! Your suggestion worked great, especially the description of how to do some simply error detection in the script.

Before changing the permissions on the file directory I inserted the error detection logic into the script. As suspected the error was due to file protection/access. I then changed the permission for the numbers directory and everything worked out.

I will use the error detection logic in future code.

Thanks again.


RE: Perl form/post question

Here is a bit of code you can look at or even use that I place in just about all of my scripts. This will give you a neat output page for your errors and even some extra info that might make errors easier to track. You can call this sub like so:

open(FILE, "myfile.html") ¦¦ &error("$!");

Yes I know it is a bit much but it helps track errors even when you are not the one running the script. Hope this helps.

$debug = 1; #make it zero if you don't want the env variables.
$prog_name= 'My Program'; #name of the program
$log = '/full/path/to/error/log/error.log'; #This is the full path to your error log.

sub error {
print "Content-type: text/html\n\n";
# Displays any errors and prints out FORM and ENV info.
($Second, $Minute, $Hour, $DayOfMonth, $Month, $Year, $Weekday,
$DayOfYear, $IsDST) = localtime(time);

$RealYear = $Year + 1900 if ($Year <= 99);
$RealYear = $Year + 2000 if ($Year > 99);
if($Month < 10) {$Month = "0" . $Month}
if($DayOfMonth < 10) {$DayOfMonth = "0" . $DayOfMonth}
if($Hour < 10) {$Hour = "0" . $Hour}
if($Minute < 10) {$Minute = "0" . $Minute}
if($Second < 10) {$Second = "0" . $Second}
$date = "$Hour:$Minute:$Second $Month-$DayOfMonth-$Year";
open(LOGS, ">>$log") ¦¦ print "can't open log file"; #use a print statement here otherwise you would end up in a never ending loop.
print LOGS "$prog_name¦$date¦$!";

print "<h3>Error: $!</h3>\n";
print "Message: $_[0]\n\n";
print "$_[1]\n\n$_[2]\n\n";
if ($debug == 1) {
print "<PRE>\n Form Variables \n";
foreach $key (sort keys %in) {
print "$key: \t$in{$key}\n";
print "\n Environment Variables \n";
foreach $env (sort keys %ENV) {
print "$env: \t$ENV{$env}\n";
print "\n</PRE>";

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close