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

This script was supposed to be just a simple database script, something went terribly wrong. The line: use CGI::Carp qw(fatalsToBrowser); prints this error message:
syntax error at playerpost.cgi line 56, near ") {" syntax error at playerpost.cgi line 75, near "}" Execution of playerpost.cgi aborted due to compilation errors.
These line are: 1. if (($gender eq "") || ($age eq "") || ($county eq "") || ($bracket eq "") || ($position eq "") || ($name eq "") || ($email eq "") || ($experience eq "")) { and 2. elsif ($email =~ m/\w{1,}\@\w{1,}\.\w{1,}/) { I've gone over the script millions of times, but I can't find anything wrong! Please help! :-) This is the entire script:
#!/usr/bin/perl print "Content-type: text/html\n\n"; use CGI::Carp qw(fatalsToBrowser); read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'}); @pairs = split(/&/, $buffer); foreach $pair (@pairs) { ($key, $value) = split(/=/, $pair); $key =~ tr/+/ /; $key =~s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; $value =~ tr/+/ /; $value =~s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; $value =~s/<!--(.|\n)*-->//g; if ($formdata{$key}) { $formdata{$key} .= ", $value"; } else { $formdata{$key} = $value; } } $gender = $formdata{'gender'}; $age = $formdata{'age'}; $county = $formdata{'county'}; $bracket = $formdata{'bracket'}; $position = $formdata{'position'}; $name = $formdata{'name'}; $email = $formdata{'email'}; $experience = $formdata{'experience'}; ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time); $month = $mon+1; $year1 = $year+1900; $date = "$mday\/$month\/$year1" if (($gender eq "") || ($age eq "") || ($county eq "") || ($bracket eq + "") || ($position eq "") || ($name eq "") || ($email eq "") || ($exp +erience eq "")) { print <<"PrintTag"; <html><head><title>Insufficient information</title><style> A:hover {color: "#CB5C03"; text-decoration: underline; font-family: "P +egasus"} A:link {color: "black"; font-family: "Pegasus"; text-decoration: none} A:visited {color: "#CB5C03"; text-decoration: none; font-family: "Pega +sus"} P {font-family: "Pegasus"; color:black} H1 {font-family: "Pegasus"; color:black} H3 {font-family: "Pegasus"; color:black} H2 {font-family: "Pegasus"; color:black} </style></head><body bgcolor = "#08BD29" text="black"> <BASE HREF="http://soccermb.netfirms.com/"> <img src="sidebar21.gif" align="left" width="20" height="300"><img src +="sidebar21.gif" align="right" width="20" height="300"><center><h2>In +sufficient information</h2></center> <center><h3>Please click the 'Back' button on your browser to try agai +n</h3></center> PrintTag exit; } elsif ($email =~ m/\w{1,}\@\w{1,}\.\w{1,}/) { elsif ($name =~ m/\w{3}/) { open(DATA, ">>$ENV{'DOCUMENT_ROOT'}/cgi-bin/datap.txt") || &error; flock(DATA, 2); print DATA "$gender\+$age\+$county\+$bracket\+$position\+$name\+$date\ ++$experience\+$email\n"; flock(DATA, 8); close(DATA); } else { print <<"PrintTag"; <html><head><title>Incorrect information</title><style> A:hover {color: "#CB5C03"; text-decoration: underline; font-family: "P +egasus"} A:link {color: "black"; font-family: "Pegasus"; text-decoration: none} A:visited {color: "#CB5C03"; text-decoration: none; font-family: "Pega +sus"} P {font-family: "Pegasus"; color:black} H1 {font-family: "Pegasus"; color:black} H3 {font-family: "Pegasus"; color:black} H2 {font-family: "Pegasus"; color:black} </style></head><body bgcolor = "#08BD29" text="black"> <BASE HREF="http://soccermb.netfirms.com/"> <img src="sidebar21.gif" align="left" width="20" height="300"><img src +="sidebar21.gif" align="right" width="20" height="300"><center><h2>In +correct information</h2></center> <center><h3>You must type three initials. Please click the 'Back' but +ton on your browser to try again</h3></center> PrintTag exit; } } else { print <<"PrintTag"; <html><head><title>Incorrect information</title><style> A:hover {color: "#CB5C03"; text-decoration: underline; font-family: "P +egasus"} A:link {color: "black"; font-family: "Pegasus"; text-decoration: none} A:visited {color: "#CB5C03"; text-decoration: none; font-family: "Pega +sus"} P {font-family: "Pegasus"; color:black} H1 {font-family: "Pegasus"; color:black} H3 {font-family: "Pegasus"; color:black} H2 {font-family: "Pegasus"; color:black} </style></head><body bgcolor = "#08BD29" text="black"> <BASE HREF="http://soccermb.netfirms.com/"> <img src="sidebar21.gif" align="left" width="20" height="300"><img src +="sidebar21.gif" align="right" width="20" height="300"><center><h2>In +correct information</h2></center> <center><h3>You must type your email address in the form 'email@addres +s.com'.<br>Please click the 'Back' button on your browser to try agai +n</h3></center> PrintTag exit; } sub error { print "Content-type: text/html\n\n"; print "<center>Can't open file</center>"; exit; }

Replies are listed 'Best First'.
Re: Faulty Script
by gav^ (Curate) on Mar 30, 2002 at 02:47 UTC

    The main problem is that you haven't used CGI (documentation can be found online here). See ovid's CGI Course for more help.

    p.s. The answer to your question is that you missed a ';' off the previous line:

    $date = "$mday\/$month\/$year1"

    gav^

Re: Faulty Script
by cjf (Parson) on Mar 30, 2002 at 04:10 UTC

    Okay, just a few suggestions here :)

    • Turn on warnings by adding the -w flag and taint mode by adding the -T flag to your path to perl (#!/usr/bin/perl). You should do this with all CGI scripts, they'll help prevent you from doing stupid (and insecure) things.
    • You should also always use strict in all your scripts. It will save you tonnes of debugging time and make programming a lot easier for both you and whoever has to maintain your code.
    • Do not attempt to hand parse the parameters, it's a lot more difficult than you think and your solution is bound to be buggy and insecure. Use CGI.pm instead.
    • Read Essential CGI Security Practices before placing your script online.
    • Indent you code so we can read it! :)

    You may also wish to consider a templating system such as HTML::Template, It will make your code far more readable and maintainable. After you've done these things feel free to repost so we can whine about relatively minor things like use CGI::Carp 'fatalsToBrowser'; in production code :)

Re: Faulty Script
by aersoy (Scribe) on Mar 30, 2002 at 02:53 UTC

    Hello.

    The errors I found are these:

    Line 54: no semicolon at line end. Should be $date = "$mday\/$month\/$year1";

    Line 78: elsif without a prior if. Should be if ($name =~ m/\w{3}/) {

    I hope these help.

    --
    Alper Ersoy