Can't get my head wrapped around CGI/Perl

Hello,

I am about 3 weeks new to CGI/ Perl scripting and so far some concepts I can wrap my head around perfectly but others not so much.

I was wondering if I could get some help in making a script that will pull from a text file and put back into a form. I can make a script that takes input from an HTML form and puts it back out as another Form, but I can't seem to get this to work back with only input from a TXT file. Any sort of online tutorial would be perfect.

I have been hitting Google since Saturday morning trying to find something out there that will all of a sudden make all I have read just click, but so far nothing has worked.

I'm a noob at scripting in general and I have done a lot of googling for help...this has helped me:

OOPWeb.com - Perl 4 Perl Newbies by Shlomi Fish

I love Perl, but if you're somewhat new to Perl and need to do a lot of scripting for the Web, try using PHP. It's similar to Perl, but much better suited for handling web form data.

Anyway, I don't understand your requirement to take input from a TXT file and output the data as a form. How is the input data structured, and what kind of form are you trying to make? Is there more than one input TXT file? Does each form look the same for each TXT file? How do you define the placement of elements on the page? What kind of validation do you need for user input? What happens to the form data once you've processed it?

Below the code is a rough breakdown of the TXT file I am pulling in from.
I ran a basic perl -c on this to see any syntax errors, and I got back a
"Global symbol "@semm_count" requires explicit package name at Line 24.

So I apparently have that variable set wrong, but I am not sure how. I am missing some larger piece and not sure what it is or how to get it to click in my head, I am thinking about this way to much. SQL wasn't this hard to pick up and neither was Flash or Javascript

All I need to get this to do is display:

Seminar Name: Number registered:
1 2
2 3
3 1
4 2
total Students registered: 8

#!c:/perl/bin/perl.exe
#super.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);
use strict;

#declare variables
my ($name, $semm, @records);
#my @semm_count = (0, 0, 0, 0);
my %semm_count = ("1", 0,
				  "2", 0,
				  "3", 0,
				  "4", 0);

#calculate survey statistics
open(INFILE, ">>", "c05ex5.txt")
	or die "Error opening c05ex5.txt. $!, stopped";
@records = <INFILE>;
close(INFILE);
foreach my $rec (@records) {
	chomp($rec);
	($name, $semm) = split(/,/, $rec);
	$semm_count[$semm] = $semm_count[$semm] + 1;
	}

#generate HTML acknowledgment
print "<HTML><HEAD><TITLE>WKRK-TV</TITLE></HEAD>\n";
print "<BODY>\n";
print "<H2>Seminar Total</H2>\n";

print "<TABLE>\n";
print "<TR><TD>Seminar</TD>    <TD>Total</TD></TR>\n";

foreach my $key ("Computer Maintenance", "Microsoft Office", "Unix Essentials", "CGI/PErl") {
	print "<TR><TD>$key</TD> <TD>$semm_count{$key}</TD></TR>\n";
	}

print "</TABLE>\n";
print "</BODY></HTML>\n";

Text file:
StudentA,1
StudentB,2
StudentC,3
StudentD,4
StudentE,1
StudentF,2
StudentG,2
StudentH,4

You sure you can't just do PHP? It's the P in "LAMP" or "XAMP". You can download an entire development kit here and have PHP running on your server in minutes: apache friends - xampp for windows for the server kit, and PHPEclipse - Trac for a great IDE.

At this point I cannot, I think I found part of my problem though.
After more research I changed a few sections to this:

#!c:/perl/bin/perl.exe
#super.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);
use strict;

#declare variables
my ($name, $semm, @records);
#my @semm_count = (0, 0, 0, 0);
my %semm_count = ("1", 0,
				  "2", 0,
				  "3", 0,
				  "4", 0);

#calculate survey statistics
open(INFILE, ">>", "c05ex5.txt")
	or die "Error opening c05ex5.txt. $!, stopped";
@records = <INFILE>;
close(INFILE);
foreach my $rec (@records) {
	chomp($rec);
	($name, $semm) = split(/,/, $rec);
	$semm_count{$semm}++;
	}

#generate HTML acknowledgment
print "<HTML><HEAD><TITLE>WKRK-TV</TITLE></HEAD>\n";
print "<BODY>\n";
print "<H2>Seminar Total</H2>\n";

print "<TABLE>\n";
print "<TR><TD>Seminar</TD>    <TD>Total</TD></TR>\n";

foreach my $key ("Computer Maintenance", "Microsoft Office", "Unix Essentials", "CGI/PErl") {
	print "<TR><TD>$key</TD> <TD>$semm_count{$key}</TD></TR>\n";
	}

print "</TABLE>\n";
print "</BODY></HTML>\n";

I've commented the bugs below...

#!c:/perl/bin/perl.exe
#super.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);
use strict;

#declare variables
my ($name, $semm, @records);
#my @semm_count = (0, 0, 0, 0);
my %semm_count = ("1", 0,
				  "2", 0,
				  "3", 0,
				  "4", 0);

#calculate survey statistics
open(INFILE, "<", "c05ex5.txt")       ## You had >> which APPENDS to the file in question. Now it reads from the file.
	or die "Error opening c05ex5.txt. $!, stopped";
@records = <INFILE>;
close(INFILE);
foreach my $rec (@records) {
	chomp($rec);
	($name, $semm) = split(/,/, $rec);
	$semm_count{$semm}= $semm_count{$semm} + 1;    ## You defined a hash, but were storing into a array. I changed it to store into the hash.
}

#generate HTML acknowledgment
print "<HTML><HEAD><TITLE>WKRK-TV</TITLE></HEAD>\n";
print "<BODY>\n";
print "<H2>Seminar Total</H2>\n";

print "<TABLE>\n";
print "<TR><TD>Seminar</TD>    <TD>Total</TD></TR>\n";

foreach my $key ("Computer Maintenance", "Microsoft Office", "Unix Essentials", "CGI/PErl") {
	print "<TR><TD>$key</TD> <TD>$semm_count{$key}</TD></TR>\n";   
## Previously, you defined the indexes of semm_count to be numbers, not words. Where do these key names come from anyway?
}

print "</TABLE>\n";
print "</BODY></HTML>\n";

I was being arbitrary with my names, just picking four from books on my desk.

Totally wrong about PHP better suited to handle form data than perl, but probably by this time you can tell this is school work. So I doubt the instructor would want the students in a perl course to use PHP.

Yea, this is 100% school work, after 3 days though, I have given up on getting this to click in my head and am moving on to the next set of assignments.

Still though if anyone has an y good sites out there, I am in need of them as a good reference. I don't want people doing the work for me, but at this point my "Search-Fu" has let me down as I can't find a whole lot that is geared towards the beginner.

I know nobody reads forum rules before posting, but it is against the rules of this forum to post school work.

There is tons of resources online geared towards beginners, the problem you are having is that your fundamentals are poor and you are trying to write a CGI script without first understanding some basics. Writing a CGI script generally comes after several chapters of more basic instuction:

simple scalars
string operators
numeric operators
lists (arrays and hashes)
loops
file I/O
subroutines and perl functions
modules and packages

and somewhere after all that:

CGI

Right now the biggest problem I can see is here:

foreach my $key ("Computer Maintenance", "Microsoft Office", "Unix Essentials", "CGI/PErl") {
	print "<TR><TD>$key</TD> <TD>$semm_count{$key}</TD></TR>\n";
	}

In the hash %semm_count there are no keys ("Computer Maintenance", "Microsoft Office", "Unix Essentials", "CGI/PErl"). You need to change the numbers in the file (1,2,3,4) to the names of the courses you listed.

StudentA,Computer Maintenance
StudentB,Microsoft Office
StudentC,Unix Essentials
StudentD,CGI/PErl
StudentE,Computer Maintenance
StudentF,Microsoft Office
StudentG,Microsoft Office
StudentH,CGI/PErl

Edit:

I see otheus already pointed this out :

That ends my participation in this thread. Good luck to you in all things.

I see what your saying, and I appreciate the help. I have dropped this assignment and will come back to it in a week or so, once my course is complete. Its just not worth the headache or stress.

There was actually zero build up to this assignment, as far as a groundwork progression. We jumped from basic data in a link, and data from a form reposting on a page to this.

Not to mention that disclaimers of "Warning! This information is incorrect in the textbook. You will get an error if you use the syntax in the book." are all over the course lectures.

Thomson Learning's Book on CGI is pretty horrid. I was able to each myself SQL / MySQL syntax and never had issues like this. (I used WROX books though, so it might just be the book itself and how it is written)

If you make the last change that has been suggested your script will work. :wink:

Bye.

Moderator's note:

The poster (sennex) initially did not ask for specific homework assignment help -- just help for links on how to do it him/herself. It was only after my prodding that he/she posted the specific problem, so I don't think a violation should be given to the user. However, I put the whole thread in here to prevent others (like classmates) from taking advantage of the solutions proffered.

-Otheus

Yes, and it is not worth the time of the experts here to spend time doing your homework for you.

Your post is a clear rule violation, as no homework should be posted here, under any circumstances.

The rules are clear. No homework.