为什么在Padre上测试Perl/CGI scipt时会出现此错误?

问题描述:

花了3天时间编写CGI脚本的一部分来处理来自我的HTML的输入表单数据。使用Padre作为编辑器,但现在在运行脚本时收到此错误。为什么在Padre上测试Perl/CGI scipt时会出现此错误?

  • “从用户代码未捕获的异常:‘-T

我想一些指针’!是在#线,也必须在scipt.pl在命令行中使用”如果有人愿意查看我的代码并提供指导。这是我第一次尝试Perl和CGI。我期望的endstate是web用户输入数据然后点击提交的表单。验证后,如果页面存在,则会将信息和错误发送回浏览器。这里是我的代码,并提前感谢!

HTML代码:

<html> 
<head><title>My First CGI-PERL</title> 
<!--Link to css for styling here--> 
<link rel="stylesheet" type="text/css" href="style.css"> 
</head> 
<body> 
<header> 
<h1>Welcome to my first CGI-PERL Form submission</h1></header> 
<br> 
<p>Please Enter the following information in the fields and click 
    SUBMIT.</p> 
<hr> 
<br> 
<div class="container1"> 
    <form action="/cgi-bin/text.pl" method="post"> <!--script to process 
    this section--> 
     Item Number<input type="text" name="Item"><br> <!--Cannot be blank--> 
     Product Name<input type="text" name="Name"><br> <!--Cannot be  
    blank--> 
     Product Cost<input type="text" name="Cost"><br> <!--must be between .50 and $1000--> 
     Selling Price<input type="text" name="Price"><br> <!--price from 1.00 to 2000.00--> 
     Quantity on Hand<input type="text" name="Quantity"><br> <!--cannot be negative--> 
    </form></div> 
    <br> 
    <br> 
    <hr> 
    <br> 
    <h2>Choose A Product Category</h2> <!--must be one of the categories--> 
    <br> 
    <div class="container2"> 
    <form action="/cgi-bin/radio.pl" method="post"> <!--name of script that handles this section--> 
     <input type="radio" name="letter" value="F">F<br> 
     <input type="radio" name="letter" value="H">H<br> 
     <input type="radio" name="letter" value="M">M<br> 
     <input type="radio" name="letter" value="C">C<br> 
     <input type="radio" name="letter" value="T">T<br> 
    <br> 
    </form></div> 
    <hr> 
    <br> 
    <div class="container4"> 
    <form action="/cgi-bin/myfirstcgi.cgi" method="post"> <!--script to process submit and send a second page back--> 
     <input type="submit" name="submit" value="SUBMIT"><br> 
    </form></div> <!--close container 2--> 

    <!--Profit should be auto generated on the second page created by the script--> 
    </body> 
    </html> 

的Perl脚本:

#!/usr/bin/perl 
print "Content-type: text/html\n\n"; 
use strict; 
use warnings; 
use CGI qw(:standard); 

print "<html><body>"; 
print "Thank you for submitting the form. <br>\n"; 
#return to html page with form 
print "To go back to the form page click"; #how can i insert a link to the form page here 


print "You chose item number ", param("Item")," \n"; 
print "The product name is ", param("Name"), " \n"; 
print "The Cost is ", param("Cost")," \n"; 
print "Selling Price ", param("Price")," \n"; 
print "Quantity on Hand is ", param("Quantity")," \n"; 

#scalar variables to hold form input data 
my $item = param("Item"); 
my $name = param("Name"); 
my $cost = param("Cost"); 
my $price = param("Price"); 
my $category = param("Category"); #radio button chosen 
my $quantity = param("Quantity"); 
my $profit = $cost - $price; 

#radio buttons 
my %categories = ("F", "H", "M", "C", "T"); 
#validation a category was chosen 

my $categories = param("letter"); 
if (exists $categories{$category}) { 
    print "The product Category is $category"; 
} 
else { 
    error ("You must select a category"); 
} 

#validate input 
if ($item eq "") { 
    error ("Field cannot be blank"); 
} 
if ($name eq "") { 
    error ("Field cannot be blank"); 
} 
if ($cost < .50 && $cost > 1000) { 
    error ("Invalid Entry Cost must be between $.50 and $1000"); 
} 
if ($price < 1.00 && $cost > 2000) { 
    error ("Invalid Amount Please enter Price between $1.00 and $2000"); 
} 
if ($quantity < 0) { 
    error ("Quantity cannot be negative number"); 
} 

sub error { 
my ($errormsg) = @_; 
print "<h2>Error</h2>\n"; 
print "$errormsg<p>\n"; 
print "</body></html>\n"; 
exit; 
} 
+0

你的脚本有很多错误,顺便说一句,我建议你阅读[此](http://perldoc.perl.org/perlsec.html#Taint-mode)。使用'perl -T'运行。 并修复所有错误,并分享您的HTML也有问题,它会更好。 –

+0

@Arunesh我该如何运行perl -T?我使用Padre(我通常使用Notepad ++时对我来说是新的)。 – allendks45

+0

在Windows中使用cmd运行。从未使用过padre。 –

你的功课应该由你来完成。但是由于那里有很多语法错误,我会尽力帮助你。

首先将html内部的所有元素绑定到一个带有单个提交按钮的表单中,以便它可以一次性以查询字符串的形式进入服务器。

你的HTML应该是:

<html> 
<head><title>My First CGI-PERL</title> 
<!--Link to css for styling here--> 
<!--<link rel="stylesheet" type="text/css" href="style.css">--> 
</head> 
<body> 
<header> 
<h1>Welcome to my first CGI-PERL Form submission</h1></header> 
<br> 
<p>Please Enter the following information in the fields and click 
    SUBMIT.</p> 
<hr> 
<br> 
<div class="container1"> 
    <form action="action.cgi" method="post"> <!--script to process 
    this section--> 
     Item Number<input type="text" name="Item"><br> <!--Cannot be blank--> 
     Product Name<input type="text" name="Name"><br> <!--Cannot be  
    blank--> 
     Product Cost<input type="text" name="Cost"><br> <!--must be between .50 and $1000--> 
     Selling Price<input type="text" name="Price"><br> <!--price from 1.00 to 2000.00--> 
     Quantity on Hand<input type="text" name="Quantity"><br> <!--cannot be negative--> 
    <br> 
    <br> 
    <hr> 
    <br> 
    <h2>Choose A Product Category</h2> <!--must be one of the categories--> 
    <br> 
    <div class="container2"> 
     <input type="radio" name="letter" value="F">F<br> 
     <input type="radio" name="letter" value="H">H<br> 
     <input type="radio" name="letter" value="M">M<br> 
     <input type="radio" name="letter" value="C">C<br> 
     <input type="radio" name="letter" value="T">T<br> 
    <br> 
    <hr> 
    <br> 
    <div class="container4"> 
     <input type="submit" name="submit" value="SUBMIT"><br> 
    </form></div> <!--close container 2--> 

    <!--Profit should be auto generated on the second page created by the script--> 
    </body> 
    </html> 

CGI脚本我命名为action.cgi和它应该是这样的从你的方法上面的HTML。无论您遇到什么错误,我都会尝试使用注释行来显示它。

#!/usr/bin/perl -T 
print "Content-type: text/html\n\n"; 
use strict; 
use warnings; 
use CGI qw(:standard); 

print "<html><body>"; 
print '<h1>"Thank you for submitting the form. <br>"\n'; 
#return to html page with form 
print '<h2>To go back to the form page click <a 
href="/newform.html">Here</a>.</h2>';#missing quotes 

print "<hr><br><br>"; 
print "You chose item number ",param("Item")," <br>"; 
print "The product name is ", param("Name"), " <br>";print "The Cost is 
", param("Cost")," \n"; 
print "Selling Price ", param("Price")," <br />"; 
print "Quantity on Hand is ", param("Quantity")," <br> "; 

#scalar variables to hold form input data 
my $item = param("Item"); 
my $name = param("Name"); 
my $cost = param("Cost"); 
my $price = param("Price"); 
my $category = param("Category"); #radio button chosen 
my $quantity = param("Quantity"); 
my $profit = $cost - $price; 

#radio buttons 
my @categories = ("F", "H", "M", "C", "T");#better use array 
#vali`dation a category was chosen 
$category = param("letter"); 
if(grep { /$category/ } @categories) { # check if category exist in your predefined array 
print "The product Category is $category \n"; 
} 
else { 
error ("You must select a category"); 
} 
#validate input 
if ($item eq "") { 
error ("Field cannot be blank"); 
} 
if ($name eq ""){ 
error ("Field cannot be blank"); 
} 
if ($cost < .50 && $cost > 1000) { 
error ("Invalid Entry Cost must be between $.50 and $1000"); 
} 
if ($price < 1.00 && $cost > 2000) { 
error ("Invalid Amount Please enter Price between $1.00 and $2000"); 
} 
if ($quantity < 0) { 
error ("Quantity cannot be negative number"); 
} 

#Error subroutine 
sub error { 
my $errormsg = shift;#you shouldn't assign array to variable 
print "<h2>Error</h2>\n"; 
print "$errormsg<p>\n"; 
print "</body></html>\n"; 
} 

如果你真的想基本的了解后,学习Perl的尝试学习

perldscperlvarperlrefperlooperlobj

+0

非常感谢。我没有任何问题解决这些问题,只能使用这种媒体作为指导和教学要点。希望有一天我会在帮助别人的键盘的另一端。我绝对很喜欢Java,JS和其他。我最大的问题之一是正确提交表格。如何决定是否以一个“行动”或多个部分提交?再次感谢,很高兴知道一旦我正确设置了Padre,我就可以修复我的错误。 – allendks45

帕德里运行使用命令像Perl程序:

/usr/bin/perl <your_file.pl> 

错误检查要求编译器如何工作的深刻变化,所以需要在编译器启动后立即打开。在程序中的shebang行上有-T,并且在编译器启动之前不会被解析 - 对于启用污染模式来说太晚了。当你认为污染模式已经打开时,Perl会开始运行你的代码,而不是在污染模式下运行,所以它会使用你所看到的错误信息停止执行。

可以通过配置策略平台与为略有不同的命令运行代码解决这个问题:

/usr/bin/perl -T <your_file.pl> 

在帕德里选择工具 - 从菜单>首选项,然后选择“语言 - 的Perl 5”选项。有一个标有“解释器参数”的文本输入。你可以把你的-T放在那里并保存更改。

此外,我只是重申my previous advice在2015年使用CGI是荒谬的。请看CGI::Alternatives并切换到更现代化的建筑。

+0

我绝对肯定你对此正确。不幸的是,我注册了一个网络服务器类,这是最后一项任务。从我在网上阅读的内容来看,这已经非常陈旧了,并被其他语言所取代。如果是JS,我相信我会完成这个项目。 – allendks45

+0

相当严峻的消息,仍然有教授这些东西的课程。的确,现在有很多其他语言被人们用来编写Web应用程序 - 但人们仍然使用Perl(仅仅使用更现代的工具)。我想这是你第一次告诉我们我们正在帮你做作业: -/ –

+0

是的,最后的夏季项目并不相信它会有所作为。只是为了了解Perl中的概念,因为我只有1周的时间来学习这门语言。高的顺序,正如你所说的那样,有一大堆语言能够处理我试图用更简单的方法完成的事情。对于教练员来说,这是一个好人,并且在有限的时间内帮助他们。 – allendks45