开发者

Why does Perl complain about a syntax error during compilation of my program with this HTML string?

I am facing a problem in my code. Whenever I try to insert a text field then it's giving an error. What's wrong in the syntax here?

   print '<table>';
  print "<tr style='background-color:#CDC9C9;'>
<td><A HREF=\"http://localhost/cgi-bin/AddUser.cgi\">ADD</A></td>
<td></td>
<td><b>UserId</b></td>
<td><input type="text" name="UserId"></td>
<td><b>UserName</b></td>
 <开发者_Go百科;td><input type="text" name="User_Name"></td>
 <td><input type="submit" name="Filter" value="Filter">  </td>
  </tr>";
print"</table>"; 


If you have a double-quoted string then it can't contain unescaped double-quotes (for, hopefully, obvious reasons).

Some ways to get around it:

1/ Escape the double quotes.

print "<tr style='background-color:#CDC9C9;'>
<td><A HREF=\"http://localhost/cgi-bin/AddUser.cgi\">ADD</A></td>
<td></td>
<td><b>UserId</b></td>
<td><input type=\"text\" name=\"UserId\"></td>
<td><b>UserName</b></td>
<td><input type=\"text\" name=\"User_Name\"></td>
<td><input type=\"submit\" name=\"Filter\" value=\"Filter\">  </td>
</tr>";

2/ Switch to a single-quoted string (as your string contains no variables or escape sequences).

print '<tr style="background-color:#CDC9C9;">
<td><A HREF="http://localhost/cgi-bin/AddUser.cgi">ADD</A></td>
<td></td>
<td><b>UserId</b></td>
<td><input type="text" name="UserId"></td>
<td><b>UserName</b></td>
<td><input type="text" name="User_Name"></td>
<td><input type="submit" name="Filter" value="Filter">  </td>
</tr>';

Note: I had to change the single quotes in the style attribute to double quotes here.

3/ Use a here-doc.

print <<END_OF_HTML;
<tr style='background-color:#CDC9C9;'>
<td><A HREF="http://localhost/cgi-bin/AddUser.cgi">ADD</A></td>
<td></td>
<td><b>UserId</b></td>
<td><input type="text" name="UserId"></td>
<td><b>UserName</b></td>
<td><input type="text" name="User_Name"></td>
<td><input type="submit" name="Filter" value="Filter">  </td>
</tr>
END_OF_HTML

4/ Choose a different quoting character.

print qq[<tr style='background-color:#CDC9C9;'>
<td><A HREF="http://localhost/cgi-bin/AddUser.cgi">ADD</A></td>
<td></td>
<td><b>UserId</b></td>
<td><input type="text" name="UserId"></td>
<td><b>UserName</b></td>
<td><input type="text" name="User_Name"></td>
<td><input type="submit" name="Filter" value="Filter">  </td>
</tr>];

But like so many of your problems, the real solution is to use a templating system.


You need to escape quite a few "

Try:

print '<table>';
print "<tr style='background-color:#CDC9C9;'>
<td><A HREF=\"http://localhost/cgi-bin/AddUser.cgi\">ADD</A></td>
<td></td>
<td><b>UserId</b></td>
<td><input type=\"text\" name=\"UserId\"></td>
<td><b>UserName</b></td>
<td><input type=\"text\" name=\"User_Name\"></td>
<td><input type=\"submit\" name=\"Filter\" value=\"Filter\">  </td>
</tr>";
print"</table>";

A better alternative would be to use a heredoc as:

$table = << "TABLE";
<table>
<tr style='background-color:#CDC9C9;'>
<td><A HREF="http://localhost/cgi-bin/AddUser.cgi">ADD</A></td>
<td></td>
<td><b>UserId</b></td>
<td><input type="text" name="UserId"></td>
<td><b>UserName</b></td>
<td><input type="text" name="User_Name"></td>
<td><input type="submit" name="Filter" value="Filter">  </td>
</tr>
</table>
TABLE
print $table;


You need to escape the double quotes using "\". For bigger strings I suggest you use a HEREDOC.

e.g.:

print "My string contains quite some \"double quotes\"";
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜