Home arrow Perl Programming arrow Page 4 - Templating Tools

Text::Template - Perl

You may have created your own templating system in Perl to meet certain project requirements, but did you know there is a better way? This article, the first in a five-part series, explores your options. It is excerpted from chapter three of Advanced Perl Programming, Second Edition, written by Simon Cozens (O'Reilly; ISBN: 0596004567). Copyright 2007 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.

TABLE OF CONTENTS:
  1. Templating Tools
  2. Formats and Text::Autoformat
  3. Text::Autoformat
  4. Text::Template
By: O'Reilly Media
Rating: starstarstarstarstar / 2
August 07, 2008

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Mark-Jason Dominus' Text::Template has established itself as the de facto standard templating system for plain text. Its templating language is very simple indeed--anything between {and} is evaluated by Perl; everything else is left alone.

It is an object-oriented module--you create a template object from a file, filehandle, or string, and then you fill it in:

  use Text::Template;
  my $template = Text::Template->new(TYPE => "FILE",
                                     SOURCE => "email.tmpl");

  my $output = $template->fill_in();

So, let's say we've got the following template:

  Dear {$who},
     
Thank you for the {$modulename} Perl module, which has saved me
  {$hours} hours of work this year. This would have left me free to play
  { int($hours*2.4) } games of go, which I would have greatly appreciated
  had I not spent the time goofing off on IRC instead.

  Love,
  Simon

We set up our template object and our variables, and then we process the template:

  use Text::Template;
  my $template = Text::Template->new(TYPE => "FILE",
                                     SOURCE => "email.tmpl");

  $who = "Mark";
  $modulename = "Text::Template";
  $hours = 15;
  print $template->fill_in();

And the output would look like:

  Dear Mark,
     
Thank you for the Text::Template Perl module, which has saved me
  15 hours of work this year. This would have left me free to play
  36 games of go, which I would have greatly appreciated
  had I not spent the time goofing off on IRC instead.

  Love,
  Simon

Notice that the fill-in variables--$who, $modulename, and so on--are not my variables. When you think about it, this ought to be obvious--the my variables are not in Text::Template's scope, and therefore it wouldn't be able to see them. This is a bit unpleasant: Text::Template has access to your package variables, and you have to do a bit more work if you want to avoid giving use strict a fit.

Text::Template has two solutions to this. The first is pretty simple--just move the fill-in variables into a completely different package:

  use Text::Template;
  my $template = Text::Template->new(TYPE => "FILE",
                                     SOURCE => "email.tmpl");

  $Temp::who = "Mark";
  $Temp::modulename = "Text::Template"; 
  $Temp::hours = 15;
  print $template->fill_in(PACKAGE => "Temp");

That's slightly better, but it still doesn't please people for whom global variables are pure evil. If that's you, you can get around the problem by passing in a portable symbol table--that is, a hash:

  use Text::Template;
  my $template = Text::Template->new(TYPE => "FILE",
                                    
SOURCE => "email.tmpl");

  print $template->fill_in(HASH => {
     
who => "Mark",
     
modulename => "Text::Template",
     
hours => 15
 
});

Please check back next week for the continuation of this article.



 
 
>>> More Perl Programming Articles          >>> More By O'Reilly Media
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PERL PROGRAMMING ARTICLES

- Perl Turns 25
- Lists and Arguments in Perl
- Variables and Arguments in Perl
- Understanding Scope and Packages in Perl
- Arguments and Return Values in Perl
- Invoking Perl Subroutines and Functions
- Subroutines and Functions in Perl
- Perl Basics: Writing and Debugging Programs
- Structure and Statements in Perl
- First Steps in Perl
- Completing Regular Expression Basics
- Modifiers, Boundaries, and Regular Expressio...
- Quantifiers and Other Regular Expression Bas...
- Parsing and Regular Expression Basics
- Hash Functions

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: