本文介绍了一个简单的Perl应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这是我的html。

 < HTML> 
< head>
< meta charset =UTF-8>
< title>KayıtFormu< / title>
< link rel =stylesheethref =https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css>
< / head>
< body>
< h2>KayıtFormu< / h2>
< form id =signup-formaction =/ sample_perl_application / signup.cgimethod =GET>
< div class =form-group>
< label for =name>广告< / label>
< input class =form-controlid =namename =nametype =text/>
< / div>
< div class =form-group>
< label for =surname> Soyad< / label>
< input class =form-controlid =surnamename =surnametype =text/>
< / div>
< div class =form-group>
< label for =age>Yaş< / label>
< input class =form-controlid =agename =agetype =text/>
< / div>
< div class =form-group>
< label for =sexual> Cinsiyet< / label>
< option id =male> Bay< / option>
< option id =female> Bayan< / option>
< / select>
< / div>
< input class =btn btn-defaultid =savename =savetype =submitvalue =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;
使用警告;

使用CGI;

本地($ buffer,@pairs,$ pair,$ name,$ value,%FORM);
#在文本中读入
$ ENV {'REQUEST_METHOD'} =〜tr / a-z / A-Z /;

if($ ENV {'REQUEST_METHOD'} eqGET){
$ buffer = $ ENV {'QUERY_STRING'};
}

#将信息拆分为名称/值对
@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();

打印$ name。。$ surname。。$ age。。$ gender;

我有这样的错误。 在标题之前脚本输出结束:signup.cgi。
我该如何解决这个问题?第二个错误是尝试从TutorialsPoint网站学习Perl。他们真的不知道他们在说什么。尝试使用来获得更好质量的Perl教程。



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

$ $ p $ $ $ $ $ $ $ $ $ $ $全局符号$缓冲区需要显式包名称你在testcgi第8行忘了声明my $ buffer?)。
全局符号@pairs需要在testcgi第8行显式包名(你忘了声明my @pairs?)。
全局符号$ pair需要在testcgi第8行显示包名称(您是否忘记声明my $ pair?)。
全局符号$ name需要明确的包名称(您忘记了在testcgi第8行声明my $ name?)。
全局符号$ value需要在testcgi第8行显式包名(您忘记声明my $ value?)。
Global符号%FORM需要在testcgi第8行显式包名(你忘了声明my%FORM?)。
全局符号$ buffer需要显式包名(你忘记声明my $缓冲区?)在testcgi第13行。
全局符号@pairs需要显式包名(你忘记声明my @pai
全局符号$ buffer需要在testcgi第17行显式包名(您忘记声明my $ buffer?)。
全局符号$ pair 在testcgi第19行需要明确的包名(你忘记声明我的$对?)。
全局符号@pairs需要明确的包名(你忘记声明我的@pairs?)在testcgi第19行。
全局符号$ name需要在testcgi第20行显式包名(您忘记声明my $ name?)。
全局符号$ value需要显式包在testcgi第20行输入名称(你忘了声明my $ value?)。
全局符号$ pair需要显式包名(你忘记声明my $ pair了吗? 。
全局符号$ value需要显式包名称(你忘记声明my $ value?)在testcgi第21行。
全局符号$ value需要明确的包名称忘记声明我的$值?)在testcgi第22行。
全局符号%FORM需要在testcgi第23行显式包名(你忘了声明my%FORM?)。
全局符号$ name需要显式包名(你忘记声明我的$ name ?)在testcgi第23行。
全局符号$ value需要在testcgi第23行显式包名称(您是否忘记声明my $ value?)。
全局符号$ name在testcgi第26行需要明确的软件包名称(你忘记声明my $ name?)。
全局符号%FORM需要明确的软件包名称(你是否忘记声明my%FORM?) testcgi第26行。
全局符号$ surname需要在testcgi第27行显式包名(您忘记声明my $ surname?)。
全局符号%FORM需要显式包名(你忘了在testcgi第27行声明my%FORM?)。
全局符号$ age需要在testcgi第28行显式包名(你忘了声明my $ age?)。
全局符号%FORM需要显式包名称(你是否f或者在testcgi第28行声明my%FORM?)。
全局符号$ gender需要在testcgi第29行显示包名(你忘了声明my $ gender?)。
全局符号%FORM需要在testcgi第29行显示包名(你忘了声明my%FORM?)。
全局符号$ name需要显式包名(你忘记声明 my $ name?)在testcgi第33行。
全局符号$ surname需要在testcgi第33行显式包名(您忘记声明我的$姓氏?)。
全局符号 $ age需要在testcgi第33行显式包名(你忘了声明my $ age?)。
全局符号$ gender需要显式包名(你忘记声明我的$性别 ?)在testcgi第33行。
testcgi有编译错误。

您可以看到所有错误都是相同的。你忘记了声明你的一些变量。您的代码应如下所示:

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

使用strict;
使用警告;

使用CGI;

#在文本中读入
$ ENV {'REQUEST_METHOD'} =〜tr / a-z / A-Z /;

my $ buffer;

if($ ENV {'REQUEST_METHOD'} eqGET){
$ buffer = $ ENV {'QUERY_STRING'};
}

#将信息拆分为名称/值对
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();

打印$ name。。$ surname。。$ age。。$ gender;

请注意,我已经使用 my 来声明变量,而不是 local local 在很大程度上是Perl 4的宿醉。自从二十年前发布Perl 5以来, my 一直是最好的在Perl程序中声明大多数变量的方法。还要注意,我声明的变量尽可能接近它们的使用位置。



我们可以在这里修改其他一些内容。




  • 如果我们从CGI.pm导入已命名的子例程,我们可以通过省略包名来使它们的调用变得更简洁一些。

  • 我们可以使用CGI.pm中的 param 子程序来替换错误的CGI参数解析器。

  • 我们可以使用事实使用双引号字符串扩展Perl变量以使您的 print 语句易于阅读。



进行这些更改后,您的代码将简化为:

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

使用strict;
使用警告;

使用CGI qw(param header);

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

#我们输出纯文本,而不是HTML
print header(-content_type =>'text / plain');

print$ name $ surname $ age $ gender;

这看起来不简单吗?



你可以在命令行中测试它:

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

foo bar 18 M

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

I have an error while developing simple sign up form application with Perl.

This is my 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>

This is my perl script.

#!"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;

I have an error like this. "End of script output before headers: signup.cgi".How can I fix the problem ?

解决方案

Your first mistake is to try learning Perl from the TutorialsPoint site. They really have no idea what they are talking about. Try the Perl tutorials hub instead for pointers to better quality Perl tutorials.

Although CGI programs are designed to run on a web server, it's often useful to run them from a command line in order to debug them. In particular, when tracking down syntax errors, then you can use perl -c to see all of the problems. I put your code in a file called "testcgi" and ran the command perl -c testcgi. I got the following output:

$ 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.

You can see that all of your errors are the same. You have forgotten to declare some of your variables. Your code should look like this:

#!"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;

Notice that I have used my to declare the variables, not local. local is largely a hangover from Perl 4. Since Perl 5 was released over twenty years ago, my has been the best way to declare most variables in a Perl program. Notice, also, that I declare the variables as close as possible to where they are used.

There are some other things we can change here.

  • If we import named subroutines from CGI.pm, we can make the calls to them a little cleaner by omitting the package name.
  • We can use the param subroutine from CGI.pm to replace your buggy CGI parameter parser.
  • We can use the fact that Perl variables are expanded in double-quoted strings to make your print statement easier to read.

Making those changes, your code reduces to this:

#!"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";

Doesn't that look simpler?

And you can test it from the command line:

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

foo bar 18 M

The biggest lesson here is that if you're learning a new language, you shouldn't trust random tutorial sites on the internet. They are rarely any use. Ask people who know the language where to find good resources.

这篇关于一个简单的Perl应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 16:10
查看更多