Why am I getting a blank page from my Perl CGI script?

The user enters the product code, price and name using the form. The script then adds it to the database or removes it from the database. If a user tries to delete a product that is not in the database, they receive an error message. If added or removed successfully, they also receive a message. However, when I check this, I just get a blank page. Perl does not contain any warnings, syntax errors, or anything else; says everything is fine, but I still just get a blank page.

script:

    #!/usr/bin/perl
#c09ex5.cgi - saves data to and removes data from a database
print "Content-type: text/html\n\n";
use CGI qw(:standard);
use SDBM_File;
use Fcntl;
use strict;

#declare variables
my ($code, $name, $price, $button, $codes, $names, $prices);


#assign values to variables
$code = param('Code');
$name = param('Name');
$price = param('Price');
$button = param('Button');

($code, $name, $price) = format_input();
($codes, $names, $prices) = ($code, $name, $price);

if ($button eq "Save") {
      add();
}
elsif ($button eq "Delete") {
      remove();
}
exit;


sub format_input {
         $codes =~ s/^ +//;
         $codes =~ s/ +$//;
         $codes =~ tr/a-z/A-Z/;
         $codes =~ tr/ //d;
         $names =~ s/^ +//;
         $names =~ s/ +$//;
         $names =~ tr/ //d;
         $names = uc($names);
         $prices =~ s/^ +//;
         $prices =~ s/ +$//;    
         $prices =~ tr/ //d;
         $prices =~ tr/$//d;
    }


sub add {
   #declare variable
   my %candles;

 #open database, format and add record, close database
     tie(%candles, "SDBM_File", "candlelist", O_CREAT|O_RDWR, 0666)
          or die "Error opening candlelist. $!, stopped";

     format_vars();
     $candles{$codes} = "$names,$prices";
     untie(%candles);

 #create web page      
      print "<HTML>\n";
      print "<HEAD><TITLE>Candles Unlimited</TITLE></HEAD>\n";
      print "<BODY>\n";
      print "<FONT SIZE=4>Thank you, the following product has been added.<BR>\n";
      print "Candle: $codes $names $prices</FONT>\n";
      print "</BODY></HTML>\n";
      } #end add


sub remove {
   #declare variables
   my (%candles, $msg);

 tie(%candles, "SDBM_File", "candlelist", O_RDWR, 0)
          or die "Error opening candlelist. $!, stopped";

     format_vars();

     #determine if the product is listed
     if (exists($candles{$codes})) {
          delete($candles{$codes});
          $msg = "The candle $codes $names $prices has been removed.";
        }
     else {
     $msg = "The product you entered is not in the database";
     }
     #close database
     untie(%candles);

#create web page
print "<HTML>\n";
print "<HEAD><TITLE>Candles Unlimited</TITLE></HEAD>\n";
print "<BODY>\n";
print "<H1>Candles Unlimited</H1>\n";
print "$msg\n";
print "</BODY></HTML>\n";
}

      

+2


a source to share


3 answers


Run at the command line with:

perl something.cgi Button=Save

      

... gives me an error:



Undefined subroutine &main::format_vars called at something.pl line 55.

      

If I change both format_vars () references to "format_input ()" I get what I think is the correct output.

+1


a source


You don't print any output other than the Content-Type header if add

or is called remove

. The problem is that you forgot to display a form (presumably containing buttons) if the button is not clicked.

Edit: Copying your posted code and doing a little cleanup, then calling it at the URL http: //localhost/~me/foo.cgi? Code = 1; Name = 2; Price = 3; Button = Save or http: // localhost / ~ me / foo.cgi? Code = 1; Name = 2; Price = 3; Button = Delete , I am getting correct HTML output. A cleaned up version of the code is used for this:

#!/usr/bin/perl

use strict;
use warnings;

print "Content-type: text/html\n\n";
use CGI qw(:standard);
use SDBM_File;
use Fcntl;
use strict;

#declare variables
my ($code, $name, $price, $button, $codes, $names, $prices);


#assign values to variables
$code = param('Code');
$name = param('Name');
$price = param('Price');
$button = param('Button');

($code, $name, $price) = format_input();
($codes, $names, $prices) = ($code, $name, $price);

if ($button eq "Save") {
  add();
}
elsif ($button eq "Delete") {
  remove();
}
exit;


sub format_input {
  $codes =~ s/^ +//;
  $codes =~ s/ +$//;
  $codes =~ tr/a-z/A-Z/;
  $codes =~ tr/ //d;
  $names =~ s/^ +//;
  $names =~ s/ +$//;
  $names =~ tr/ //d;
  $names = uc($names);
  $prices =~ s/^ +//;
  $prices =~ s/ +$//;
  $prices =~ tr/ //d;
  $prices =~ tr/$//d;
  }

sub add {
#   #declare variable
#   my %candles;
#
# #open database, format and add record, close database
#     tie(%candles, "SDBM_File", "candlelist", O_CREAT|O_RDWR, 0666)
#          or die "Error opening candlelist. $!, stopped";
#
#     format_vars();
#     $candles{$codes} = "$names,$prices";
#     untie(%candles);

 #create web page      
 print "<HTML>\n";
 print "<HEAD><TITLE>Candles Unlimited</TITLE></HEAD>\n";
 print "<BODY>\n";
 print "<FONT SIZE=4>Thank you, the following product has been added.<BR>\n";
 print "Candle: $codes $names $prices</FONT>\n";
 print "</BODY></HTML>\n";
 } #end add


sub remove {
#   #declare variables
#   my (%candles, $msg);
#
# tie(%candles, "SDBM_File", "candlelist", O_RDWR, 0)
#          or die "Error opening candlelist. $!, stopped";
#
#     format_vars();
#
#     #determine if the product is listed
#     if (exists($candles{$codes})) {
#          delete($candles{$codes});
#          $msg = "The candle $codes $names $prices has been removed.";
#        }
#     else {
#     $msg = "The product you entered is not in the database";
#     }
#     #close database
#     untie(%candles);

  #create web page
  print "<HTML>\n";
  print "<HEAD><TITLE>Candles Unlimited</TITLE></HEAD>\n";
  print "<BODY>\n";
  print "<H1>Candles Unlimited</H1>\n";
#  print "$msg\n";
  print "<p>Called remove</p>";
  print "</BODY></HTML>\n";
  }

      

Note that when enabled, warnings

this prints a lot of "uninitialized value" warnings because you get $code

vs $codes

, $name

vs $names

and $price

vs $prices

tangled up with each other in bad relationships. (Hint: you assign ($code, $name, $price) = format_input();

but format_input

don't return three values.)



I suspect that, as suggested in a previous comment, you are having case sensitivity / still issues again. My first attempt at checking this failed because I used "button = Save" instead of "Button = Save" in the URL. The names of the HTTP request parameters are generally lowercase for a good reason, as it avoids this type of problem.

Other casual comments:

  • You can declare your variables at the same time as assigning them, for example my $code = param('Code');

    . This is generally considered best / preferred practice, as declaring your declaration as close as possible helps to minimize the scope of variables.

  • In format_input

    it is redundant for s/^ +//; s/ +$//;

    both tr/ //d;

    , as it tr

    will also remove leading and trailing spaces.

  • When getting the values ​​of your parameters, you must either provide default values ​​if the parameter is empty / missing, or check for missing / missing and show the error to the user.

  • You should also have a final sentence else

    after elsif ($button eq "Delete")

    to display an error if $button

    missing or invalid. Yes, I know this script is meant to be called from a specific form, so it should "always" be valid $button

    , but it's trivial to traverse the form and pass any set of values ​​(valid or not) to the script, so you still need to check and validate everything on the server side, because you don't know where it will actually come from, or if the client has been validated correctly.

+1


a source


This is how I ran the script and it gave correct results. Make sure that wherever you host your site, it has the appropriate PERL modules installed.

Note. The hosting service I am using (BlueHost) requires me to call my Perl modules via #! / Usr / bin / perlml

   #!/usr/bin/perlml

   use strict;
   use warnings;

   print "Content-type: text/html\n\n";
   use CGI qw(:standard);
   use SDBM_File;
   use Fcntl;
   use strict;

   #declare variables
   my ($code, $name, $price, $button, $codes, $names, $prices);


   #assign values to variables
   $code = param('Code');
   $name = param('Name');
   $price = param('Price');
   $button = param('Button');

   ($codes, $names, $prices) = format_input();
   ($codes, $names, $prices) = ($code, $name, $price);

   if ($button eq "Save") {
   add();
   }
   elsif ($button eq "Delete") {
   remove();
   }
   exit;


   sub format_input {
   $codes =~ s/^ +//;
   $codes =~ s/ +$//;
   $codes =~ tr/a-z/A-Z/;
   $codes =~ tr/ //d;
   $names =~ s/^ +//;
   $names =~ s/ +$//;
   $names =~ tr/ //d;
   $names = uc($names);
   $prices =~ s/^ +//;
   $prices =~ s/ +$//;
   $prices =~ tr/ //d;
   $prices =~ tr/$//d;
   }

   sub add {
   #declare variable
   my %candles;

   #open database, format and add record, close database
   tie(%candles, "SDBM_File", "candlelist", O_CREAT|O_RDWR, 0666)
   or die "Error opening candlelist. $!, stopped";

   format_input();
   $candles{$code} = "$name,$price";
   untie(%candles);

   #create web page      
   print "<HTML>\n";
   print "<HEAD><TITLE>Candles Unlimited</TITLE></HEAD>\n";
   print "<BODY>\n";
   print "<FONT SIZE=4>Thank you, the following product has been added.<BR>\n";
   print "Candle: $codes, $names, $prices</FONT>\n";
   print "</BODY></HTML>\n";
   } #end add

   sub remove {
   #declare variables
   my (%candles, $msg);

   tie(%candles, "SDBM_File", "candlelist", O_RDWR, 0)
   or die "Error opening candlelist. $!, stopped";
   format_input();

   #determine if the product is listed
   if (exists($candles{$code})) {
   delete($candles{$code});
   $msg = "The candle $code, $name, $price has been removed.";
   }
   else {
   $msg = "The product you entered is not in the database";
   }
   #close database
   untie(%candles);

   #create web page
   print "<HTML>\n";
   print "<HEAD><TITLE>Candles Unlimited</TITLE></HEAD>\n";
   print "<BODY>\n";
   print "<H1>Candles Unlimited</H1>\n";
   print "$msg\n";
   print "</BODY></HTML>\n";
   }

      

0


a source







All Articles