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

Why am I getting this error when testing a Perl/CGI scipt on Padre?

花了 3 天的大部分时间编写 CGI 脚本来处理来自我 HTML 的输入表单数据。使用 Padre 作为编辑器,但现在在 运行 脚本时收到此错误。

如果有人愿意查看我的代码并提供指导,我希望得到一些指示。这是我第一次接触 Perl 和 CGI​​。我想要的最终状态是网络用户输入数据然后点击提交的表单。验证后,页面将返回给浏览器,其中包含信息和错误(如果存在)。这是我的代码,提前致谢!

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 00-->
        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 00");
}
if ($price < 1.00 && $cost > 2000) {
   error ("Invalid Amount Please enter Price between .00 and 00");
}
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;
}

Padre 运行s Perl 程序使用如下命令:

/usr/bin/perl <your_file.pl>

污点检查需要对编译器的工作方式进行深入更改,因此需要在编译器启动后立即启用。您在程序中的 shebang 行上有 -T 并且直到编译器启动后才被解析 - 为时已晚,无法启用污点模式。当您认为污点模式已打开时,Perl 启动您的代码而不是污点模式会令人困惑,因此它会停止执行并显示您看到的错误消息。

您可以通过使用稍微不同的命令将 Padre 配置为 运行 您的代码来解决此问题:

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

在 Padre 中,从菜单中选择工具 -> 首选项,然后 select "Language - Perl 5" 选项。有一个标记为 "Interpreter arguments" 的文本输入。您可以将 -T 放在那里并保存更改。

此外,我将重申 that using CGI in 2015 is ridiculous. Please take a look at CGI::Alternatives 并切换到更现代的架构。

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

首先将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 00-->
        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 00");
}
if ($price < 1.00 && $cost > 2000) {
error ("Invalid Amount Please enter Price between .00 and 00");
}
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,那就试试吧

perldsc,perlvar,perlref,perloo,perlobj