What Is The Issue With This Cgi/html/perl Program?
Solution 1:
Several issues here, but the crux of the matter is this code.
@animals = (<IN>);
$item = param;
foreach $line (<IN>) {
if ($line =~ /$item/) {
print"$item";
}
}
Let's look at each line in turn:
@animals = (<IN>);
This reads all of the data from IN
into the array @animals
. It also leaves IN
's file pointer at the end of the file. Any further attempts to read data from IN
will fail.
$item = param;
If you call param
with no arguments, you get a list of the parameter names that were found in the CGI request. As you're assigning this list to a scalar value, this behaviour changes and you'll get the number of parameters. In your system this will always be 1. So $item
contains the value 1.
foreach $line (<IN>) {
Remember how you read all of the data from IN
a couple of lines back? Well you're trying to read more data from it here. And that's not going to work. I think you probably wanted @animals
here, not <IN>
. Currently your foreach
is never executed as on the first iteration the call to <IN>
returns undef
- which is false.
if ($line =~ /$item/) {
Let's assume that you've replaced <IN>
with @animals
in your foreach
loop - so that the loop body is actually executed. This still isn't doing what you wanted. Remember that $item
contains 1 rather than the name of an animal to search for. And I doubt that you have an animal called "1".
What you probably want is something more like this:
my $animal = param('Search');
while (<IN>) {
printif /$animal/;
}
I'd also point out that learning CGI in 2014 is pretty ridiculous. You would be far better off looking at a simple Perl web framework like Web::Simple or Dancer.
Solution 2:
I think your problem is
$item = param;
Which is putting the number of parameters in your form, in this case 2, into $item. I doubt you have an animal named 2
Change it to
$item = param('Search');
Post a Comment for "What Is The Issue With This Cgi/html/perl Program?"