user7880564
user7880564

Reputation:

How to debug a Perl script that displays error message even when it shouldn't?

I'm a student and very new to Perl and could use some help on why this isn't executing properly. Even when the criteria entered in is proper it will display all the error messages. I wanted it to display the errors that apply if need be or to display the thank you message if it all fields meet the proper criteria.

#!/usr/bin/perl
print "Content-type: text/html\n\n";
use CGI qw(:standard);

$rn = param("Reservation Name");
$rc = param("Reservation Create Date");
$rr = param("Reservation Room Type");
$rnd = param("Reservation Number of Days");
$rs = param("Reservation Start Date");
$re = param("Reservation Room Cost");
$rp = param("Reservation Payment Method");
$rd = param("Reservation Discount Code");
$e1 = 0;
$e2 = 0;
$e3 = 0;
$e4 = 0;
$e5 = 0;
$e6 = 0;
$e7 = 0;
$e8 = 0;
$errors = 0;
if ( $rn eq "")
{
$e1 = 1;
$errors =1;
}
if ( $rc eq "")
{
$e2 = 1;
$errors = 1;
}
if ($rr ne "s" && $rr ne "d" && $rr ne "k" && $rr ne "m")
{
$e3 = 1;
$errors = 1;
}
if ( $rnd eq 0 || ($rnd gt 7 ))
{
$e4 = 1;
$errors = 1;
}
if ( $rs eq "")
{
$e5 = 1;
$errors = 1;
}
if ( $re le 65.00 || ($re gt 490.00))
{
$e6 = 1;
$errors = 1;
}
if ( $rp le 1 || ($rp gt 4 ))
{
$e7 = 1;
$errors = 1;
}
if ($rd le 5 || $rd gt 99)
{
$e8 = 1;
$errors = 1;
}
if ( $errors == 0 )
{
print "<html><body>Thank you for making your reservation</body></html>";
}
else
{
if ($e1 == 1)
{
print "<br>Reservation Number May Not Be Blank<br>";
}
if ($e2 == 1)
{
print "<br>Reservation Create Date may not be blank<br>";
}
if ($e3 == 1)
{
print "<br>The Room Type Must be leters S,D,K,or M<br>";
}
if  ($e4 == 1)
{
print "<br>The number of days must be greate than zero and less than 7<br>";
}
if ($e5 == 1)
{
print "<br>The Start Date Must Not be Blank<br>";
}
if ($e6 == 1)
{
print "<br>The Room cost must be between $65.00 and $490.00<br>";
}
if ($e7 == 1)
{
print "<br>The payment method must be 1 thru 4<br>";
}
if ($e8 == 1)
{
print "<br>The discount code must be two digits and be on of the following 
numbers 5,10,17,20<br>";
}
}

Upvotes: 0

Views: 350

Answers (1)

Dave Cross
Dave Cross

Reputation: 69264

It works in my tests. The only thing I can think of is that the names of your parameters aren't what you say they are. You haven't shown us your HTML form, so I can't know for sure.

Your code is rather strange. You're using string comparison operators (gt, le) to compare numbers. And your error messages don't seem to match the comparisons you are making.

In addition, your error checking technique is rather clunky. The version below uses fewer variables and might be more maintainable.

#!/usr/bin/perl

use strict;
use warnings;

use CGI qw(:standard);

print header;

my $rn = param("Reservation Name");
my $rc = param("Reservation Create Date");
my $rr = param("Reservation Room Type");
my $rnd = param("Reservation Number of Days");
my $rs = param("Reservation Start Date");
my $re = param("Reservation Room Cost");
my $rp = param("Reservation Payment Method");
my $rd = param("Reservation Discount Code");

my @errors;

if ( $rn eq "") {
  push @errors, 'Reservation Number May Not Be Blank';
}

if ( $rc eq "") {
  push @errors, 'Reservation Create Date may not be blank';
}

my %valid_rr = map { $_ => 1 } qw[s d k m];

if ( !$valid_rr{$rr} ) {
  push @errors, 'The Room Type Must be leters S,D,K,or M';
}

if ( $rnd == 0 || ($rnd > 7 )) {
  push @errors, 'The number of days must be greate than zero and less than 7';
}

if ( $rs eq "") {
  push @errors, 'The Start Date Must Not be Blank';
}

if ( $re <= 65.00 || ($re > 490.00)) {
  push @errors, 'The Room cost must be between $65.00 and $490.00';
}

if ( $rp <= 1 || ($rp > 4 )) {
  push @errors, 'The payment method must be 1 thru 4';
}

if ($rd <= 5 || $rd > 99) {
  push @errors, 'The discount code must be two digits and be on of the following 
  numbers 5,10,17,20';
}

if ( @errors ) {
  print map { "<br>$_<br>" } @errors;
} else {
  print "<html><body>Thank you for making your reservation</body></html>";
}

You should also revisit your checks for valid strings. With use warnings turned on, you'll get lots of "use of uninitialised value" errors.

Upvotes: 2

Related Questions