70000 0 0 0 0
  #   ˆl–Ðz¾”*_)ú‚fàq‘)‹FÿÂ„L¼÷ÁÀ 84    #============================================================= -*-perl-*-
#
# Template::Tutorial::Datafile
#
# DESCRIPTION

#
# AUTHOR
#   Dave Cross  <dave@dave.org.uk>
#
# COPYRIGHT
#   Copyright (C) 1996-2008 Andy Wardley.  All Rights Reserved.
#
#   This module is free software; you can redistribute it and/or
#   modify it under the same terms as Perl itself.
#
#========================================================================

=head1 NAME

Template::Tutorial::Datafile - Creating Data Output Files Using the Template Toolkit

=head1 DESCRIPTION



=head1 Introducing the Template Toolkit

There are a number of Perl modules that are universally 
recognised as The Right Thing To Use for certain tasks. If you 
accessed a database without using DBI, pulled data from the WWW 
without using one of the LWP modules or parsed XML without using 
XML::Parser or one of its subclasses then you'd run the risk of 
being shunned by polite Perl society.

I believe that the year 2000 saw the emergence of another 'must 
have' Perl module - the Template Toolkit. I don't think I'm 
alone in this belief as the Template Toolkit won the 'Best New 
Module' award at the Perl Conference last summer. Version 2.0 of 
the Template Toolkit (known as TT2 to its friends) was recently 
released to the CPAN.

TT2 was designed and written by Andy Wardley E<lt>abw@wardley.orgE<gt>. 
It was born out of Andy's previous templating module, 
Text::Metatext, in best Fred Brooks 'plan to throw one away' 
manner; and aims to be the most useful (or, at least, the most 
I<used>) Perl templating system.

TT2 provides a way to take a file of fixed boilerplate text 
(the template) and embed variable data within it. One obvious 
use of this is in the creation of dynamic web pages and this is 
where a lot of the attention that TT2 has received has been 
focussed. In this article, I hope to demonstrate that TT2 is 
just as useful in non-web applications.

=head1 Using the Template Toolkit

Let's look at how we'd use TT2 to process a simple data file. 
TT2 is an object oriented Perl module. Having downloaded it from 
CPAN and installed it in the usual manner, using it in your 
program is as easy as putting the lines

    use Template;
    my $tt = Template->new;

in your code. The constructor function, C<new>, takes 
a number of optional parameters which are documented in the 
copious manual pages that come with the module, but for the 
purposes of this article we'll keep things as simple as 
possible.

To process the template, you would call the C<process> method 
like this

    $tt->process('my_template', \%data)
        || die $tt->error;

We pass two parameters to C<process>, the first is the name of 
the file containing the template to process (in this case, 
my_template) and the second is a reference to a hash which 
contains the data items that you want to use in the template. If 
processing the template gives  any kind of error, the program 
will die with a (hopefully) useful error message.

So what kinds of things can go in C<%data>? The answer is just 
about anything. Here's an example showing data about English 
Premier League football teams.

    my @teams = ({ name   => 'Man Utd',
                   played => 16,
                   won    => 12,
                   drawn  => 3,
                   lost   => 1 },
                 { name   => 'Bradford',
                   played => 16,
                   won    => 2,
                   drawn  => 5,
                   lost   => 9 });

    my %data = ( name   => 'English Premier League',
                 season => '2000/01',
                 teams  => \@teams );

This creates three data items which can be accessed within the 
template, called C<name>, C<season> and C<teams>. Notice that 
C<teams> is a complex data structure.

Here is a template that we might use to process this data.

    League Standings

    League Name: [% name %]
    Season     : [% season %]

    Teams:
    [% FOREACH team = teams -%]
    [% team.name %] [% team.played 