Home arrow Perl Programming arrow Page 3 - Looping, Security, and Templating Tools

Text::Template Tricks - Perl

In this second part of a five-part series on templating tools, you'll learn about loops, arrays, hashes, and more. It is excerpted from chapter three of the book 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.

  1. Looping, Security, and Templating Tools
  2. Security and Error Checking
  3. Text::Template Tricks
  4. HTML::Template
By: O'Reilly Media
Rating: starstarstarstarstar / 3
August 14, 2008

print this article



Using { and } to delimit code is fine for most uses of Text::Template--when you're generating form letters or emails, for instance. But what if you're generating text that makes heavy use of { and }-- HTML pages including JavaScript, for example, or TEX code for typesetting?

One solution is to escape the braces that you don't want to be processed as Perl snippets with backslashes:

  if (browser == "Opera") \{

However, as one user pointed out, if you're generating TeX, which attaches meaning to backslashes and braces, you're entering a world of pain:

  \\textit\{ {$title} \} \\dotfill \\textbf\{ \\${$cost} \}

A much nicer solution would be to specify alternate delimiters, and get rid of the backslash escaping:

  \textit{ [[[ $title ]]] } \dotfill \textbf{ [[[ $cost ]]] }

Much clearer!

To do this with Text::Template, use the DELIMITERS option on either the constructor or the fill_in method:

  print $template->fill_in(DELIMITERS => [ '[[[', ']]]' ]);

This actually runs faster than the default because it doesn't do any special backslash processing, but needless to say, you have to ensure that your delimiters do not appear in the literal text of your template.

Mark suggests a different trick if this isn't appropriate: use Perl's built-in quoting operators to escape the braces. If we have a program fragment
{ q{ Hello } }, this returns the string "Hello" and inserts it into the template output. So another way to get literal text without escaping the braces is simply to add more braces!

  { q{

   if (browser == "Opera") { ... }

  } }

Another problem is that your fingers fall off from typing:

  my $template = new Text::Template(...);

all the time. The object-oriented style is perfect when you have a template that you need to fill in hundreds of times--a form letter, for instance--but not so great if you're just filling it in once. For these cases, Text::Template can export a subroutine, fill_in_file. This does the preparation and filling in all in one go:

  use Text::Template qw(fill_in_file);

  print fill_in_file("email.tmpl", PACKAGE => "Q", ...);

Note that you do have to import this function specifically.

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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- 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: