Perl
  Home arrow Perl arrow Page 3 - Looping, Security, and Templating Tools
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
PERL

Looping, Security, and Templating Tools
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 3
    2008-08-14


    Table of Contents:
  • Looping, Security, and Templating Tools
  • Security and Error Checking
  • Text::Template Tricks
  • HTML::Template

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Looping, Security, and Templating Tools - Text::Template Tricks
    ( Page 3 of 4 )

    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(...);
      $template->fill_in();

    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 Articles          >>> More By O'Reilly Media
     

       

    PERL ARTICLES

    - More Perl Bits
    - Perl, Bit by Bit
    - Basic Charting with Perl
    - Using Getopt::Long: More Command Line Option...
    - Command Line Options in Perl: Using Getopt::...
    - Web Access with LWP
    - More Templating Tools for Perl
    - Site Layout with Perl Templating Tools
    - Build a Perl RSS Aggregator with Templating ...
    - Looping, Security, and Templating Tools
    - Perl: Bon Voyage Lists and Hashes
    - Templating Tools
    - Perl: Number Crunching
    - Perl Debuggers in Detail
    - Debugging Perl





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek