利用HTML::Template模塊

戻る

生成互相鏈接的復(fù)數(shù)個(gè)HTML文件

# 通過這個(gè)程序,把幾百行的數(shù)據(jù)生成HMLT表格 # multi.pl use strict; use HTML::Template; my(%t,@fld,$n,$template,@loop); print "Please input filename="; chop($t{root}=<STDIN>); $t{tmpl} = 'index.html'; $t{inputf} = $t{root} . '.txt'; open(IN,"names.txt") or die "Can't open the file names.txt.\n"; while(<IN>){ 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(<IN>){ next if $. == 1; # 跳過第一行 next if length($_) < 2; # 最后的空行也跳過 if ( $t{flag} == 1 ) { # 第一組數(shù)據(jù) $t{flag} = 2; push(@{ $t{N1s} },$_); $t{N11} = $_; } elsif ($t{flag} == 2) { # 第二組數(shù)據(jù) $t{clist}{$t{N11}} = $_; $t{flag} = 3; } elsif ($t{flag} == 3) { # 第三組數(shù)據(jù) $t{elist}{$t{N11}} = $_; $t{flag} = 1; } } close(IN); # 按第一組數(shù)據(jù)排序 @{ $t{NN} } = sort {lc($a) cmp lc($b)} @{ $t{N1s} }; # 為了檢查輸入數(shù)據(jù)的錯(cuò)誤,第一次運(yùn)行是最好不排序 #@{ $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 ) { # 這個(gè)操作是為了防止重復(fù) 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 美國(guó) NAME BS 英國(guó) NAME DIN 德國(guó) NAME EN 歐洲 NAME GB 中國(guó) NAME ISO ISO NAME JIS 日本 NAME NF 法國(guó) <table width=75% align="center" border=1 cellpadding=5> <tr bgcolor="#3399FF" align="center"><th width=20%>編號(hào)</th><th width=40%>中文名稱</th><th width=40%>英文名稱</th></tr> <TMPL_LOOP NAME="std_loop"> <tr bgcolor="lightcyan" align="left"><td><TMPL_VAR NAME="N1"></td><td><TMPL_VAR NAME="C1"></td><td><TMPL_VAR NAME="E1"></td></tr> </TMPL_LOOP> </table>

一氣生成數(shù)百個(gè)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}=<STDIN>); $$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(<IN>){ 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} = '<a href="' . $t{file1} . '">' . $t{file1} . '</a>'; $t{file1} =~ s/csv/xls/; $t{xls1} = '<a href="' . $t{file1} . '">' . $t{file1} . '</a>'; $t{file1} =~ s/xls/htm/; $t{gif1} = '<a href="' . $t{file1} . '">' . $t{file1} . '</a>'; 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); # 這個(gè)循環(huán)可一氣生成指定數(shù)目的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__; 錯(cuò)誤信息 $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
戻る
宝应县| 图片| 勐海县| 屏东市| 达孜县| 循化| 介休市| 武义县| 札达县| 郴州市| 荔浦县| 贞丰县| 道真| 开平市| 丽水市| 五莲县| 静海县| 进贤县| 靖西县| 永定县| 连平县| 台山市| 江川县| 汽车| 黔西| 衢州市| 灌阳县| 若羌县| 高邑县| 焦作市| 合肥市| 花莲县| 施秉县| 盐津县| 桑植县| 麻江县| 始兴县| 田东县| 霍山县| 康马县| 前郭尔|