2011-04-05 45 views
0

我是一個Perl新手。我試圖根據以前的作品和書籍如Learning PerlModern Perl來學習。我試圖更新這個腳本來解釋HTML表單中的數據並將其寫入文本文件,因爲我們的實驗室有興趣重新啓動並運行它。最初的腳本是爲在Linux服務器上使用而編寫的,但我們之後從Linux切換到Windows服務器。我的平面文件文本文件的路徑是否錯誤?從Linux到Windows服務器

我沒有管理員權限來查看錯誤消息的服務器是具有ActivePerl的Windows服務器。我很難搞清楚Windows的等價路徑,告訴Perl腳本在哪裏寫信息。從談話到管理員看來,我們的內部網映射到E:驅動器上,雖然這可能不是致命錯誤。

當我嘗試運行數據後,瀏覽器這個腳本在表單上輸入了它只是返回一個通用:

CGI Error 
The specified CGI application misbehaved by not returning a complete set of HTTP headers. 

任何提示,文檔,教程讚賞。謝謝。

#!C:\perl\bin\perl.exe -w -t 

# Good programming practice dictates... 
use strict; 
use warnings; 

# CGI.pm -- makes life easy 
#Carp qw(fatalsToBrowser); outputs the error messages to the browser since there is no terminal to output error messages to. Should be removed before script is used in production. 
use CGI::Carp qw(fatalsToBrowser) or die "Problems loading CGI.pm"; 

# Initialize the CGI Interface 
my($cgi) = new CGI; 

# Print the Header 
print $cgi->header(); 

#The dbmopen call is now de-appreciated. IE: it no longer works 
#Kept for archival reasons 
#if (! dbmopen(%DB, "/vol/research/docs/old_site_files/eyesignup/data/eyesignup_NEW.dat", 0666)) 
# { 
# print "Error -- Cannot open database.\n"; 
# exit; 
# } 
# Tie is the correct way to do it now. But first we are going to experiment with writing to a flat .txt file. 
open (Datastore, '>>',"E:/intranet/sds/research/docs/data.txt") or die "Can't open file: $!"; 



# Store variables and increment access count for this user 
# So param('VARIABLE') is the name of the variables used in the HTML form while $custVARIABLE is the input for the database 

my($custFirst) = $cgi->param('firstname'); 
my($custLast) = $cgi->param('lastname'); 
my($custGender) = $cgi->param('gender'); 
my($custAge) = $cgi->param('age'); 
my($custDiv) = $cgi->param('division'); 
my($custPhone) = $cgi->param('phone'); 
my($custEmail) = $cgi->param('email'); 
my($custEmployee) = $cgi->param('employee'); 
my($custInternet) = $cgi->param('internet'); 
my($custwww) = $cgi->param('www'); 
my($custDemographic) = $cgi->param('demographic'); 
my($custProjects) = $cgi->param('projectsworked'); 
my($custExperience) = $cgi->param('experience'); 
my($custWeekdays) = $cgi->param('Weekdays'); 

#Kept for archival reasons 
#my($custName) = $cgi->param('name'); 
#my($custGender) = $cgi->param('gender'); 
#my($custDiv) = $cgi->param('division'); 
#my($custPhone) = $cgi->param('phone'); 
#my($custEmail) = $cgi->param('email'); 
#my($custInternet) = $cgi->param('internet'); 
#my($custwww) = $cgi->param('www'); 
#my($custDemographic) = $cgi->param('demographic'); 
#my($custExperience) = $cgi->param('experience'); 
#my($custTimes) = $cgi->param('times'); 
#my($custStudies) = $cgi->param('studies'); 
#$custTimes =~ s/\r\n/~/g; 

#This takes the input and places it into an array, starting with the individual's 
@InfoDB = $custFirst."|".$custLast."|".$custGender."|".$custAge."|".$custDiv."|".$custPhone."|".$custEmail."|".$custEmployee."|".$custInternet."|".$custwww."|".$custDemographic."|".$custProjects."|".$custExperience."|".$custWeekdays; 
print Datastore (@InfoDB); 
print "\n"; 

#Kept for archivival reasons. 
#$DB{$custName} = $custGender."|".$custDiv."|".$custPhone."|".$custEmail."|".$custInternet."|".$custwww."|".$custDemographic."|".$custExperience."|".$custTimes."|".$custStudies; 

#Kept for archival reasons. dbmclose is de-appreciated 
#dbmclose(%DB); 
#Instead use untie. But first we're just going experiment with using a flat storage system. 
#untie(%DB); 
close (Datastore) or die; 

#Now inform the person their data has been saved. This is terribly ancient code so I haven't gotten around to fixing this part yet. 
print "Content-type: text/html\n\n"; 

print "<HTML> 
<HEAD> 
<TITLE>Thank you!</TITLE> 
</HEAD> 
<BODY>"; 

print "<H1><U>Thank You ".$custFirst."\!</U></H1> 
<P>We appreciate your assistance.</P> 
<HR width=\"75%\">"; 

print "<P><H3>The following information has been recorded:</H3> 
Name: <I>".$custFirst."</I></p><P> 
Gender: <i>".$custGender."</i></p><p> 
Division: <i>".$custDiv."</i></p><p> 
Phone: <i>".$custPhone."</i></p><p> 
Email: <i>".$custEmail."</I></p><P> 
How often do you use the internet?: <i>".$custInternet."</i></p><p> 
How often do you visit the website?: <i>".$custwww."</i></p><p> 
Are you familiar with demographic data?: <i>".$custDemographic."</i></p><p> 
Do you have work experience in economics, business, or a related field?: <i>".$custExperience."</i></p><p> 
Weekdays that you are available: <i>".$custWeekdays."</i></p><p> 
"; 

print " 
</BODY> 
</HTML>"; 

我做了一些修改,以彌補一些我在工作的限制。例如,暫時輸出錯誤到瀏覽器,直到我得到這個工作。並從舊的dbmopen調用(不再有效)調用到平面文件存儲。

回答

1

use CGI::Carp qw(fatalsToBrowser) or die "Problems loading CGI.pm";是你的問題。

$ perl -wle 'use CGI::Carp qw(fatalsToBrowser) or die "Problems loading CGI.pm";' 
syntax error at -e line 1, near "qw(fatalsToBrowser) or" 
Execution of -e aborted due to compilation errors. 

你的程序在編譯時正在死亡,所以你沒有任何有用的語法錯誤。通常情況下,你可以在日誌中看到這個,但你不能。在use聲明中,or die是不必要的(和語法錯誤)。它已經會拋出一個錯誤。

你確實需要在你的本地機器上測試你的程序的Perl副本。用它作爲彈藥。如果他們仍然不讓你擁有工具,請使用不需要安裝程序的便攜版Strawberry Perl

您還需要訪問您的錯誤日誌。詢問管理員。他們可以讓你訪問只是你的日誌,而不會讓你完全訪問服務器。

+0

謝謝,我將使用Strawberry Perl的可移植版本來測試我的腳本。這應該沒問題,因爲它不需要管理員權限來安裝。我的情況有點奇怪,因爲我從來沒有被僱用過這樣做(因此我的能力有限,並且能夠訪問諸如錯誤日誌之類的東西)。這只是我必須扭轉自己作爲額外工作責任的其中一件事。謝謝! – OneBigNewbie 2011-04-11 13:09:49

1

您有以下行:

# Print the Header 
print $cgi->header(); 

進一步回落:

#Now inform the person their data has been saved. This is terribly ancient code so I haven't gotten around to fixing this part yet. 
print "Content-type: text/html\n\n"; 

都做同樣的事情 - 要打印相同的Content-Type頭的兩倍。您可能會刪除一個或另一個print呼叫。也可以通過參考CGI.pm docs中的header()來快速瞭解您可以執行的其他操作。

+0

謝謝,我想我補充說,在我的一個嘗試調試代碼(一個網站提到,如果一個頭不存在,那麼perl將輸出到終端)。雖然我嘗試刪除一個或另一個,但它仍然無法正常工作。 :( – OneBigNewbie 2011-04-05 20:46:14

+0

這並非如此,你可以在命令行上運行腳本,並且只需將標題和其他所有內容一起發送到標準輸出,標題只是文本,嘗試通過將腳本拆分爲更小的部分,直到你找到可用的東西,然後再添加你的腳本,然後重新運行,直到找到麻煩來源。 – 2011-04-05 20:59:48

1
+0

謝謝,我沒有在工作中使用ActivePerl或StrawberryPerl(啊..在受限制的環境中工作的樂趣)我必須將它安裝在家中的機器上,並嘗試在家中進行調試,感謝這些技術的鏈接 – OneBigNewbie 2011-04-05 20:49:24

0

而且,在黑暗中刺粗糙:

open (Datastore, '>>',"E:/intranet/sds/research/docs/data.txt") or die "Can't open file: $!"; 

我不是一個Windows的傢伙,但我覺得在Windows路徑名使用backslashes,而不是正斜槓是幾乎每個人都看中使用。

除了固定電話print,你也可以更換的路徑:

E:\intranet\sds\research\docs\data.txt 
+2

Windows通常會接受任一類型的斜槓。 ,但是打開一個文件應該沒有問題,除了必須做所有的轉義之外,不能傷害到...... – Schwern 2011-04-05 22:23:50

0

,可能不會有什麼做的問題,但僅僅是一個冗餘的事情的另外一個建議:

#!C:\perl\bin\perl.exe -w -t 

-w標誌是同樣的事情:

use warnings; 
+2

不完全一樣,'use warnings'是詞法,它只適用於它的範圍(in這個 情況下,文件)。 '-w'打開整個程序的警告:您使用的每個函數和庫。不傷害打開兩者。 – Schwern 2011-04-05 22:25:26

相關問題