如何从Perl中的几个文件创建HTML文档?

时间:2009-10-29 14:45:14

标签: html perl

我需要一些帮助制作这个脚本...到目前为止我觉得它应该看起来像这样(我倾向于使用AWK很多)

#!/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>

中的两个strins

我是在正确的轨道上吗?

另外,我意识到我写的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"\">"
}

5 个答案:

答案 0 :(得分:8)

你的代码没有任何意义。我建议在尝试更复杂的事情之前先看一下基本的Perl intro

那就是说,你可能要做的就是这样:

#!/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模块的(大量)文档,请参阅CPAN上的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的文档。

答案 1 :(得分: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>

答案 2 :(得分: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。

答案 3 :(得分: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 /

答案 4 :(得分:-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需要稍微调整一下,其中很多都是多余的,但它运行良好:)