利用HTML::Template模块
戻る
生成互相链接的复数个HTML文件
# 通过这个程序,把几百行的数据生成HMLT表格
#  multi.pl
use strict;
use HTML::Template;
my(%t,@fld,$n,$template,@loop);
print "Please input filename=";
chop($t{root}=);
$t{tmpl} = 'index.html';
$t{inputf} = $t{root} . '.txt';
open(IN,"names.txt") or die "Can't open the file names.txt.\n";
while(){
	if ( /^NAME\s/ ) {
		@fld = split;
		$t{list}{$fld[1]} = $fld[2];
	}
}
close(IN);
$template = HTML::Template->new(filename => $t{tmpl});
@loop = ();
$t{htmfile} = $t{root} . '.htm';
$t{flag} = 1;
open(IN,"$t{inputf}") or die "Can't open the file $t{inputf}";
while(){
    next if $. == 1;          # 跳过第一行
    next if length($_) < 2;   # 最后的空行也跳过
    if ( $t{flag} == 1 ) {    # 第一组数据
        $t{flag} = 2;
        push(@{ $t{N1s} },$_);
		$t{N11} = $_;
    } elsif ($t{flag} == 2) {  # 第二组数据
		$t{clist}{$t{N11}} = $_;
        $t{flag} = 3;
    } elsif ($t{flag} == 3) {  # 第三组数据
		$t{elist}{$t{N11}} = $_;
        $t{flag} = 1;
    } 
}
close(IN);
# 按第一组数据排序
@{ $t{NN} } = sort {lc($a) cmp lc($b)} @{ $t{N1s} };
# 为了检查输入数据的错误,第一次运行是最好不排序
#@{ $t{NN} } = @{ $t{N1s} };
for $n ( 0 .. $#{ $t{NN} } ) {
    $t{N1} = $t{NN}[$n];
    $t{c1} = $t{clist}{$t{N1}};
    $t{e1} = $t{elist}{$t{N1}};
	$t{count}{$t{N1}}++;
	if ( $t{count}{$t{N1}} > 1 ) {  # 这个操作是为了防止重复
		next;
	}
    my %row = (
            N1 => $t{N1},
            C1 => $t{c1},
            E1 => $t{e1}
    );
    push(@loop, \%row);
}
$t{etitle} = uc($t{root});
$template->param(std_loop => \@loop);
$template->param(ename => $t{etitle});
$template->param(cname => $t{list}{$t{etitle}});
open(OUT,">$t{htmfile}");
print OUT $template->output;
close(OUT);
print "The output file is $t{htmfile}\n";
__END__;
filename:names.txt
NAME ANSI 美国
NAME BS   英国
NAME DIN  德国
NAME EN   欧洲
NAME GB   中国
NAME ISO  ISO
NAME JIS  日本
NAME NF   法国
一气生成数百个HTML文件
#  make_html.pl
use strict;
use HTML::Template;
my(%t,@fld,$n,$template,@loop,$h_ref);
print "Please input the directory name=";
chop($t{root}=);
$$h_ref{dir} = 'vF' . $t{root};
$t{inputf} = $t{root} . '_vF.csv';
open(IN,"./$$h_ref{dir}/$t{inputf}") or die "Can't open the file /$$h_ref{dir}/$t{inputf}.\n";
while(){
	next if ( $. == 1 );
	chop;
	@fld = split(/,/);
	next unless $fld[1];
	$t{T1} = sprintf("%10.6f",$fld[0]);
	push(@{ $$h_ref{Time} },$t{T1});
	push(@{ $$h_ref{k_files} },$fld[1]);
	push(@{ $$h_ref{Start} },$fld[2]);
}
close(IN);
$t{tmpl} = 'output0.htm';
$t{htmfile} = 'index.html';
$template = HTML::Template->new(filename => $t{tmpl});
opendir(DIR,"$$h_ref{dir}") or die "Can't opendir $$h_ref{dir}: $!";
@loop = ();
$t{N1} = 0;
for $n ( 0 .. $#{ $$h_ref{Time} } ) {
    $t{N1}++;
	$t{Time1} = $$h_ref{Time}[$n];
	$t{file1} = $$h_ref{k_files}[$n];
	$t{Start1} = $$h_ref{Start}[$n];
	$t{csv1} = '' . $t{file1} . '';
	$t{file1} =~ s/csv/xls/;
	$t{xls1} = '' . $t{file1} . '';
	$t{file1} =~ s/xls/htm/;
	$t{gif1} = '' . $t{file1} . '';
    my %row = (
            N1 => $t{N1},
            Time => $t{Time1},
            csv => $t{csv1},
            xls => $t{xls1},
            gif => $t{gif1},
            Start => $t{Start1}
    );
    push(@loop, \%row);
}
$template->param(loop => \@loop);
$template->param(dir => $$h_ref{dir});
open(OUT,">./$$h_ref{dir}/$t{htmfile}");
print OUT $template->output;
close(OUT);
# 这个循环可一气生成指定数目的HTML文件
for $n ( 0 .. $#{ $$h_ref{Time} } ) {
	$$h_ref{file1} = $$h_ref{k_files}[$n];
	($h_ref) = make_vhtm($h_ref);
}
close(IN1);
print "Finished.\n";
sub make_vhtm {
	my($h_ref) = @_;
	my(%t,$n,$template1,@loop);
	$$h_ref{file1} =~ s/csv/htm/;
	$t{htmfile} = $$h_ref{file1};
	$template1 = HTML::Template->new(filename => "v000000.htm");
	$template1->param(htm => $t{htmfile});
	$$h_ref{file1} =~ s/htm/gif/;
	$template1->param(gif => $$h_ref{file1});
	open(OUT1,">./$$h_ref{dir}/$t{htmfile}");
	print OUT1 $template1->output;
	close(OUT1);
	return($h_ref);
}
1;
__END__;
错误信息
$template = HTML::Template->new(filename => $$h_ref{tmpl},option => "$$h_ref{NO}");
-------------------------------------
Please input the directory name=1_2_1
The output file is ./vF1_2_1/index.html
HTML::Template->new() called with odd number of option parameters - should be of
 the form option => value at make_html.pl line 78
戻る