这个CGI / HTML / Perl程序有什么问题? [英] What is the issue with this CGI/HTML/Perl program?

查看:66
本文介绍了这个CGI / HTML / Perl程序有什么问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用XAMMP Apache服务器。每行都有一个名为 animal.txt的文本文件,其中包含动物的名称,并带有基本描述。

I am using XAMMP Apache Server. There is a text file named 'animal.txt' containing the name of an animal on each line, with a basic description.

我正在尝试使用HTML和CGI编写Perl程序。该程序的目的是让用户使用HTML表单搜索动物名称。然后,它将链接到Perl程序,该程序循环遍历animal.txt文件并逐行读取文件。该程序将采用与用户从原始HTML表单中搜索到的任何行匹配的行,并打印所有包含相同名称的动物。

I am trying to write a Perl program, using HTML and CGI. The aim of the program is to have the user search an animal name, using a HTML form. This will then link to a Perl program which loops through the animal.txt file and reads the file line by line. The program will take any line that matches the users search from the original HTML form, and print all animals containing the same name.

到目前为止,这就是我所在的地方:

So far this is where I am :

(客户端表单)

#!\xampp\perl\bin\perl.exe
use CGI qw/:standard/; # load standard CGI routines
use CGI::Carp('fatalsToBrowser'); 

print header(); # create the HTTP header
print  <<HTML
<head>
  <title>Shop Here</title>
 </head>
 <body>

<h1>Animal list search</h1>

     A basic form  <br />

  <form action="dumpsVar2.pl"> 
    Search:     <input type="text", name="Search" size=5><br><br>
    Submit:     <input type="submit" name="select" size="7"><br>
    </form> 
  </body>
 </html>
 HTML

然后执行perl程序:(服务器端)
# !xampp\perl\bin\perl.exe

And then the perl program: (Server-Side) #!\xampp\perl\bin\perl.exe

use CGI qw(:standard);
use CGI::Carp('fatalsToBrowser'); 
$query = new CGI;

@parameters = $query -> param;
print header, start_html("Parameters");
print "$0 was passed these parameters:<br>  <br> ";

foreach $name (@parameters) {
    $value = $query -> param($name);
    print p("$name = $value"); 
} 
$inFile = "animal.txt";
open (IN, $inFile) or die "Can't find file: $inFile";

@animals = (<IN>);
$item = param;
foreach $line (<IN>) {
    if ($line =~ /$item/) {
        print "$item";
    }
}

print end_html;


推荐答案

这里有几个问题,但问题的关键是

Several issues here, but the crux of the matter is this code.

@animals = (<IN>);
$item = param;
foreach $line (<IN>) {
    if ($line =~ /$item/) {
        print "$item";
    }
}

让我们依次查看每行:

@animals = (<IN>);

这将从 IN 中读取所有数据放入数组 @animals 。它还会将 IN 的文件指针留在文件末尾。从 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;

如果不带参数调用 param ,您将获得在CGI请求中找到的参数名称的列表。当您将此列表分配为标量值时,此行为会更改,并且您将获得参数的 number 。在您的系统中,该值始终为1。因此 $ item 包含值1。

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>) {

记住您如何阅读所有来自 IN 的数据几行了吗?好吧,您正在尝试从此处读取更多数据。那是行不通的。我想您可能在这里想要 @animals ,而不是< IN> 。当前,您的 foreach 从未执行过,因为在第一次迭代中,对< IN> 的调用将返回 undef -这是错误的。

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/) {

假设您已替换< IN> foreach 循环中带有 @animals 的循环-以便实际执行循环主体。这仍然不能满足您的要求。请记住, $ item 包含1而不是要搜索的动物的名称。而且我怀疑您是否有一种动物叫做 1。

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>) {
    print if /$animal/;
}

我还要指出,在2014年学习CGI非常荒谬。如果您看一下像Web :: Simple或Dancer这样的简单Perl Web框架,那就更好了。

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.

这篇关于这个CGI / HTML / Perl程序有什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆