Perl HTML::Template サンプルプログラム

戻る

# stanadard.pl # 07.11.28 use strict; use HTML::Template; my(%t,@fld,$n,$template,@loop,$s_ref); open(IN,"names.txt") or die "Can't open the file names.txt\n"; while(<IN>){ @fld = split; next unless $fld[2]; push(@{ $t{namee} },$fld[2]); push(@{ $t{namec} },$fld[3]); } close(IN); for $n ( 0 .. $#{ $t{namee} } ) { $$s_ref{e1} = $t{namee}[$n]; $$s_ref{c1} = $t{namec}[$n]; ($s_ref) = write2html($s_ref); } sub write2html { my($s_ref) = @_; my(%t,$n); $t{e1} = lc($$s_ref{e1}); $t{inputf} = $t{e1} . '.txt'; $t{outputf} = $t{e1} . '.htm'; $t{template} = HTML::Template->new(filename => "index.html"); @loop = (); $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 ) { $t{flag} = 2; push(@{ $t{N1s} },$_); } elsif ($t{flag} == 2) { push(@{ $t{C1s} },$_); $t{flag} = 3; } elsif ($t{flag} == 3) { push(@{ $t{E1s} },$_); $t{flag} = 1; } } close(IN); for $n ( 0 .. $#{ $t{N1s} } ) { $t{N1} = $t{N1s}[$n]; $t{c1} = $t{C1s}[$n]; $t{e1} = $t{E1s}[$n]; my %row = ( N1 => $t{N1}, C1 => $t{c1}, E1 => $t{e1} ); push(@loop, \%row); } $t{template}->param(namec => $$s_ref{c1}); $t{template}->param(namee => $$s_ref{e1}); $t{template}->param(std_loop => \@loop); open(OUT,">$t{outputf}"); print OUT $t{template}->output; close(OUT); print "The output file is $t{outputf}\n"; return($s_ref); } -------------------------------------------------------------------------- <HTML> <HEAD> <TITLE><TMPL_VAR NAME="namec">(<TMPL_VAR NAME="namee">)</TITLE> <META http-equiv="Content-Type" content="text/html; charset=UTF-8"> </HEAD> <BODY bgcolor="#FFFFFF"> <center> <h2><TMPL_VAR NAME="namec">(<TMPL_VAR NAME="namee">)</h2> <a href="asme.htm">ASME</a> | <a href="bs.htm">BS</a> | <a href="din.htm">DIN</a> | <a href="gb.htm">GB</a> | <a href="iec.htm">IEC</a> | <a href="ieee.htm">IEEE</a> | <a href="iso.htm">ISO</a> <hr color="#003366"> <table width=75% align="center" border=1 cellpadding=5> <tr bgcolor="lightcyan" align="center"><th width=20%>编号</th><th width=40%>中文名称</th><th width=40%>英文名称</th></tr> <TMPL_LOOP NAME="std_loop"> <tr bgcolor="#fcf5ca" align="left"></td><td><TMPL_VAR NAME="N1"></td><td><TMPL_VAR NAME="C1"></td><td><TMPL_VAR NAME="E1"></td></tr> </TMPL_LOOP> <tr bgcolor="lightcyan" align="center"><th width=20%>编号</th><th width=40%>中文名称</th><th width=40%>英文名称</th></tr> </table> <hr color="#003366"> </center> <a href="../index.html">返回</a><br> </BODY> </HTML> -------------------------------------------------------------------------- # product.pl # 07.12.01 use strict; use HTML::Template; my(%t,@fld,$n,$template,@loop,$s_ref); open(IN,"products.txt") or die "Can't open the file products.txt\n"; while(<IN>){ @fld = split; if ( /^SH/ ) { my %row = ( code => $fld[0], ld => $fld[1], length => $fld[2], air => $fld[3] ); push(@loop, \%row); } } close(IN); $t{template} = HTML::Template->new(filename => "products0.htm"); $t{template}->param(LOOP => \@loop); open(OUT,">products.htm"); print OUT $t{template}->output; close(OUT);
戻る