我有一個 Perl 腳本,它建立一個資料庫連接并以 HTML 格式顯示輸出。它試圖顯示的資料中嵌入了標簽 (<>),因此不會顯示 HTML。如果我打開腳本使用記事本生成的實際 HTML 檔案,我會看到資料。但是由于標簽的原因我無法顯示它。知道如何解決這個問題嗎?
#!/usr/bin/perl
use DBI;
use HTML::Escape 'escape_html';
unlink("D:\\Perl32\\scripts\\UndeliveredRAW.html");
my $host = '${Node.Caption}';
my $user = '${USER}';
my $pwd = '${PASSWORD}';
my $driver = "SQL Server";
$dbhslam = DBI->connect("dbi:ODBC:Driver=$driver;Server=$host;UID=$user;PWD=$pwd") || die "connect failed:";
$sthslam = $dbhslam->prepare("SELECT
DBA_Reports.dbo.undelivered_raw_host_msgs.ID
DBA_Reports.dbo.undelivered_raw_host_msgs.MESSAGE
FROM
DBA_Reports.dbo.undelivered_raw_host_msgs");
$sthslam->execute;
$msg = "Up";
$Count = 0;
$Output = "";
$Temp = "";
$tbl = "<TABLE border=1 bordercolor=orange cellspacing=0 cellpadding=1>";
$tblhd = "<TR><TH>ID</TH><TH>MESSAGE</TH></TR>";
while (my $ref = $sthslam->fetchrow_hashref()) {
$Count ;
$Output .= '<TR><TD align=center rowspan=1 valign=top width=1000 height=1000>'
. $ref->{'ID'}.'</TD>'
. '<TD align=center rowspan=1 valign=top width=1000 height=1000>'
. escape_html($ref->{'MESSAGE'}).'</TD></TR>';
}
$dbhslam->disconnect;
$Output = "$tbl$tblhd$Output</TABLE>";
my $filename1 = 'D:\\Perl32\\Scripts\\UndeliveredRAW.html';
open(my $fh1, '>', $filename1) or die "Could not open file '$filename1' $!";
print $fh1 "$Output";
close $fh1;
if ($Count > 0) {
$msg = $Output;
}
print "\nMessage: $msg";
print "\nStatistic: $Count";
期望輸出

生成的 HTML 內容

uj5u.com熱心網友回復:
以下代碼片段演示了已發布代碼的略微修改版本。
escape_html(...)有關資料庫獲取資料的用法,請參閱回圈部分。
#!/usr/bin/env perl
#
# vim: ai ts=4 sw=4
use strict;
use warnings;
use feature 'say';
use DBI;
use HTML::Escape qw/escape_html/;
my $filename = 'D:\Perl32\scripts\UndeliveredRAW.html';
unlink($filename) if -e $filename;
my $host = ${Node.Caption};
my $user = ${USER};
my $pwd = ${PASSWORD};
my $driver = 'SQL Server';
my $dbh = DBI->connect("dbi:ODBC:Driver=$driver;Server=$host;UID=$user;PWD=$pwd")
or die 'DB connect failed:';
my $query = '
SELECT
DBA_Reports.dbo.undelivered_raw_host_msgs.MESSAGE
FROM
DBA_Reports.dbo.undelivered_raw_host_msgs
';
my $rv = $dbh->do($query) or die $dbh->errstr;
my $msg = 'Up';
my $Count = 0;
my $tbl = '
<TABLE border=1 bordercolor=orange cellspacing=0 cellpadding=1>
<TR><TH>MESSAGE</TH></TR>
';
while (my $ref = $sth->fetchrow_hashref()) {
$Count ;
$tbl .= "\n\t<TR><TD align=center rowspan=1 valign=top width=5000 height=5000>"
. escape_html($ref->{'MESSAGE'})
. '</TD></TR>';
}
$tbl .= '
</TABLE>
';
my $html =
'<!DOCTYPE html>
<html>
<head>
<title>Undelivered RAW</title>
</head>
<body>
<h1>DB table data</h1>
' . $tbl . '
</body>
</html>
';
open my $fh, '>', $filename
or die "Could not open file '$filename1' $!";
print $fh $html;
close $fh;
if ($Count > 0) {
say 'Message: ' . $msg;
say 'Statistic: ' . $Count";
}
注意:為避免HTML 樣式屬性污染代碼,請花點時間學習 CSS,您生成的HTML不包含DOCTYPE、html、head、title、body所需的部分
參考:
- 發展部
- HTML::轉義
- DBI/DBD::ODBC 教程
- CSS
- HTML
uj5u.com熱心網友回復:
HTML 有一個易于理解的機制來包含通常會被解釋為特殊字符的字符。例如,如果您想<在 HTML 中包含 a ,這通常會被視為在您的檔案中開始一個新的 HTML 元素。
解決方案是用代表這些字符的 HTML 物體替換那些有問題的字符。例如,<應替換為<. 請注意,這意味著如果要將與號 ( ) 包含在 HTML 中,則&需要將與號 ( ) 添加到應替換的字符集(在本例中為&)。
Perl 在 Web 上的使用歷史悠久,因此有許多工具可用于執行這種替換也就不足為奇了。HTML::Escape可能是最著名的。它提供了一個函式 ( escape_html()),它接受一個文本字串并回傳該字串,其中所有有問題的字符都被適當的物體替換。
use HTML::Escape 'escape_html';
my $html = '<some text> & <some other text>'
my $escaped_html = escape_html($html);
運行此代碼后,$escaped_html現在包含“$lt;some text$gt; $amp; $lt;some other text$gt;”。如果您將該文本發送到瀏覽器,您將獲得正確的輸出。
因此,最簡單的解決方案是在程式頂部加載 HTML::Escape,然后escape_html()在向輸出添加可能有問題的字串時呼叫。這意味著你的while回圈看起來像這樣:
while (my $ref = $sthslam->fetchrow_hashref()) {
$Count ;
$Output .= '<TR><TD align=center rowspan=1 valign=top width=5000 height=5000>'
. escape_html($ref->{'MESSAGE'})
. '</TD></TR>';
}
請注意,我已經洗掉了$Temp變數(它似乎沒有做任何有用的事情)并切換到.=用于構建輸出字串。=.是“賦值連接”運算子 - 它將右側的新字串添加到左側變數中當前存在的任何內容的末尾。
您似乎是在作業中學習 Perl(這很棒),但是您在一個似乎使用已經過時大約 20 年的技術的環境中學習它真的很遺憾。您的問題很好地說明了為什么嘗試在 Perl 代碼中構建原始 HTML 字串是個壞主意。使用某種模板引擎是一個更好的主意(Perl 世界中的事實上的標準似乎是Template Toolkit)。
我還建議將層疊樣式表視為一種更現代的 HTML 輸出樣式方法。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/316282.html
