Perl的简单表单应用程序

时间:2017-05-01 01:17:04

标签: 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">
</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>

这是我的perl脚本。

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

use strict;
use warnings;

use CGI;

local ($buffer, @pairs, $pair, $name, $value, %FORM);
# Read in text
$ENV{'REQUEST_METHOD'} =~ tr/a-z/A-Z/;

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

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

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

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

print CGI::header();

print $name." ".$surname." ".$age." ".$gender;

我有这样的错误。 &#34;标题之前的脚本输出结束:signup.cgi&#34;。 我该如何解决这个问题?

2 个答案:

答案 0 :(得分:6)

您的第一个错误是尝试从TutorialsPoint网站学习Perl。他们真的不知道他们在说什么。请尝试Perl tutorials hub,以获得更高质量的Perl教程。

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

$ 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来声明变量,而不是locallocal在很大程度上是Perl 4的遗留问题。自从Perl 5在二十多年前发布以来,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

这里最重要的一点是,如果你正在学习一门新语言,你就不应该相信互联网上的随机教程网站。它们很少使用。询问知道语言的人在哪里可以找到好的资源。

答案 1 :(得分:2)

您尚未声明$ age,$ gender,$ surname变量,因此脚本即将死亡。

尝试通过'perl -w myscript.pl'手动验证脚本,并在@weggami建议的网站服务器错误日志中查找。

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

我通常会用'my'声明事物,并使声明更接近使用,因此变量的范围受到限制:

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

my $surname = $FORM{surname};