2011-04-05 44 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

相关问题