2009-10-29 84 views
1

我需要一些幫助,使這個劇本......到目前爲止,我認爲它應該是這個樣子(我傾向於使用AWK很多)如何從Perl的幾個文件創建一個HTML文檔?

#!/usr/bin/perl -w 
@filelist=(
file1, 
file2, 
file3 
) 
print ("<html>"); 
foreach $filelist { 
    print ("<table>"; 
    print ("<td>"$filelist"</td>") 
    foreach [line in the file] 
    print ("<td>"$1, $2"</td>"); 
    } 
    print ("</table>"; 
} 
print ("</html>"); 

所以我想劇本去的每個文件,打印文件名,然後爲每一行打印兩個大小寫<td>

我在正確的軌道上嗎?

另外,我實現了AWK我寫需要一對夫婦的IF語句 - 我相信它應該是這樣的

while(<F_IN>) { 
if ($1>=2) { 
    print "<td class=\"green\" title=\""$1,$2"\">" 
} 
if ($1>=1) { 
    print "<td class=\"amber\" title=\""$1,$2"\">" 
} 
if ($1>=0) { 
    print "<td class=\"red\" title=\""$1,$2"\">" 
} 
+2

CSS類的名稱,如「綠色」是一個壞主意。使用與通過使用類傳遞的概念相關的類名。例如,如果您正在編寫腳本以顯示系統狀態信息,則綠色應爲'status_ok',琥珀色應爲'status_warning',紅色應爲'status_alert'。這樣,當您因製作色盲所無法使用的系統而煩惱時,您可以將顏色切換爲白色,淺藍色和深藍色,而無需在所有html中重命名類,或者更改只更改樣式表那個班'red' ='背景:深藍色;顏色:白色'。 – daotoad 2009-10-29 15:18:42

+0

:D謝謝 - 這將只會被少數人使用,但我明白了你的觀點:) – Soop 2009-10-29 15:31:00

+2

@Soop,從一開始就使用好名字的難度是TINY,而不是做錯的代價。對那些在兩年內不得不維護你的代碼的可憐的男孩感到可惜。它可能是你。冷靜下來吧@Sinan, – daotoad 2009-10-29 15:36:27

回答

8

您的代碼並沒有真正任何意義。我建議在嘗試任何更復雜的事情之前先看一下基本的Perl intro

這就是說,你很可能試圖做的是這樣的:在CPAN

#!/usr/bin/perl 

use strict;  # never forget this line 
use warnings; # or this one, it's better than the -w flag 

use CGI qw(:standard); # this gives us convenient functions for outputting 
         # HTML, plus other stuff for processing CGI requests 

my @filelist = ('file1', 'file2', 'file3'); 

print header; 
print start_html; 
print start_table; 

foreach my $file(@filelist) { 
    print Tr(td($file)); 
} 

print end_table; 
print end_html; 

欲瞭解CGI模塊(大量)的文檔,請CGI

編輯

如果你想在表中的每個文件的內容,那麼你就需要在每個文件讀入內循環的變量。這裏是你如何能做到這一點的例子:

foreach my $file(@filelist) { 
    open my $fh, $file or die "Could not open $file: $!"; 

    local $/ = undef; # this allows us to slurp the whole file at once 
    my $filedata = <$fh>; 

    print Tr(td($filedata)); 
} 

一些更多的文檔,你應該閱讀:Perl open() tutorial,以及所有關於slurping

+0

哇,你真的知道perl! 謝謝你。 四個問題: 1-我需要文件名周圍的引號嗎? 2 - 如果我想打開文件來逐行解析它們,我是否需要執行整個「打開(處理」$ filename「);,以及如何引用數組的一部分? 3 - I'我已經編輯了我的原始請求,並且使用了我從AWK得知的$ 1,$ 2 - 是否會按順序引用每行上的字符串? 4 - 我可以(與AWK一樣)將它輸出到.html文件中像「perl myfile> page.html」? – Soop 2009-10-29 15:13:25

+0

+1 @Friedo:我想他想要這些文件的內容在td中@Soop你必須在做之前真正嘗試學習這門語言複雜的東西 – 2009-10-29 15:21:48

+0

默認情況下打印到標準輸出,所以你可以做任何正常的重定向 你可以在你的腳本中打開一個句柄,並打印到它'print $ out'stuff to print''。很遺憾,你也可以'選擇$ out;打印'stuff';'但不這樣做,這種方法會殺死k ittens。 'select'可以用來設置'print'和幾個特殊變量的默認句柄。不要那樣使用它。如果你在文檔中看到它,假裝你沒有。因此打開一個句柄並用'print FILEHANDLE LIST'打印或重定向你的輸出。 – daotoad 2009-10-29 15:32:57

3

運行您的代碼並閱讀錯誤消息。你有多個語法錯誤。

無論教程中,你正在閱讀不給你一些重要的建議:

#!/usr/bin/perl 

use strict; # Always, until you know when to turn it off--and then in a limited scope. 
use warnings; # see perllexwarn for why this is better than -v 

# you need to quote those bare words. qw is a handy quoting operator. See perlop. 
my @filelist = qw( 
    file1 
    file2 
    file3 
);    # added semicolon 

# use a heredoc for big chunks of text. 
# your html is seriously bogus. Fixed somewhat. 
print <<'ENDHTML'; 
<html> 
    <head> 
    </head> 
    <body> 
ENDHTML 

# $filelist does not exist. 
# fixed syntax of foreach 
foreach my $file (@filelist) { 

    # do you really want a new table for each file? 
    print "<table>\n"; 

    # need to escape your quotes 
    print "<tr><td>\"$file\"</td></tr>"; 

    # open a file handle. 
    # use lexical handles instead of old style global handles. 
    # use 3 argument open instead of 2 argument style 
    open(my $fh, '<', $file); 

    # $fh evaluates to true if we were able to open the file. 
    if ($fh) { 

    # iterate over the file. 
    while(my $line = <$fh>) { 
     # where the hell did $1 and $2 come from? 
     print "<tr><td>$line</td></tr>"; 
    } 

    } 
    else { 
     # put the error message in the table instead of the file contents 
     print "<tr><td>Error opening file: $!</td></tr>";   
    } 

    print "</table>"; 
} 
print "</html>"; 

既然你沒有測試你的代碼,我也沒有。但它應該工作。

當然,除了最簡單的可拋棄腳本之外,最好避免使用內聯HTML。如果您正在做任何持久或嚴重的事情,請使用Template :: Toolkit或HTML :: Template等模板系統。

+0

啊謝謝你。我沒有使用教程,就像我從幾年前和一些常識中記得的一樣。 正如我所說的,我習慣了AWK,其中字符串被$ 1,$ 2等引用。 – Soop 2009-10-29 15:18:42

+1

@Soop編程不是常識。首先關於規範和語法。用直覺來預測計算機程序的運作是不可能的。它比這更簡單。你得到一本書,寫一些玩具程序,至少在接受工作之前學習一下這門語言。 – 2009-10-29 15:24:16

+0

當你開始的時候,perldoc的最有價值的部分是perlfunc的Perl Functions Per category部分。 http://perldoc.perl.org/perlfunc.html#Perl-Functions-by-Category你也可以通過執行'perldoc perlfunc',或者'man perlfunc'來獲得它。你可以用'perldoc -f funcname'在cli上搜索一個函數,並用'perldoc -q someregexp'搜索FAQ。有關更多perldoc功能,請參閱「man perldoc」。 – daotoad 2009-10-29 15:26:42

4

使用HTML::Template,以便您完全控制HTML並且您的代碼不可讀。有關如何在代碼中嵌入模板的示例,請參閱my answer to another question。當然,相同的模板可以存在於腳本之外,這是將表示與邏輯分開的好處。

#!/usr/bin/perl 

use strict; use warnings; 
use autodie; 

use HTML::Template; 

my @files = qw(file1 file2); 
my @files_loop; 

for my $file (@files) { 
    open my $fh, '<', $file; 

    push @files_loop, { 
     LINES => [ 
      map { chomp; length $_ ? {LINE => $_} :() } <$fh> 
    ]}; 
} 

my $tmpl = HTML::Template->new(filehandle => \*DATA); 
$tmpl->param(FILES => \@files_loop); 
print $tmpl->output; 

__DATA__ 
<html> 
<body> 
<TMPL_LOOP FILES> 
<table> 
<TMPL_LOOP LINES><tr><td><TMPL_VAR LINE></td></tr></TMPL_LOOP> 
</table> 
</TMPL_LOOP> 
</html> 

輸出:

 
C:\Temp> fish.pl 
<html> 
<body> 

<table> 
<tr><td>bye bye</td></tr><tr><td>hello</td></tr><tr><td>thank you</td></tr><tr>< 
td>no translation</td></tr> 
</table> 

<table> 
<tr><td>chao</td></tr><tr><td>hola</td></tr><tr><td>gracias</td></tr> 
</table> 

</html> 
-1

這是我從談話你們想出了 - 個大感謝大家:d

 #!/usr/bin/perl 

    use strict;  # never forget this line 
    use warnings; # or this one, it's better than the -w flag 

    use CGI qw(:standard); # this gives us convenient functions for outputting 
          # HTML, plus other stuff for processing CGI requests 

    my @filelist=(
'file1', 
'file2', 
'file3', 
'file4', 
'file5', 
'file6', 
    ); 
    # 
    # 
    print "<html>\n"; 
    print "<head>\n"; 
    print "<link rel=\"stylesheet\" type=\"text/css\" href=\"test.css\">\n"; 
    print "</head>\n"; 
    foreach my $file (@filelist) { 
     print "<table>\n"; 
     print "<div>\n"; 
     print "<tr>\n"; 
     print td($file); 
     open(my $fh, '<', $file); 
     if ($fh) { 
      while(my $line = <$fh>) { 
      chomp $line; 
      if (substr($line, 0, 1)>="2") { 
      print "<td class=\"green\" title=\"" . $line . "\">\n"; 
      } 
      elsif (substr($line, 0, 1)=="1") { 
      print ("<td class=\"amber\" title=\"" . $line . "\">\n"); 
      } 
      elsif (substr($line, 0, 1)=="0") { 
      print ("<td class=\"red\" title=\"" . $line . "\">\n"); 
      } 
      } 
     } 
     print "</tr>\n"; 
     print "</div>\n"; 
     print "</table>\n"; 
    } 
    print "</html>"; 

的HTML需要一點點調整,而且很多是多餘的,但它工作正常:)

+0

好的,但你錯過了選擇接受答案。請這樣做。 – 2009-11-02 20:34:35

0

上面的一些優秀答案。基於Soop的回答,這次還有另外一個主題是Markapl

use strict; 
use warnings; 
use CGI(); 
use Markapl; 

my @filelist = qw/file1.txt file2.txt file3/; 

template 'web' => sub { 
    html { 
    head { 
     html_link (rel => 'stylesheet', type => 'text/css', href => 'test.css') {} 
    } 
    body { 
     table { 
     for my $file (@filelist) { 
      open my $fh, '<', $file or next; 
      row { 
      for my $title (<$fh>) { 
       chomp $title; 
       cell { $file } 
       cell (class => title_class($title), title => $title) {} 
} } } } } } }; 

print CGI::header(); 
print main->render('web'); 

sub title_class { 
    my $first_char = substr $_[0], 0, 1; 
    return 'green' if $first_char >= 2; 
    return 'amber' if $first_char == 1; 
    return 'red'; 
} 

我喜歡這些「建設者」類型的方法。對於一些更看到這太問題CL-WHO-like HTML templating for other languages?

/I3az/

相關問題