Forms 一个简单的Perl表单应用程序

Forms 一个简单的Perl表单应用程序,forms,perl,web-applications,Forms,Perl,Web Applications,我在用Perl开发简单的注册表单应用程序时出错 这是我的html <html> <head> <meta charset="UTF-8"> <title>Kayıt Formu</title> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> <

我在用Perl开发简单的注册表单应用程序时出错

这是我的html

<html>
<head>
    <meta charset="UTF-8">
    <title>Kayıt Formu</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
</head>
<body>
    <h2>Kayıt Formu</h2>
    <form id="signup-form" action="/sample_perl_application/signup.cgi" method="GET">
        <div class="form-group">
            <label for="name">Ad</label>
            <input class="form-control" id="name" name="name" type="text"/>
        </div>
        <div class="form-group">
            <label for="surname">Soyad</label>
            <input class="form-control" id="surname" name="surname" type="text"/>
        </div>
        <div class="form-group">
            <label for="age">Yaş</label>
            <input class="form-control" id="age" name="age" type="text"/>
        </div>
        <div class="form-group">
            <label for="sexual">Cinsiyet</label>
            <select class="form-control" id="sexual" name="sexual">
                <option id="male">Bay</option>
                <option id="female">Bayan</option>
            </select>
        </div>
        <input class="btn btn-default" id="save" name="save" type="submit" value="Kaydet"/>
    </form>
</body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</html>
我有一个这样的错误。“头之前的脚本输出结束:signup.cgi”。
如何解决此问题?

您尚未声明$age、$gender、$姓氏变量,因此脚本正在消亡

尝试通过“perl-wmyscript.pl”手动验证脚本,并按照@ikegami的建议查看Web服务器错误日志

脚本中变量的声明与“现代Perl”的用法略有不同

我通常使用“my”来声明内容,并使声明更接近用法,从而限制变量的范围:

my @pairs = split(/&/, $buffer);
foreach my $pair (@pairs) { 
    my($name, $value) = split(/=/, $pair);

my $surname = $FORM{surname};

Etc

您的第一个错误是尝试从TutorialsPoint站点学习Perl。他们真的不知道自己在说什么。相反,请尝试查找指向更高质量Perl教程的指针

尽管CGI程序设计为在web服务器上运行,但从命令行运行它们以调试它们通常是有用的。特别是,在跟踪语法错误时,可以使用
perl-c
查看所有问题。我将您的代码放在一个名为“testcgi”的文件中,并运行命令
perl-ctestcgi
。我得到了以下输出:

$ perl -c testcgi 
Global symbol "$buffer" requires explicit package name (did you forget to declare "my $buffer"?) at testcgi line 8.
Global symbol "@pairs" requires explicit package name (did you forget to declare "my @pairs"?) at testcgi line 8.
Global symbol "$pair" requires explicit package name (did you forget to declare "my $pair"?) at testcgi line 8.
Global symbol "$name" requires explicit package name (did you forget to declare "my $name"?) at testcgi line 8.
Global symbol "$value" requires explicit package name (did you forget to declare "my $value"?) at testcgi line 8.
Global symbol "%FORM" requires explicit package name (did you forget to declare "my %FORM"?) at testcgi line 8.
Global symbol "$buffer" requires explicit package name (did you forget to declare "my $buffer"?) at testcgi line 13.
Global symbol "@pairs" requires explicit package name (did you forget to declare "my @pairs"?) at testcgi line 17.
Global symbol "$buffer" requires explicit package name (did you forget to declare "my $buffer"?) at testcgi line 17.
Global symbol "$pair" requires explicit package name (did you forget to declare "my $pair"?) at testcgi line 19.
Global symbol "@pairs" requires explicit package name (did you forget to declare "my @pairs"?) at testcgi line 19.
Global symbol "$name" requires explicit package name (did you forget to declare "my $name"?) at testcgi line 20.
Global symbol "$value" requires explicit package name (did you forget to declare "my $value"?) at testcgi line 20.
Global symbol "$pair" requires explicit package name (did you forget to declare "my $pair"?) at testcgi line 20.
Global symbol "$value" requires explicit package name (did you forget to declare "my $value"?) at testcgi line 21.
Global symbol "$value" requires explicit package name (did you forget to declare "my $value"?) at testcgi line 22.
Global symbol "%FORM" requires explicit package name (did you forget to declare "my %FORM"?) at testcgi line 23.
Global symbol "$name" requires explicit package name (did you forget to declare "my $name"?) at testcgi line 23.
Global symbol "$value" requires explicit package name (did you forget to declare "my $value"?) at testcgi line 23.
Global symbol "$name" requires explicit package name (did you forget to declare "my $name"?) at testcgi line 26.
Global symbol "%FORM" requires explicit package name (did you forget to declare "my %FORM"?) at testcgi line 26.
Global symbol "$surname" requires explicit package name (did you forget to declare "my $surname"?) at testcgi line 27.
Global symbol "%FORM" requires explicit package name (did you forget to declare "my %FORM"?) at testcgi line 27.
Global symbol "$age" requires explicit package name (did you forget to declare "my $age"?) at testcgi line 28.
Global symbol "%FORM" requires explicit package name (did you forget to declare "my %FORM"?) at testcgi line 28.
Global symbol "$gender" requires explicit package name (did you forget to declare "my $gender"?) at testcgi line 29.
Global symbol "%FORM" requires explicit package name (did you forget to declare "my %FORM"?) at testcgi line 29.
Global symbol "$name" requires explicit package name (did you forget to declare "my $name"?) at testcgi line 33.
Global symbol "$surname" requires explicit package name (did you forget to declare "my $surname"?) at testcgi line 33.
Global symbol "$age" requires explicit package name (did you forget to declare "my $age"?) at testcgi line 33.
Global symbol "$gender" requires explicit package name (did you forget to declare "my $gender"?) at testcgi line 33.
testcgi had compilation errors.
您可以看到,您的所有错误都是相同的。您忘记了声明一些变量。您的代码应该如下所示:

#!"c:\xampp\perl\bin\perl.exe"

use strict;
use warnings;

use CGI;

# Read in text
$ENV{'REQUEST_METHOD'} =~ tr/a-z/A-Z/;

my $buffer;

if ($ENV{'REQUEST_METHOD'} eq "GET") {
   $buffer = $ENV{'QUERY_STRING'};
}

# Split information into name/value pairs
my @pairs = split(/&/, $buffer);

my %FORM;
foreach my $pair (@pairs) {
   my ($name, $value) = split(/=/, $pair);
   $value =~ tr/+/ /;
   $value =~ s/%(..)/pack("C", hex($1))/eg;
   $FORM{$name} = $value;
}

my $name = $FORM{name};
my $surname = $FORM{surname};
my $age = $FORM{age};
my $gender = $FORM{sexual};

print CGI::header();

print $name." ".$surname." ".$age." ".$gender;
请注意,我使用了
my
来声明变量,而不是
local
<代码>本地很大程度上是Perl4的遗留问题。自从Perl 5在20多年前发布以来,
my
一直是在Perl程序中声明大多数变量的最佳方式。还要注意,我声明的变量尽可能接近它们的使用位置

在这里,我们还可以改变其他一些事情

  • 如果我们从CGI.pm导入命名的子例程,我们可以通过省略包名使对它们的调用更干净一些
  • 我们可以使用CGI.pm中的
    param
    子例程来替换有缺陷的CGI参数解析器
  • 我们可以利用Perl变量在双引号字符串中展开的事实,使
    print
    语句更易于阅读
进行这些更改后,您的代码将简化为:

#!"c:\xampp\perl\bin\perl.exe"

use strict;
use warnings;

use CGI qw(param header);

my $name    = param('name');
my $surname = param('surname');
my $age     = param('age');
my $gender  = param('sexual');

# We're outputing plain text, not HTML
print header(-content_type => 'text/plain');

print "$name $surname $age $gender";
这看起来不简单吗

您可以从命令行对其进行测试:

$ perl testcgi2 'name=foo&surname=bar&age=18&sexual=M'
Content-Type: text/plain; charset=ISO-8859-1

foo bar 18 M

这里最大的教训是,如果你正在学习一门新语言,你不应该相信互联网上随机的教程网站。它们很少有任何用处。询问懂这门语言的人在哪里可以找到好的资源。

首先,你需要找出你犯了什么错误。Web服务器通常会将CGI脚本的stderr输出发送到它们的错误日志中。如果您使用的是CGI.pm,为什么要解析CGI请求?!?!使用
参数
!还要注意的是,您遇到了一个HTML注入错误。在HTML中插入文本字符串时,没有正确转义需要转义的字符。请使用更好的资源学习Perl。请参阅和,该站点提供了一个非常糟糕的Perl CGI脚本示例。您已经按照建议添加了“use strict”,因此您的脚本将终止,因为您尚未声明所有变量($SEXT,$LANSAME)。关于为什么“我的”比“本地的”更受欢迎,请参见以下答案()
$ perl testcgi2 'name=foo&surname=bar&age=18&sexual=M'
Content-Type: text/plain; charset=ISO-8859-1

foo bar 18 M