home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / tsw / TSW_3.4.0.exe / Apache2 / perl / Datafile.pod < prev    next >
Encoding:
Text File  |  2004-01-30  |  15.2 KB  |  462 lines

  1. #============================================================= -*-perl-*-
  2. #
  3. # Template::Tutorial::Datafile
  4. #
  5. # DESCRIPTION
  6. #   This tutorial gives an overview of the Template Toolkit, showing in
  7. #   particular how to use it to read and write data files in various
  8. #   different formats and styles. It was written by Dave Cross and
  9. #   first appeared as a lead article at http://www.perl.com/ earlier in
  10. #   the year (2001).
  11. #
  12. # AUTHOR
  13. #   Dave Cross  <dave@dave.org.uk>
  14. #
  15. # COPYRIGHT
  16. #   Copyright (C) 1996-2001 Andy Wardley.  All Rights Reserved.
  17. #   Copyright (C) 1998-2001 Canon Research Centre Europe Ltd.
  18. #
  19. #   This module is free software; you can redistribute it and/or
  20. #   modify it under the same terms as Perl itself.
  21. #
  22. # REVISION
  23. #   
  24. #
  25. #========================================================================
  26.  
  27.  
  28. #------------------------------------------------------------------------
  29. # IMPORTANT NOTE
  30. #   This documentation is generated automatically from source
  31. #   templates.  Any changes you make here may be lost.
  32. #   The 'docsrc' documentation source bundle is available for download
  33. #   from http://www.template-toolkit.org/docs.html and contains all
  34. #   the source templates, XML files, scripts, etc., from which the
  35. #   documentation for the Template Toolkit is built.
  36. #------------------------------------------------------------------------
  37.  
  38. =head1 NAME
  39.  
  40. Template::Tutorial::Datafile - Creating Data Output Files Using the Template Toolkit
  41.  
  42. =head1 DESCRIPTION
  43.  
  44. This tutorial gives an overview of the Template Toolkit, showing in
  45. particular how to use it to read and write data files in various
  46. different formats and styles. It was written by Dave Cross and first
  47. appeared as a lead article at http://www.perl.com/ earlier in the year
  48. (2001).
  49.  
  50. =head1 Introducing the Template Toolkit
  51.  
  52. There are a number of Perl modules that are universally 
  53. recognised as The Right Thing To Use for certain tasks. If you 
  54. accessed a database without using DBI, pulled data from the WWW 
  55. without using one of the LWP modules or parsed XML without using 
  56. XML::Parser or one of its subclasses then you'd run the risk of 
  57. being shunned by polite Perl society.
  58.  
  59. I believe that the year 2000 saw the emergence of another 'must 
  60. have' Perl module - the Template Toolkit. I don't think I'm 
  61. alone in this belief as the Template Toolkit won the 'Best New 
  62. Module' award at the Perl Conference last summer. Version 2.0 of 
  63. the Template Toolkit (known as TT2 to its friends) was recently 
  64. released to the CPAN.
  65.  
  66. TT2 was designed and written by Andy Wardley E<lt>abw@wardley.orgE<gt>. 
  67. It was born out of Andy's previous templating module, 
  68. Text::Metatext, in best Fred Brooks 'plan to throw one away' 
  69. manner; and aims to be the most useful (or, at least, the most 
  70. I<used>) Perl templating system.
  71.  
  72. TT2 provides a way to take a file of fixed boilerplate text 
  73. (the template) and embed variable data within it. One obvious 
  74. use of this is in the creation of dynamic web pages and this is 
  75. where a lot of the attention that TT2 has received has been 
  76. focussed. In this article, I hope to demonstrate that TT2 is 
  77. just as useful in non-web applications.
  78.  
  79. =head1 Using the Template Toolkit
  80.  
  81. Let's look at how we'd use TT2 to process a simple data file. 
  82. TT2 is an object oriented Perl module. Having downloaded it from 
  83. CPAN and installed it in the usual manner, using it in your 
  84. program is as easy as putting the lines
  85.  
  86.     use Template;
  87.     my $tt = Template->new;
  88.  
  89. in your code. The constructor function, C<new>, takes 
  90. a number of optional parameters which are documented in the 
  91. copious manual pages that come with the module, but for the 
  92. purposes of this article we'll keep things as simple as 
  93. possible.
  94.  
  95. To process the template, you would call the C<process> method 
  96. like this
  97.  
  98.     $tt->process('my_template', \%data)
  99.         || die $tt->error;
  100.  
  101. We pass two parameters to C<process>, the first is the name of 
  102. the file containing the template to process (in this case, 
  103. my_template) and the second is a reference to a hash which 
  104. contains the data items that you want to use in the template. If 
  105. processing the template gives  any kind of error, the program 
  106. will die with a (hopefully) useful error message.
  107.  
  108. So what kinds of things can go in C<%data>? The answer is just 
  109. about anything. Here's an example showing data about English 
  110. Premier League football teams.
  111.  
  112.     my @teams = ({ name   => 'Man Utd',
  113.                    played => 16,
  114.                    won    => 12,
  115.                    drawn  => 3,
  116.                    lost   => 1 },
  117.                  { name   => 'Bradford',
  118.                    played => 16,
  119.                    won    => 2,
  120.                    drawn  => 5,
  121.                    lost   => 9 });
  122.  
  123.     my %data = ( name   => 'English Premier League',
  124.                  season => '2000/01',
  125.                  teams  => \@teams );
  126.  
  127. This creates three data items which can be accessed within the 
  128. template, called C<name>, C<season> and C<teams>. Notice that 
  129. C<teams> is a complex data structure.
  130.  
  131. Here is a template that we might use to process this data.
  132.  
  133.     League Standings
  134.  
  135.     League Name: [% name %]
  136.     Season     : [% season %]
  137.  
  138.     Teams:
  139.     [% FOREACH team = teams -%]
  140.     [% team.name %] [% team.played -%] 
  141.      [% team.won %] [% team.drawn %] [% team.lost %]
  142.     [% END %]
  143.  
  144. Running this template with this data gives us the following 
  145. output
  146.  
  147.             League Standings
  148.  
  149.     League Name: English Premier League
  150.     Season     : 2000/01
  151.  
  152.     Teams:
  153.     Man Utd 16 12 3 1
  154.     Bradford 16 2 5 9
  155.  
  156. Hopefully the syntax of the template is simple enough to 
  157. follow. There are a few points to note.
  158.  
  159. =over 4
  160.  
  161. =item *
  162.  
  163. Template processing directives are written using a simple 
  164. language which is not Perl.
  165.  
  166. =item *
  167.  
  168. The keys of the C<%data> have become the names of the data 
  169. variables within the template.
  170.  
  171. =item *
  172.  
  173. Template processing directives are surrounded by C<[%> and 
  174. C<%]> sequences.
  175.  
  176. =item *
  177.  
  178. If these tags are replaced with C<[%-> C<-%]> then the preceding 
  179. or following linefeed is suppressed.
  180.  
  181. =item *
  182.  
  183. In the C<FOREACH> loop, each element of the C<teams> list was 
  184. assigned, in turn, to the temporary variable C<team>.
  185.  
  186. =item *
  187.  
  188. Each item assigned to the C<team> variable is a Perl hash. 
  189. Individual values within the hash are accessed using a dot notation.
  190.  
  191. =back
  192.  
  193. It's probably the first and last of these points which are the 
  194. most important. The first point emphasises the separation of the 
  195. data acquisition logic from the presentation logic. The person 
  196. creating the presentation template doesn't need to know Perl, 
  197. they only need to know the data items which will be passed into
  198. the template.
  199.  
  200. The last point demonstrates the way that TT2 protects the 
  201. template designer from the implementation of the data structures.
  202. The data objects passed to the template processor can be scalars,
  203. arrays, hashes, objects or even subroutines. The template 
  204. processor will just interpret your data correctly and Do The 
  205. Right Thing to return the correct value to you. In this example 
  206. each team was a hash, but in a larger system each team might be 
  207. an object, in which case C<name>, C<played>, etc. would be accessor 
  208. methods to the underlying object attributes. No changes would be 
  209. required to the template as the template processor would realise 
  210. that it needed to call methods rather than access hash values.
  211.  
  212. =head2 A more complex example
  213.  
  214. Stats about the English Football League are usually presented in
  215. a slightly more complex format than the one we used above. A 
  216. full set of stats will show the number of games that a team has 
  217. won, lost or drawn, the number of goals scored for and against 
  218. the team and the number of points that the team therefore has.
  219. Teams gain three points for a win and one point for a draw. When
  220. teams have the same number of points they are separated by the 
  221. goal difference, that is the number of goals the team has scored
  222. minus the number of team scored against them. To complicate 
  223. things even further, the games won, drawn and lost and the goals 
  224. for and against are often split between home and away games.
  225.  
  226. Therefore if you have a data source which lists the team name 
  227. togther with the games won, drawn and lost and the goals for and 
  228. against split into home and away (a total of eleven data items) 
  229. you can calculate all of the other items (goal difference, 
  230. points awarded and even position in the league). Let's take such 
  231. a file, but we'll only look at the top three teams. It will look 
  232. something like this:
  233.  
  234.     Man Utd,7,1,0,26,4,5,2,1,15,6
  235.     Arsenal,7,1,0,17,4,2,3,3,7,9
  236.     Leicester,4,3,1,10,8,4,2,2,7,4
  237.  
  238. A simple script to read this data into an array of hashes will 
  239. look something like this (I've simplified the names of the data 
  240. columns - w, d, and l are games won, drawn and lost and f and a 
  241. are goals scored for and against; h and a at the front of a data 
  242. item name indicates whether it's a home or away statistic):
  243.  
  244.     my @cols = qw(name hw hd hl hf ha aw ad al af aa);
  245.  
  246.     my @teams;
  247.     while (<>) {
  248.         chomp;
  249.  
  250.         my %team;
  251.  
  252.         @team{@cols} = split /,/;
  253.  
  254.         push @teams, \%team;
  255.     }
  256.  
  257. We can then go thru the teams again and calculate all of the 
  258. derived data items:
  259.  
  260.     foreach (@teams) {
  261.         $_->{w} = $_->{hw} + $_->{aw};
  262.         $_->{d} = $_->{hd} + $_->{ad};
  263.         $_->{l} = $_->{hl} + $_->{al};
  264.  
  265.         $_->{pl} = $_->{w} + $_->{d} + $_->{l};
  266.  
  267.         $_->{f} = $_->{hf} + $_->{af};
  268.         $_->{a} = $_->{ha} + $_->{aa};
  269.  
  270.         $_->{gd} = $_->{f} - $_->{a};
  271.         $_->{pt} = (3 * $_->{w}) + $_->{d};
  272.     }
  273.  
  274. And then produce a list sorted in descending order:
  275.  
  276.     @teams = sort { 
  277.     $b->{pt} <=> $b->{pt} || $b->{gd} <=> $a->{gd} 
  278.     } @teams;
  279.  
  280. And finally add the league position data item:
  281.  
  282.     $teams[$_]->{pos} = $_ + 1 
  283.         foreach 0 .. $#teams;
  284.  
  285. Having pulled all of our data into an internal data structure 
  286. we can start to produce output using out templates. A template 
  287. to create a CSV file containing the data split between home and 
  288. away stats would look like this:
  289.  
  290.     [% FOREACH team = teams -%]
  291.     [% team.pos %],[% team.name %],[% team.pl %],[% team.hw %],
  292.     [%- team.hd %],[% team.hl %],[% team.hf %],[% team.ha %],
  293.     [%- team.aw %],[% team.ad %],[% team.al %],[% team.af %],
  294.     [%- team.aa %],[% team.gd %],[% team.pt %]
  295.     [%- END %]
  296.  
  297. And processing it like this:
  298.  
  299.     $tt->process('split.tt', { teams => \@teams }, 'split.csv')
  300.       || die $tt->error;
  301.  
  302. produces the following output:
  303.  
  304.     1,Man Utd,16,7,1,0,26,4,5,2,1,15,6,31,39
  305.     2,Arsenal,16,7,1,0,17,4,2,3,3,7,9,11,31
  306.     3,Leicester,16,4,3,1,10,8,4,2,2,7,4,5,29
  307.  
  308. Notice that we've introduced the third parameter to C<process>. 
  309. If this parameter is missing then the TT2 sends its output to 
  310. C<STDOUT>. If this parameter is a scalar then it is taken as the 
  311. name of a file to write the output to. This parameter can also be 
  312. (amongst other things) a filehandle or a reference to an object w
  313. hich is assumed to implement a C<print> method.
  314.  
  315. If we weren't interested in the split between home and away games, 
  316. then we could use a simpler template like this:
  317.  
  318.     [% FOREACH team = teams -%]
  319.     [% team.pos %],[% team.name %],[% team.pl %],[% team.w %],
  320.     [%- team.d %],[% team.l %],[% team.f %],[% team.a %],
  321.     [%- team.aa %],[% team.gd %],[% team.pt %]
  322.     [% END -%]
  323.  
  324. Which would produce output like this:
  325.  
  326.     1,Man Utd,16,12,3,1,41,10,6,31,39
  327.     2,Arsenal,16,9,4,3,24,13,9,11,31
  328.     3,Leicester,16,8,5,3,17,12,4,5,29
  329.  
  330. =head1 Producing XML
  331.  
  332. This is starting to show some of the power and flexibility of 
  333. TT2, but you may be thinking that you could just as easily produce 
  334. this output with a C<foreach> loop and a couple of C<print> 
  335. statements in your code. This is, of course, true; but that's 
  336. because I've chosen a deliberately simple example to explain the 
  337. concepts. What if we wanted to produce an XML file containing the 
  338. data? And what if (as I mentioned earlier) the league data was held 
  339. in an object? The code would then look even easier as most of the code 
  340. we've written earlier would be hidden away in C<FootballLeague.pm>.
  341.  
  342.     use FootballLeague;
  343.     use Template;
  344.  
  345.     my $league = FootballLeague->new(name => 'English Premier');
  346.  
  347.     my $tt = Template->new;
  348.  
  349.     $tt->process('league_xml.tt', { league => $league })
  350.         || die $tt->error;
  351.  
  352. And the template in C<league_xml.tt> would look something like this:
  353.  
  354.     <?xml version="1.0"?>
  355.     <!DOCTYPE LEAGUE SYSTEM "league.dtd">
  356.  
  357.     <league name="[% league.name %]" season="[% league.season %]">
  358.     [% FOREACH team = league.teams -%]
  359.       <team name="[% team.name %]"
  360.             pos="[% team.pos %]"
  361.             played="[% team.pl %]"
  362.             goal_diff="[% team.gd %]"
  363.             points="[% team.pt %]">
  364.          <stats type="home">
  365.             win="[% team.hw %]"
  366.             draw="[%- team.hd %]"
  367.             lose="[% team.hl %]"
  368.             for="[% team.hf %]"
  369.             against="[% team.ha %]" />
  370.          <stats type="away">
  371.             win="[% team.aw %]"
  372.             draw="[%- team.ad %]"
  373.             lose="[% team.al %]"
  374.             for="[% team.af %]"
  375.             against="[% team.aa %]" />
  376.       </team>
  377.     [% END -%]
  378.     &/league>
  379.  
  380. Notice that as we've passed the whole object into C<process> then 
  381. we need to put an extra level of indirection on our template 
  382. variables - everything is now a component of the C<league> variable. 
  383. Other than that, everything in the template is very similar to what 
  384. we've used before. Presumably now C<team.name> calls an accessor 
  385. function rather than carrying out a hash lookup, but all of this 
  386. is transparent to our template designer.
  387.  
  388. =head1 Multiple Formats
  389.  
  390. As a final example, let's suppose that we need to create output
  391. football league tables in a number of formats. Perhaps we are 
  392. passing this data on to other people and they can't all use the 
  393. same format. Some of our users need CSV files and others need 
  394. XML. Some require data split between home and away matches and 
  395. other just want the totals. In total, then, we'll need four 
  396. different templates, but the good news is that they can use the 
  397. same data object. All the script needs to do is to establish 
  398. which template is required and process it.
  399.  
  400.     use FootballLeague;
  401.     use Template;
  402.  
  403.     my ($name, $type, $stats) = @_;
  404.  
  405.     my $league = FootballLeague->new(name => $name);
  406.  
  407.     my $tt = Template->new;
  408.  
  409.     $tt->process("league_${type}_$stats.tt", 
  410.                  { league => $league }
  411.                  "league_$stats.$type")
  412.         || die $tt->error;
  413.  
  414. For example, you can call this script as
  415.  
  416.     league.pl 'English Premier' xml split
  417.  
  418. This will process a template called C<league_xml_split.tt> 
  419. and put the results in a file called C<league_split.xml>.
  420.  
  421. This starts to show the true strength of the Template Toolkit. 
  422. If we later wanted to add another file format - perhaps we 
  423. wanted to create a league table HTML page or even a LaTeX 
  424. document - then we would just need to create the appropriate 
  425. template and name it according to our existing naming 
  426. convention. We would need to make no changes to the code.
  427.  
  428. I hope you can now see why the Template Toolkit is fast becoming
  429. an essential part of many people's Perl installation.
  430.  
  431. =head1 AUTHOR
  432.  
  433. Dave Cross E<lt>dave@dave.org.ukE<gt>
  434.  
  435.  
  436.  
  437.  
  438. =head1 VERSION
  439.  
  440. Template Toolkit version 2.13, released on 30 January 2004.
  441.  
  442. =head1 COPYRIGHT
  443.  
  444.  
  445. Copyright (C) 2001 Dave Cross E<lt>dave@dave.org.ukE<gt>
  446.  
  447. This module is free software; you can redistribute it and/or
  448. modify it under the same terms as Perl itself.
  449.  
  450.  
  451.  
  452. =cut
  453.  
  454. # Local Variables:
  455. # mode: perl
  456. # perl-indent-level: 4
  457. # indent-tabs-mode: nil
  458. # End:
  459. #
  460. # vim: expandtab shiftwidth=4:
  461.