Now I'd like to replace the push @urls sections with a subroutine, to avoid evil copy-and-paste, since the real scripts have several url hashes each.
So I read chapter 8 of The Llama and chapter seven of Elements of Programming with Perl. Lotsa good info which I pretty much grasp. But I'm suffering a severe case of flatulo-cephalism on applying this newfound knowledge to minimized code below.
Two things that are particulary baffling:
print header, $template->output;#!/usr/bin/perl -wT use strict; use HTML::Template; use CGI qw(:all); use CGI::Carp qw(fatalsToBrowser); # debugging only - for production +"use CGI::Carp;" use vars qw(@urlsA @urlsB); my $template = HTML::Template->new(filename => "template.tmpl"); $template-> param( servname => $ENV{'SERVER_NAME'}, ); my %urlhashA = ( 'Home' => '/', 'Icons' => '/icons/', 'Sitedocs' => '/doc/', ); my %urlhashB = ( 'Cisco' => 'http://www.cisco.com/', 'CPAN' => 'http://search.cpan.org/', 'Google' => 'http://www.google.com/', 'Perl Monks' => 'http://www.perlmonks.org/', ); while (my ($name,$url) = each %urlhashA) { push @urlsA, {nameA=>$name, urlA=>$url} } $template->param(urlloopA => \@urlsA); while (my ($name,$url) = each %urlhashB) { push @urlsB, {nameB=>$name, urlB=>$url} } $template->param(urlloopB => \@urlsB);
(template.tmpl)
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <TITLE><!-- tmpl_var name=servname --></TITLE> </HEAD> <BODY> <!-- tmpl_loop name="urlloopA" --> <A HREF="<!-- tmpl_var name="urlA" -->"> <B><!-- tmpl_var name="nameA" --></B> </A> <!-- /tmpl_loop --> <P> <!-- tmpl_loop name="urlloopB" --> <A HREF="<!-- tmpl_var name="urlB" -->"> <!-- tmpl_var name="nameB" --> </A> <BR> <!-- /tmpl_loop --> </BODY> </HTML>
In reply to Got those HTML::Template and subroutine blues by ybiC
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |