meh*_*tem -4 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>
Run Code Online (Sandbox Code Playgroud)
这是我的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;
Run Code Online (Sandbox Code Playgroud)
我有这样的错误."标题前面的脚本输出结束:signup.cgi".我该如何解决这个问题?
您的第一个错误是尝试从TutorialsPoint站点学习Perl.他们真的不知道他们在说什么.尝试使用Perl教程中心,以获得更高质量的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.
Run Code Online (Sandbox Code Playgroud)
您可以看到所有错误都是相同的.你忘了申报你的一些变数.您的代码应如下所示:
#!"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;
Run Code Online (Sandbox Code Playgroud)
请注意,我曾经用于my声明变量,而不是local.localPerl 5是二十年前发布的,my因此它是Perl程序中声明大多数变量的最佳方式.另请注意,我声明变量尽可能接近它们的使用位置.
我们可以在这里改变一些其他的东西.
paramCGI.pm中的子例程来替换你的错误的CGI参数解析器.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";
Run Code Online (Sandbox Code Playgroud)
这看起来不简单吗?
您可以从命令行测试它:
$ perl testcgi2 'name=foo&surname=bar&age=18&sexual=M'
Content-Type: text/plain; charset=ISO-8859-1
foo bar 18 M
Run Code Online (Sandbox Code Playgroud)
这里最重要的一点是,如果你正在学习一门新语言,你就不应该相信互联网上的随机教程网站.它们很少使用.询问知道语言的人在哪里可以找到好的资源.
| 归档时间: |
|
| 查看次数: |
557 次 |
| 最近记录: |