2014-02-25 2 views
0

perl의 textarea/textbox를하고 싶습니다. 이 태그가 있지만 구문 오류가 발생합니다. 이 다가오고 왜Perl의 Textarea 태그

<textarea name="answer" rows="20" cols="70"></textarea> 

내가 아무 생각이 없다, 내 코드는 다음과 같습니다

#!/usr/bin/perl 
use strict; 
use warnings; 
use CGI; 
use CGI::Carp qw(fatalsToBrowser); 
use constant debug=>0; 

print "Content-type: text/html\n\n"; 

# Program: assignment 3 
# Author: Jay 
# Date: 02/2014 

my $cgi= CGI->new(); 
my $a=$cgi->param('action'); 
my $num1=$cgi->param('num1'); 
my $num2=$cgi->param('num2'); 
my $num3=$cgi->param('num3'); 
my $num4=$cgi->param('num4'); 
my $hard=$cgi->param('hardware'); 
my $soft=$cgi->param('software'); 
print "$a, $num1, $num2, $num3, $num4" if debug; 

if ($a eq undef) { 
print " 
<!DOCTYPE html> 
<html> 
<head> 
<title>Error Logging</title> 
</head> 
<body> 
<h1>Error Log - IT Support</h1> 
<form method=\"post\" action=/~it.jasonc/cgi-bin/assignment3.pl> 
<input type=\"hidden\" name=\"action\" value=\"error\"> 
<table> 
<tr><td>Site:</td><td><input type=\"text\" name=\"site\"></td></tr> 
<tr><td>Type of Error:</td> 
<td><select name=\"error\"> 
<option value=\"1\">Hardware</option> 
<option value=\"2\">Software</option></select></td></tr> 
<textarea name="answer" rows="20" cols="70"></textarea> 
<tr><td colspan=\"2\"><input type=\"submit\" value=\"Submit Error\"></td></tr> 
</table> 
</form> 
</body> 
</html> " 
} 

if ($hard) { 
    print "Go to room 1"; 
} 
if ($soft) { 
    print "Go to room 2"; 
} 

내가 도와주세요

<textarea type=\"text\" name=\"details\" value\rows="4" cols="50"> 
Please explain the error here! 
</textarea> 

예를 들어 다른 유형을했다!

제이

+0

하여 인쇄 후 qq{의 사용과 따옴표 폐쇄 문자열을 대체하는 것을 당신은 나타나는 항상 완벽한 당신이 얻을 정확한 오류를 게시하고, 또한 표시해야 옆에 코드의 행 번호. – TLP

답변

4

당신이 따옴표 " 문자를 사용하여 문자열을 인용하는 경우, 당신은 그 안에 모든 따옴표를 이스케이프해야합니다. 당신은 몇 가지를 탈출하지만, 모든있다 : 예를 들어

... 
<option value=\"2\">Software</option></select></td></tr> 
<textarea name="answer" rows="20" cols="70"></textarea> 
#   ^ ^ ^^ ^^ 

당신이 볼 수 있듯이, \"2\" 탈출하지만, "answer"는 없습니다. 또한 사용할 수 있습니다

print qq# 
<!DOCTYPE html> 
.... 

:

이 문자열을 처리하는 더 좋은 방법은 다른이 예 qq##를 들어, 사용자의 필요에 따라 다양한 구분 기호를 취할 수있는 qq를 사용하는 것으로, 인용 사용하는 것입니다 heredoc :

print <<EOF; 
<!DOCTYPE html> 
.... 
EOF 
+0

예 큰 따옴표 "문자는 이해하지만 친구에게 큰 따옴표 (Perl의 전문가)가 있는지 확인하고 그 안에 큰 따옴표를 모두 이스케이프 처리했다고합니다. @TLP – Jason

+2

친구가 잘못되었습니다. 이 큰 따옴표는 이스케이프되지 않습니다. 앞에 백 슬래시를 넣어 문자를 이스케이프 처리하므로'\ "는 이스케이프 처리됩니다. – TLP

0

라인 <textarea name="answer" rows="20" cols="70"></textarea>은 큰 따옴표로 묶은 문자열 안에 있습니다. 문자열 안에 큰 따옴표를 넣으면 (큰 다중 행 문자열) 앞에 백 슬래시를 두어 이스케이프해야합니다. 그것은 당신의 코드에서 어디서나 할 수 있습니다. 코드가 끊기는 곳, 6 개의 이스케이프되지 않은 큰 따옴표가있는 곳은 예외입니다.

다음 텍스트는 HTML이고 perl이 아니므로 구문 오류가 예상됩니다.

큰 따옴표를 인용하는 것에 신경 쓰지 않으려면 (실제로는 일부를 잊어 버리는 것이 쉽습니다) 물론 다른 방법 (실제로는 여러 개)이 있습니다.

예를 들어 perl 문자열에 큰 따옴표 대신 qq{}을 사용할 수 있습니다. 따옴표가 이스케이프되는지 여부를 결정하면 코드가 작동합니다. 특별한 것은 없지만 완벽하게 표준 Perl입니다.

다음은 일부 HTML 코드에서 임의의 인용 부호를 이스케이프 처리하지 않은 예제입니다 (프로덕션 코드에서는 구문 오류가 발생하지 않도록 모든 코드를 이스케이프 처리합니다).

단순한 차이가 }

#!/usr/bin/perl 
use strict; 
use warnings; 
use CGI; 
use CGI::Carp qw(fatalsToBrowser); 
use constant debug=>0; 

print "Content-type: text/html\n\n"; 

# Program: assignment 3 
# Author: Jay 
# Date: 02/2014 

my $cgi= CGI->new(); 
my $a=$cgi->param('action'); 
my $num1=$cgi->param('num1'); 
my $num2=$cgi->param('num2'); 
my $num3=$cgi->param('num3'); 
my $num4=$cgi->param('num4'); 
my $hard=$cgi->param('hardware'); 
my $soft=$cgi->param('software'); 
print "$a, $num1, $num2, $num3, $num4" if debug; 

if ($a eq undef) { 
print qq{ 
<!DOCTYPE html> 
<html> 
<head> 
<title>Error Logging</title> 
</head> 
<body> 
<h1>Error Log - IT Support</h1> 
<form method="post" action=/~it.jasonc/cgi-bin/assignment3.pl> 
<input type=\"hidden\" name=\"action\" value=\"error\"> 
<table> 
<tr><td>Site:</td><td><input type=\"text\" name=\"site\"></td></tr> 
<tr><td>Type of Error:</td> 
<td><select name=\"error\"> 
<option value=\"1\">Hardware</option> 
<option value=\"2\">Software</option></select></td></tr> 
<textarea name="answer" rows="20" cols="70"></textarea> 
<tr><td colspan=\"2\"><input type=\"submit\" value=\"Submit Error\"></td></tr> 
</table> 
</form> 
</body> 
</html> 
} 
} 

if ($hard) { 
    print "Go to room 1"; 
} 
if ($soft) { 
    print "Go to room 2"; 
}