gotpong has asked for the wisdom of the Perl Monks concerning the following question:

Don't know if this is the right avenue to take, but I'm new to programming perl and tried to make a cgi program that doesn't work.

Didn't know if there was anyone that was out there that could give me some advice or help me out. I mean I don't know if I'm close or if I'm way off and I haven't found many good sources to try and correct myself.

Any way so that I don't have a ton of code on this site, the URL for the source is http://www.gotpong.com/sourcecode/rateatablescript.txt. Any help is appreciated. So would any recommendations for good ready materials about perl as well, thanks

janitored by ybiC: Retitle from "Don't know if this is the right avenue to take", broke out into paragraphs for legibility, made URL live using [ and ], added code from URL

Code from above URL follows:

#!/usr/bin/perl -w read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'}); $rating = substr($buffer,0,1); $table = substr($buffer,1,(length($buffer)+1)); $temp = "$table" . "ips" . ".txt"; $temp1 = "$table" . "results" . ".txt"; $ipaddress = $ENV{'REMOTE_ADDRESS'}; $IPcheck = 0; open(TABLEIP, "<$temp"); #test IP addresses while(<TABLEIP>) { $thisline = $_; chomp($thisline); if($thisline == $ipaddress) { $IPcheck = 1; {last} } else { $IPcheck = 0; } } close(TABLEIP); #if ip address is already used it responds they already voted else it +adds the ip address to the data list if($IPcheck == 1) { print "Content-type:text/html\n\n"; print <<EndOfHTML; <HTML> <HEAD><TITLE>$table</TITLE></HEAD> <BODY BACKGROUND="FFFFFF"> <TABLE> <TR><TD><IMG SRC="gotponglogo.gif"></TD></TR> <TR><TD><FONT FACE="ARIAL" SIZE="+1"><B>You already voted on this +table.</B></TD></TR> </TABLE> </BODY> </HTML> EndOfHTML exit (0); } else { open(TABLEIP, ">>temp"); print TABLEIP "ipaddress"; close(TABLEIP); } open(TABLERESULTS, "$temp1"); $rating = $rating + 1; $counter = 0; while(<TABLERESULTS>) { $thisline = $_; $counter++; if($counter == 1) { $scoretotal = chomp($thisline); } if($counter == 2) { $votetotal = chomp($thisline); } if($counter > 2) { {last} } } close(TABLERESULTS); $scoretotal = $scoretotal + $rating; $votetotal = $votetotal + 1; $newrating = $scoretotal / $votetotal; print "Content-type:text/html\n\n"; print <<EndOfHTML; <HTML> <HEAD><TITLE>$table</TITLE></HEAD> <BODY BACKGROUND="FFFFFF"> <TABLE> <TR><TD><IMG SRC="gotponglogo.gif"></TD></TR> <TR><TD><font face="arial" size="-1">The score of this table is $ne +wrating</td></tr> <TR><TD><font face="arial" size="-1">There was $votetotal votes</td +></tr> <TR><TD><font face="arial" size="-1">Your rating was $rating</td></ +tr> </TABLE> </BODY> </HTML> EndOfHTML open(TABLERESULTS, ">temp1"); $counter1 = 1; until($counter1 == 3) { if($counter1 == 1) { print TABLERESULTS "$scoretotal\n"; } if($counter1 == 2) { print TABLERESULTS "$votetotal\n"; } $counter++; } close(TABLERESULTS); }

Replies are listed 'Best First'.
Re: Resources for learning Perl with CGI?
by kvale (Monsignor) on Sep 16, 2003 at 23:23 UTC
    It is OK to post code, we like code :)

    Looking at the code, I see that you don't use the CGI perl module. That module is specialized for easing the burden of parsing input and writing dynamic web pages. I'd recommend checking it out. This tutorial by the creator of CGI.pm may help.

    -Mark

Re: Resources for learning Perl with CGI?
by atcroft (Abbot) on Sep 16, 2003 at 23:24 UTC

    First of all, welcome to the Monastery. I hope you'll enjoy your stay.

    As to your question of resources regarding perl and CGI programming, my first suggestion would be the Tutorials section. There are a number of very good resources listed there, both local and off-site. I would highly recommend them (as well as the others there, when you need or want to broach those subjects as well). Another resource you may want to look at is the documentation for the CGI.pm and other modules you may be using. Many of these modules include excellent documentation (accessible through the perldoc command) and examples.

    I hope some of these resources prove helpful, and again, welcome aboard. Hope you find your stay most enjoyable and educational.

Re: Resources for learning Perl with CGI?
by vek (Prior) on Sep 17, 2003 at 03:02 UTC
Re: Resources for learning Perl with CGI?
by InfiniteSilence (Curate) on Sep 17, 2003 at 00:17 UTC
    Well, first off your code reads from STDIN like a c program. CGI is easier to use. Here's your code:
    read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'}); $rating = substr($buffer,0,1); $table = substr($buffer,1,(length($buffer)+1));
    Here's mine that does the same thing:
    use CGI qw/:standard/; my ($rating, $table); if (param()){ $rating = param('rating'); $table = param('table'); }
    Try reading the CGI documentation by using perldoc CGI

    Especially read the section on debugging on the command line:

    perl -e "use CGI qw/:standard :debug/; if(param()){print param('you') +};" you=hello hello
    And before you are done you should read about tainted data. Your program takes information from the user and turns around and tries to write to a file. That is dangerous:
    perl -Te "use CGI qw/:standard :debug/; $ms=param('you'); if (-f qq($m +s\.txt)){open(H,qq(>>$ms\.txt))};print <H>; close(H);" you=hello Insecure dependency in open while running with -T switch at -e line 1.

    Celebrate Intellectual Diversity

Re: Resources for learning Perl with CGI?
by Roger (Parson) on Sep 17, 2003 at 00:18 UTC
    read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
    Perhaps you could use the CGI module and do something like:
    use CGI; my $cgi = new CGI; my $var1 = $cgi->param('FORM_VAR_1');
    Where FORM_VAR_1 is a variable on the HTML form, and it is part of the CGI get - for example http://somesite.com/cgi/script.cgi?FORM_VAR_1=xxxx
    $temp = "$table" . "ips" . ".txt"; $temp1 = "$table" . "results" . ".txt";
    This can be rewritten as follows to save keystrokes... ;-)
    $temp = "${table}ips.txt"; $temp1 = "${table}results.txt";
    And then in the following code:
    open(TABLEIP, "<$temp"); while(<TABLEIP>) { $thisline = $_; chomp($thisline); if($thisline == $ipaddress) { $IPcheck = 1; {last} } else { $IPcheck = 0; } } close(TABLEIP);
    Try to use the perl module IO::File to read from a file, and the this line variable is not necessary, the string test with == is wrong, the correct syntax is eq...
    use IO::File; .... $IPcheck = 0; { my $file = new IO::File "$temp", "r"; while (<$file>) { chomp; # operates on the $_ variable directly if ($_ eq $ipaddress) { $IPcheck = 1; last; } } } # note that you do not have to close the $file handle, falling # out of the scope and perl will automatically close the file.
    And later in your code ...
    while(<TABLERESULTS>) { $thisline = $_; $counter++; if($counter == 1) { $scoretotal = chomp($thisline); } if($counter == 2) { $votetotal = chomp($thisline); } if($counter > 2) { {last} } }
    This could be written as follows...
    while(<TABLERESULTS>) { $counter++; chomp; $counter == 1 ? {$scoretotal = $_} : $counter == 2 ? {$votetotal = $_} : {last} }
    And so on...

    The main failing in the original program would be comparing two strings with the == operator.

    Looks like you still have some readings to do in perl, why not try out the tutorials on PerlMonks, and also at Perl Open Library
Re: Resources for learning Perl with CGI?
by sauoq (Abbot) on Sep 16, 2003 at 23:42 UTC
    But I'm new to programming perl and tried to make a cgi program that doesn't work.

    Oh, that should be easy. Try making one that does...

    Sorry. Couldn't resist. :-)

    -sauoq
    "My two cents aren't worth a dime.";
    
      Aww, I was going to say that.