Perl
  Home arrow Perl arrow Page 3 - 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

Templating Tools
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 2
    2008-08-07


    Table of Contents:
  • Templating Tools
  • Formats and Text::Autoformat
  • Text::Autoformat
  • Text::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


    Templating Tools - Text::Autoformat
    ( Page 3 of 4 )

    There's a more 21st century way to deal with formatting, however, and that's the Text::Autoformat module. This has two main purposes--it wraps text more sensitively than the usual Text::Wrap module or the Unix fmt command, and it provides a syntactically simpler but more featureful replacement for the built-in format language.

    Text::Autoformat's text wrapping capabilities are only tangentially related to templating, but they're still worth mentioning here.

    The idea behind autoformat is to solve the problem of wrapping structured text; it was created specifically for email messages (with special consideration for quoted text, signatures, etc.), but it's applicable to any structured textual data. For instance, given the text:

      You have:
         * a splitting headache 
         *no tea 
         * your gown (being worn)
                  It looks like your gown contains:
             . a thing your aunt gave you which you don't know what it is
             . a buffered analgesic
             . pocket fluff

    fmt fails rather spectacularly:

      You have:
          
    * a splitting headache * no tea * your gown
          (being worn)
           
    It looks like your gown contains:
              . a thing your aunt gave you which
              you don't know what it is . a buffered
              analgesic . pocket fluff

    In this case, the autoformat subroutine does things a lot better, as it looks ahead at the structure of the text it's formatting:

      You have:
          
    * a splitting headache
          * no tea
          
    * your gown (being worn) It looks like your
            gown contains:
              . a thing your aunt gave you which you
                
    don't know what it is
              . a buffered analgesic
              . pocket fluff

    Text::Autoformat's format language is quite similar to Perl's native one, but with some simplifications. First, the distinction between filling @ fields and continuing ^ fields is made by the choice of picture character, not the prefix to the field. Hence, what was:

      @<<<< @<<<<<<<<< @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< @<<<<<<<<<<<<<<<<<<<

    now simply becomes:

      <<<<< <<<<<<<<<< <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< <<<<<<<<<<<<<<<<<<<<<

    For continuation formats, you now use [ and ] , which repeat as necessary on subsequent lines:

    Id      : <<<<<
    Message : 
            [[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[
    [[[[[[[[[[[[[[[[[[[[[[[[[[[[[[

    This will produce output like the following:

      Id      :    1
      Message :
              Hi Simon, Thank you for the supply of widgets that you sent me
              last week. I can assure you that they have all been put to good...

    Unlike Perl's built-in continuation formats, however, be aware that the [and] lines repeat the entire format time and time again until the variable is completely printed out. So this, for instance, won't do what you expect:

      Id    : <<<<< [[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[
    [[[[[[[[[[[[[[[[[[[[

    Instead, it'll produce output something like this:

    Id

    :

    1

    Hi Simon, Thank you for the supply of widgets that you sent

    Id

    :

     

    me last week. I can assure you that they have all been put

    Id

    :

     

    to good use, and have been found, as usual to be the very...

    with even more spectacularly bad results for formats longer than one line.

    One big advantage, though, is that with Text::Autoformat, formats are just plain strings instead of cleverly compiled patterns interleaved with code. These strings are processed with the form function, which needs to be exported specifically:

      use Text::Autoformat qw(form);

      my $format = <<EOF;
     
    Id      : <<<<<
      Date    : <<<<<<<<
      From    : <<<<<<<<<<<<<<<<<<<<<
      Subject : <<<<<<<<<<<<<<<<<<<<<...
      EOF
      my $id = 10;
      my $date = "20/12/02";
      my $from = "Fred Foonly";
      my $subject = "Autoformatted message";
      print form($format, $id, $date, $from, $subject);

    Text::Autoformat also provides extremely flexible control over the hyphenation of form fields in a multi-line block, including the ability to plug in other hyphenation routines such as Jan Pazdziora's TeX::Hyphen, the hyphenation algorithm used in Donald Knuth's TeX package. The main disadvantage, however, is that you don't get the same control over headers and footers as you would with write.

    Both Perl formats and Text::Autoformat are great for producing formatted output in the style of 1980s form-based programs, but when people think of forms these days, they're more likely to think of things like form letters. Let's move on to look at modules that are more suited to this style of templating.



     
     
    >>> 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 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek