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

I am working on a rating script for my website and when I tested it just now it killed my cgi-bin scripts. I looked over the code and think I figured out what was going wrong but I want to post here and get some input before I break the server again. Orignal code:
#!/user/perl/ use CGI q~:standar~; use CGI::Carp qw(fatalsToBrowser); use strict; use DBI print "Content-type: text/html\n\n"; my ($dbh, $sth, $filename, @votes, $average, $i); $filename = $ENV{'QUERY_STRING'}; $dbh = DBI -> connect ('dbi:ODBC:', '', '') or die $DBI::errstr; $sth = $dbh -> prepare (qq~select One, Two, Three, Four, Five from Fan +Ratings where Title = ?~) or die $DBI::errstr; $sth -> execute ($filename) or die $DBI::errstr; @votes = $sth -> fetchrow_array; $average = ((1 * $votes[0]) + (2 * $votes[1]) + (3 * $votes[2]) + (4 * + $votes[3]) + (5 * $votes[4])) / 5; $i = 1; while ($i <= $average) { print qq~<img src="http://www.ffinfo.com/images/misc/starfull.gif" + width="15" height="15" alt="Full Star" style="vertical-align: bottom +" /> ~; $i++ } if ($average =~ /\d\.\d/) { print qq~<img src="http://www.ffinfo.com/images/misc/starhalf.gif" + width="15" height="15" alt="Half Full Star" style="vertical-align: b +ottom" />~; } while ($i <= 5) { print qq~<img src="http://www.ffinfo.com/images/misc/starempty.gif +" width="15" height"15" alt="Empty Star" style="vertical-align: botto +m" /> ~; $i++ } $dbh -> disconnect();
What i think will fix the problem:
#!/user/perl/ use CGI q~:standar~; use CGI::Carp qw(fatalsToBrowser); use strict; use DBI print "Content-type: text/html\n\n"; my ($dbh, $sth, $filename, @votes, $average, $i); $filename = $ENV{'QUERY_STRING'}; $dbh = DBI -> connect ('dbi:ODBC:FFmisc', 'Eaglef93', 'garnet0114') or + die $DBI::errstr; $sth = $dbh -> prepare (qq~select One, Two, Three, Four, Five from Fan +Ratings where Title = ?~) or die $DBI::errstr; $sth -> execute ($filename) or die $DBI::errstr; @votes = $sth -> fetchrow_array; if (defined $votes[0]) { $average = ((1 * $votes[0]) + (2 * $votes[1]) + (3 * $votes[2]) + +(4 * $votes[3]) + (5 * $votes[4])) / 5; $i = 0; while ($i <= $average) { print qq~<img src="http://www.ffinfo.com/images/misc/starfull. +gif" width="15" height="15" alt="Full Star" style="vertical-align: bo +ttom" /> ~; $i++ } if ($average =~ /\d\.\d/) { print qq~<img src="http://www.ffinfo.com/images/misc/starhalf. +gif" width="15" height="15" alt="Half Full Star" style="vertical-alig +n: bottom" />~; } while ($i <= 5) { print qq~<img src="http://www.ffinfo.com/images/misc/starempty +.gif" width="15" height"15" alt="Empty Star" style="vertical-align: b +ottom" /> ~; $i++ } } else { print qq~Be the first to rate this Fan Creation.~; } $dbh -> disconnect();
Any input on what might be breaking the server, or just a better way to do something in my script would be apreaciated (by both me and my hosting provide lol )

Replies are listed 'Best First'.
Re: Sciprt killing my server
by bigmacbear (Monk) on Jun 28, 2007 at 02:15 UTC

    I suspect you are getting compile-time errors, at a first glance there are some syntax errors that will prevent your code from compiling. The two most obvious errors I can see right off the bat are the misspelling of the :standard flag in the "use CGI qw~:standar~;" line and the missing semicolon after "use DBI".

    But don't take my word for it, take perl's word for it. I would start by attempting to run the script in your local Perl environment (if you are on a PC and don't have a local Perl, download it from ActiveState) and eliminating compilation errors first, before you even upload to your hosting provider. Since you're using CGI.pm, that will be a lot easier to debug locally.

    Two other things that will help you immensely are adding the -w and -T flags to your #! line, and adding use diagnostics; while you are syntax checking.

    Good luck!

Re: Sciprt killing my server
by snopal (Pilgrim) on Jun 28, 2007 at 02:32 UTC

    I'm guessing that you are writing this code and then uploading it to a remote site for rendering. This might explain your testing it from the cgi-bin rather than from a command line. This is less than ideal, because being able to test compile success is much easier when you get a direct response.

    Even when you test code through a web server, Perl very nicely reports what it doesn't like, and you can find these reports in your web server error log. For most typographic errors, this will give enough information to correct code compile failures.

    Once you are comfortable with the web development process, you can then use 'warn' alerts to determine execution process results and errors.

    Of course, being able to test your code locally first is best.