2010-05-01 86 views
2

用戶使用表單輸入產品代碼,價格和名稱。然後腳本將其添加到數據庫或從數據庫中刪除它。如果用戶試圖刪除不在數據庫中的產品,則會收到錯誤消息。成功添加或刪除後,他們也會收到一條消息。但是,當我測試它時,我只是得到一個空白頁面。 Perl不會提出任何警告,語法錯誤或任何東西;說一切都很好,但我仍然只是空白頁。爲什麼我從我的Perl CGI腳本中得到一個空白頁面?

腳本:

#!/usr/bin/perl 
#c09ex5.cgi - saves data to and removes data from a database 
print "Content-type: text/html\n\n"; 
use CGI qw(:standard); 
use SDBM_File; 
use Fcntl; 
use strict; 

#declare variables 
my ($code, $name, $price, $button, $codes, $names, $prices); 


#assign values to variables 
$code = param('Code'); 
$name = param('Name'); 
$price = param('Price'); 
$button = param('Button'); 

($code, $name, $price) = format_input(); 
($codes, $names, $prices) = ($code, $name, $price); 

if ($button eq "Save") { 
     add(); 
} 
elsif ($button eq "Delete") { 
     remove(); 
} 
exit; 


sub format_input { 
     $codes =~ s/^ +//; 
     $codes =~ s/ +$//; 
     $codes =~ tr/a-z/A-Z/; 
     $codes =~ tr/ //d; 
     $names =~ s/^ +//; 
     $names =~ s/ +$//; 
     $names =~ tr/ //d; 
     $names = uc($names); 
     $prices =~ s/^ +//; 
     $prices =~ s/ +$//;  
     $prices =~ tr/ //d; 
     $prices =~ tr/$//d; 
    } 


sub add { 
    #declare variable 
    my %candles; 

#open database, format and add record, close database 
    tie(%candles, "SDBM_File", "candlelist", O_CREAT|O_RDWR, 0666) 
      or die "Error opening candlelist. $!, stopped"; 

    format_vars(); 
    $candles{$codes} = "$names,$prices"; 
    untie(%candles); 

#create web page  
     print "<HTML>\n"; 
     print "<HEAD><TITLE>Candles Unlimited</TITLE></HEAD>\n"; 
     print "<BODY>\n"; 
     print "<FONT SIZE=4>Thank you, the following product has been added.<BR>\n"; 
     print "Candle: $codes $names $prices</FONT>\n"; 
     print "</BODY></HTML>\n"; 
     } #end add 


sub remove { 
    #declare variables 
    my (%candles, $msg); 

tie(%candles, "SDBM_File", "candlelist", O_RDWR, 0) 
      or die "Error opening candlelist. $!, stopped"; 

    format_vars(); 

    #determine if the product is listed 
    if (exists($candles{$codes})) { 
      delete($candles{$codes}); 
      $msg = "The candle $codes $names $prices has been removed."; 
     } 
    else { 
    $msg = "The product you entered is not in the database"; 
    } 
    #close database 
    untie(%candles); 

#create web page 
print "<HTML>\n"; 
print "<HEAD><TITLE>Candles Unlimited</TITLE></HEAD>\n"; 
print "<BODY>\n"; 
print "<H1>Candles Unlimited</H1>\n"; 
print "$msg\n"; 
print "</BODY></HTML>\n"; 
} 
+1

http://perldoc.perl.org/perldebtut! .html – Ether 2010-05-01 22:55:46

回答

1

在命令行中與運行它:

perl something.cgi Button=Save 

...給我一個錯誤:

Undefined subroutine &main::format_vars called at something.pl line 55. 

如果我改變的兩個引用format_vars()到「format_input()」,我得到我認爲是正確的輸出。

+0

修正了這個問題,但我仍然只是得到一個空白頁。 – Jason 2010-05-01 21:47:21

+0

嘗試添加一個打印「$ button \ n」;得到參數後,第19行左右。確保你得到你期望的輸入。請記住,eq檢查區分大小寫,所以如果按鈕=「保存」,它將不起作用。在輸出上做一個查看源,它有多遠?你可以通過命令行運行它嗎? – SqlACID 2010-05-01 22:33:44

+0

如果我添加打印按鈕行,它會出現保存,因爲它應該。除此之外,儘管它仍然只是一個空白頁面,頁面源代碼甚至不顯示任何內容,只是空白,根本沒有代碼。 – Jason 2010-05-01 23:03:46

1

您不打印除Content-Type標題外的任何輸出,除非調用addremove。問題在於,如果沒有按鈕被點擊,你忘記了顯示一個表單(大概是包含按鈕的表單)。

編輯:複製您的發佈代碼,並做了一些清理,然後調用它的URL http://localhost/~me/foo.cgi?Code=1;Name=2;Price=3;Button=Savehttp://localhost/~me/foo.cgi?Code=1;Name=2;Price=3;Button=Delete,我得到正確的HTML輸出。用於此代碼的清理版本:

#!/usr/bin/perl 

use strict; 
use warnings; 

print "Content-type: text/html\n\n"; 
use CGI qw(:standard); 
use SDBM_File; 
use Fcntl; 
use strict; 

#declare variables 
my ($code, $name, $price, $button, $codes, $names, $prices); 


#assign values to variables 
$code = param('Code'); 
$name = param('Name'); 
$price = param('Price'); 
$button = param('Button'); 

($code, $name, $price) = format_input(); 
($codes, $names, $prices) = ($code, $name, $price); 

if ($button eq "Save") { 
    add(); 
} 
elsif ($button eq "Delete") { 
    remove(); 
} 
exit; 


sub format_input { 
    $codes =~ s/^ +//; 
    $codes =~ s/ +$//; 
    $codes =~ tr/a-z/A-Z/; 
    $codes =~ tr/ //d; 
    $names =~ s/^ +//; 
    $names =~ s/ +$//; 
    $names =~ tr/ //d; 
    $names = uc($names); 
    $prices =~ s/^ +//; 
    $prices =~ s/ +$//; 
    $prices =~ tr/ //d; 
    $prices =~ tr/$//d; 
    } 

sub add { 
# #declare variable 
# my %candles; 
# 
# #open database, format and add record, close database 
#  tie(%candles, "SDBM_File", "candlelist", O_CREAT|O_RDWR, 0666) 
#   or die "Error opening candlelist. $!, stopped"; 
# 
#  format_vars(); 
#  $candles{$codes} = "$names,$prices"; 
#  untie(%candles); 

#create web page  
print "<HTML>\n"; 
print "<HEAD><TITLE>Candles Unlimited</TITLE></HEAD>\n"; 
print "<BODY>\n"; 
print "<FONT SIZE=4>Thank you, the following product has been added.<BR>\n"; 
print "Candle: $codes $names $prices</FONT>\n"; 
print "</BODY></HTML>\n"; 
} #end add 


sub remove { 
# #declare variables 
# my (%candles, $msg); 
# 
# tie(%candles, "SDBM_File", "candlelist", O_RDWR, 0) 
#   or die "Error opening candlelist. $!, stopped"; 
# 
#  format_vars(); 
# 
#  #determine if the product is listed 
#  if (exists($candles{$codes})) { 
#   delete($candles{$codes}); 
#   $msg = "The candle $codes $names $prices has been removed."; 
#  } 
#  else { 
#  $msg = "The product you entered is not in the database"; 
#  } 
#  #close database 
#  untie(%candles); 

    #create web page 
    print "<HTML>\n"; 
    print "<HEAD><TITLE>Candles Unlimited</TITLE></HEAD>\n"; 
    print "<BODY>\n"; 
    print "<H1>Candles Unlimited</H1>\n"; 
# print "$msg\n"; 
    print "<p>Called remove</p>"; 
    print "</BODY></HTML>\n"; 
    } 

需要注意的是,啓用warnings,這噴出了大量的「未初始化值」警告,因爲你得到$code VS $codes$name VS $names,和$price vs $prices以不好的方式彼此混淆。 (提示:您指定($code, $name, $price) = format_input();,但format_input不返回三個值。)

我懷疑,正如您在先前的評論中所建議的那樣,您遇到了大小寫敏感問題。我第一次嘗試測試這個失敗,因爲我在URL中使用了「button = Save」而不是「Button = Save」。根據約定,HTTP請求參數名稱通常全部爲小寫字母,並且有充分的理由,因爲它有助於避免此類問題。

其他亂評:

  • 可以同時聲明變量,你第一次給它們,例如,my $code = param('Code');。這通常被認爲是更好的/首選的做法,因爲儘可能晚地做出聲明有助於最大限度地減少變量的範圍。

  • format_input,它是多餘既s/^ +//; s/ +$//;tr/ //d;,作爲tr還將刪除開頭和結尾的空格。

  • 獲取參數值時,如果參數爲空/缺少,或者檢查是否爲空/缺少並向用戶顯示錯誤,則應提供缺省值。

  • 如果$button丟失或無效,您應該在elsif ($button eq "Delete")之後有最終的else子句,以顯示錯誤。是的,我知道這個腳本是從一個特定的表單調用的,所以它應該總是有一個有效的$button,但是繞過表單並直接向腳本提交任何一組值(有效或無效)是微不足道的,所以您仍然需要驗證並驗證服務器端的所有內容,因爲您不知道它實際來自哪裏或客戶端是否正確驗證了它。

+0

這沒有任何意義。我可以添加一個打印按鈕行,並驗證按鈕值是否越過,這意味着添加或刪除IS被調用,並且我仍然會得到一個空白頁。一旦按下按鈕,表單就會調用腳本,而不是相反。 – Jason 2010-05-02 16:26:33

0

這就是我如何運行腳本,它確實產生了正確的結果。確保您在網站的任何位置都安裝了適當的PERL模塊。

注:我使用的託管服務(BlueHost的)要求我通過#調出我的Perl模塊的/ usr/bin中/ perlml

#!/usr/bin/perlml 

    use strict; 
    use warnings; 

    print "Content-type: text/html\n\n"; 
    use CGI qw(:standard); 
    use SDBM_File; 
    use Fcntl; 
    use strict; 

    #declare variables 
    my ($code, $name, $price, $button, $codes, $names, $prices); 


    #assign values to variables 
    $code = param('Code'); 
    $name = param('Name'); 
    $price = param('Price'); 
    $button = param('Button'); 

    ($codes, $names, $prices) = format_input(); 
    ($codes, $names, $prices) = ($code, $name, $price); 

    if ($button eq "Save") { 
    add(); 
    } 
    elsif ($button eq "Delete") { 
    remove(); 
    } 
    exit; 


    sub format_input { 
    $codes =~ s/^ +//; 
    $codes =~ s/ +$//; 
    $codes =~ tr/a-z/A-Z/; 
    $codes =~ tr/ //d; 
    $names =~ s/^ +//; 
    $names =~ s/ +$//; 
    $names =~ tr/ //d; 
    $names = uc($names); 
    $prices =~ s/^ +//; 
    $prices =~ s/ +$//; 
    $prices =~ tr/ //d; 
    $prices =~ tr/$//d; 
    } 

    sub add { 
    #declare variable 
    my %candles; 

    #open database, format and add record, close database 
    tie(%candles, "SDBM_File", "candlelist", O_CREAT|O_RDWR, 0666) 
    or die "Error opening candlelist. $!, stopped"; 

    format_input(); 
    $candles{$code} = "$name,$price"; 
    untie(%candles); 

    #create web page  
    print "<HTML>\n"; 
    print "<HEAD><TITLE>Candles Unlimited</TITLE></HEAD>\n"; 
    print "<BODY>\n"; 
    print "<FONT SIZE=4>Thank you, the following product has been added.<BR>\n"; 
    print "Candle: $codes, $names, $prices</FONT>\n"; 
    print "</BODY></HTML>\n"; 
    } #end add 

    sub remove { 
    #declare variables 
    my (%candles, $msg); 

    tie(%candles, "SDBM_File", "candlelist", O_RDWR, 0) 
    or die "Error opening candlelist. $!, stopped"; 
    format_input(); 

    #determine if the product is listed 
    if (exists($candles{$code})) { 
    delete($candles{$code}); 
    $msg = "The candle $code, $name, $price has been removed."; 
    } 
    else { 
    $msg = "The product you entered is not in the database"; 
    } 
    #close database 
    untie(%candles); 

    #create web page 
    print "<HTML>\n"; 
    print "<HEAD><TITLE>Candles Unlimited</TITLE></HEAD>\n"; 
    print "<BODY>\n"; 
    print "<H1>Candles Unlimited</H1>\n"; 
    print "$msg\n"; 
    print "</BODY></HTML>\n"; 
    } 
相關問題