0

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>";
}
}
4
  • 6
    Welcome to Stack Overflow and the Perl tag. Please always use strict; and use warnings; in all your programs. They help you to spot syntax errors and other problems early on. You should also indent your code properly. It currently is pretty hard to read. You can edit your question and update the code. Also include the error messages that you are seeing, so we know what the problem is. You might also want to tell us what input you are using, as that is not obvious from the code, as well as an explanation of what your code is supposed to do. Also see minimal reproducible example. Commented Apr 17, 2017 at 20:33
  • Well the perl is supposed to error check the input of an html form so that Reservation Number May Not Be Blank Reservation Create Date may not be blank The Room Type Must be leters S,D,K,or M The Start Date Must Not be Blank The Room cost must be between .00 and .00 The payment method must be 1 thru 4 The discount code must be two digits and be one of the following numbers 5,10,17,20 display message pertaining to which field has an error.it's displaying all the error messages even if there are none Commented Apr 17, 2017 at 20:42
  • 1
    Don't use string comparison operators like le or gt with numbers. Commented Apr 17, 2017 at 20:53
  • 3
    Is this code straight from 1994? Commented Apr 17, 2017 at 20:54

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.