2015-07-12 43 views
0

花了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。 –

回答

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

+0

非常感謝。我沒有任何問題解決這些問題,只能使用這種媒體作爲指導和教學要點。希望有一天我會在幫助別人的鍵盤的另一端。我絕對很喜歡Java,JS和其他。我最大的問題之一是正確提交表格。如何決定是否以一個「行動」或多個部分提交?再次感謝,很高興知道一旦我正確設置了Padre,我就可以修復我的錯誤。 – allendks45

1

帕德里運行使用命令像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

相關問題