为什么在Padre上测试Perl/CGI scipt时会出现此错误?
花了3天的大部分时间编写CGI脚本来处理HTML中的输入表单数据。使用Padre作为编辑器,但现在在运行脚本时收到此错误为什么在Padre上测试Perl/CGI scipt时会出现此错误?,perl,cgi,Perl,Cgi,花了3天的大部分时间编写CGI脚本来处理HTML中的输入表单数据。使用Padre作为编辑器,但现在在运行脚本时收到此错误 来自用户代码的未捕获异常:“-T”位于#!行上,它还必须在scit.pl的命令行上使用 如果有人愿意查看我的代码并提供指导,我想要一些提示。这是我第一次尝试Perl和CGI。我想要的endstate是一个Web用户输入数据然后点击submit的表单。验证后,页面将发送回浏览器,其中包含信息和错误(如果存在)。这是我的代码,提前谢谢 HTML代码: <html>
- 来自用户代码的未捕获异常:“-T”位于#!行上,它还必须在scit.pl的命令行上使用
<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>
我的第一个CGI-PERL
欢迎使用我的第一个CGI-PERL表单提交
请在字段中输入以下信息,然后单击
提交
项目编号
产品名称
产品成本
售价
现存量
选择一个产品类别
F
H
M
C
T
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;
}
#/usr/bin/perl
打印“内容类型:text/html\n\n”;
严格使用;
使用警告;
使用CGI qw(:标准);
打印“”;
打印“感谢您提交表单。
\n”;
#返回带有表单的html页面
打印“要返回表单页面,请单击”#如何在此处插入指向表单页的链接
打印“您选择的项目编号”、参数(“项目”)、“\n”;
打印“产品名称为”,参数(“名称”),“\n”;
打印“成本为”,参数(“成本”),“\n”;
打印“售价”,参数(“价格”),“\n”;
打印“现存量为”,参数(“数量”),“\n”;
#保存表单输入数据的标量变量
我的$item=参数(“item”);
我的$name=param(“name”);
我的$cost=参数(“成本”);
我的$price=param(“价格”);
my$category=参数(“类别”)#选择单选按钮
my$quantity=参数(“数量”);
我的$利润=$成本-$价格;
#单选按钮
我的类别百分比=(“F”、“H”、“M”、“C”、“T”);
#确认选择了一个类别
我的$categories=参数(“字母”);
if(存在$categories{$categories}){
打印“产品类别为$Category”;
}
否则{
错误(“您必须选择一个类别”);
}
#验证输入
如果($item eq“”){
错误(“字段不能为空”);
}
如果($name eq“”){
错误(“字段不能为空”);
}
如果($cost<.50&$cost>1000){
错误(“无效输入成本必须介于0.50美元和1000美元之间”);
}
如果($price<1.00&$cost>2000){
错误(“无效金额请输入介于$1.00和$2000之间的价格”);
}
如果($数量<0){
错误(“数量不能为负数”);
}
子错误{
我的($errormsg)=@;
打印“错误\n”;
打印“$errormsg\n”;
打印“\n”;
出口
}
Padre使用如下命令运行Perl程序:
/usr/bin/perl <your_file.pl>
/usr/bin/perl
污染检查需要对编译器的工作方式进行深刻的更改,因此需要在编译器启动后立即打开它。您的程序中的shebang行上有-T,在编译器启动之前不会解析它-太晚了,无法启用污染模式。当您认为污染模式已经打开时,Perl开始不在污染模式下运行代码会让人感到困惑,因此它会用您看到的错误消息停止执行
您可以通过将Padre配置为使用稍微不同的命令运行代码来解决此问题:
/usr/bin/perl -T <your_file.pl>
/usr/bin/perl-T
在Padre中,从菜单中选择工具->首选项,然后选择“语言-Perl5”选项。有一个标记为“解释器参数”的文本输入。您可以将-T放在那里并保存更改
此外,我只想重申,2015年使用CGI是荒谬的。请看一看并换一个更现代的建筑。你的家庭作业应该由你自己完成。但是因为那里有很多语法错误,我会尽力帮助你 首先,用一个提交按钮将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-PERL
欢迎使用我的第一个CGI-PERL表单提交
请在字段中输入以下信息,然后单击
提交
项目编号
产品名称
产品成本
售价
现存量
选择一个产品类别
F
H
M
C
T
您的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";
}
#/usr/bin/perl-T
打印“内容类型:text/html\n\n”;
严格使用;
使用警告;
使用CGI qw(:标准);
打印“”;
打印“感谢您提交表单。
”\n;
#返回带有表单的html页面
打印“要返回表单页面,请单击”#缺少引号
打印“
”;
打印“您选择的项目编号”,参数(“项目”),“
”;
打印“产品名称为”,参数(“名称”),“
”;打印“成本为
,参数(“成本”),“\n”;
打印“售价”,参数(“价格”),“
”;
打印“现存量为”,参数(“数量”),“
”;
#保存表单输入数据的标量变量
我的$item=参数(“item”);
我的$name=param(“name”);
我的$cost=参数(“成本”);
我的$price=param(“价格”);
my$category=参数(“类别”)#选择单选按钮
my$quantity=参数(“数量”);
我的$利润=$成本-$价格;
#单选按钮
my@categories=(“F”、“H”、“M”、“C”、“T”)#更好地使用阵列
#选择了一个类别
$category=参数(“字母”);
如果(grep{/$category/}@categories){#检查目录中是否存在类别