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

时间:2015-07-12 16:31:36

标签: perl cgi

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

  • "用户代码未被捕获的异常:" -T"在#!也必须在scipt.pl"
  • 的命令行中使用它

如果有人愿意查看我的代码并提供指导,我想要一些指示。这是我第一次进入Perl和CGI。我对endstate的期望是webuser输入数据然后点击提交的形式。验证后,如果页面存在,则会将页面发送回浏览器,并显示信息和错误。这是我的代码并提前致谢!

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;
}

2 个答案:

答案 0 :(得分:1)

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

/usr/bin/perl <your_file.pl>

污点检查需要对编译器的工作方式进行深入更改,因此需要在编译器启动后立即打开它。在程序内部的shebang行上有-T,并且在编译器启动之前不会解析 - 对于启用污点模式来说太迟了。当您认为污点模式已经打开时,Perl开始运行您的代码而不是处于污点模式会很困惑,因此它会使用您看到的错误消息暂停执行。

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

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

在Padre中选择工具 - &gt;从菜单中选择首选项,然后选择“语言 - Perl 5”选项。有一个标记为“解释器参数”的文本输入。您可以将-T放在那里并保存更改。

另外,我将重申my previous advice在2015年使用CGI是荒谬的。请查看CGI::Alternatives并切换到更现代的架构。

答案 1 :(得分:1)

你的作业应由你完成。但由于存在很多语法错误,我会尽力帮助你。

首先将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