Beefy Boxes and Bandwidth Generously Provided by pair Networks RobOMonk
Welcome to the Monastery
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
A couple of warnings on the use of Text::Template. If you use strict like a good Perl programmer, Text::Template will not see your variables as you might expect. Since templates are opened in new scopes by Text::Template, this behavior makes perfect sense, but is rather annoying.

You have two options available when you run into this problem:

  1. As mirod mentions, declare and initialize your variables in a separate package. Personally, I don't think this is very elegant.
  2. Pass your Text::Template object a hash reference with the variables to replace. On the plus side, this allows you to control what information is available to templates. On the minus side, you have to go to the trouble of building another hash. (Of course, if you've already built it, then this isn't an issue.)
Truthfully, neither option is that much of a problem in most situations. Just be aware.

Also, I found that Text::Template doesn't behave exactly as you might expect in dereferencing variables. For instance, take the following code as an example:

my %vars = ('measurements' => {'temperature' => {'current' => '22 degC +', 'high' => '26 degC', 'low' => '18 degC' }, 'humidity' => '82%' }, 'time' => scalar(localtime) ); my $tt = Text::Template->new(SOURCE => $templateFile) or die "Couldn't + construct template: $Text::Template::ERROR"; my $weather = $tt->fill_in(HASH => \%vars);
Assuming your template delimiters are the default ({ and }), you might expect to use {$measurements->{'temperature'}->{'current'}} to print the current temperature in your template. I had to use {$measurements{'temperature'}{'current'}} instead, and I'm not entirely sure why. (If any monks have an idea, please let me know.) This was with version 1.41 (current as of 2001-10-22); I have emailed the author with the question and am awaiting a response.

With all that said, Text::Template is a great module for removing more of the display element from your code. For my scripts, I probably would have gone with either HTML::Template or Template Toolkit if they had been readily available.

Update: I overlooked one important section of the documentation. As Dominus politely pointed out in an email, the HASH section explains how the templates are filled. Indeed,

If the value is a reference to an array, then @key is set to that array. If the value is a reference to a hash, then %key is set to that hash. Similarly if value is any other kind of reference.
I always manage to miss the important parts of documentation. :-)

--PotPieMan


In reply to Re: Text::Template by PotPieMan
in thread Text::Template by mirod

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others drinking their drinks and smoking their pipes about the Monastery: (5)
    As of 2014-04-23 19:46 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      April first is:







      Results (553 votes), past polls