如何从 Perl 中的多个文件创建一个 HTML 文档? [英] How can create an HTML document from several files in Perl?
问题描述
我需要一些帮助来制作这个脚本......到目前为止我认为它应该看起来像这样(我经常使用 AWK)
I need some help making this script... so far I think it should look something like this (I tend to use AWK a lot)
#!/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>
So I want the script to go to each file, print the filename, and then for each line print two strins in a <td>
我在正确的轨道上吗?
此外,我意识到我编写的 AWK 需要几个 IF 语句 - 我认为它应该是这样的
Also, I realised the AWK I wrote needed a couple of IF statements - I believe it should look like this
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"\">"
}
推荐答案
你的代码没有任何意义.我建议在尝试任何更复杂的事情之前先看一下基本的 Perl 介绍.
Your code doesn't really make any sense. I suggest taking a look at a basic Perl intro before trying anything more complicated.
也就是说,您可能正在尝试做的事情是这样的:
That said, what you're probably trying to do is something like this:
#!/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.
For the (extensive) documentation for the CGI module, see CGI at CPAN.
编辑
如果您想要表中每个文件的内容,那么您需要将每个文件读入循环内的变量中.以下是您可以如何操作的示例:
If you want the content of each file in the table, then you'll need to read each file into a variable inside the loop. Here's an example of how you could do it:
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() 教程,以及所有关于啜饮.
Some more docs that you should read: Perl open() tutorial, and all about slurping.
这篇关于如何从 Perl 中的多个文件创建一个 HTML 文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!