Home arrow Perl Programming arrow Page 4 - Build a Perl RSS Aggregator with Templating Tools

Basic Dynamism - Perl

In this third part of a five-part series on templating tools, you'll learn how to write a simple RSS aggregator, 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.

TABLE OF CONTENTS:
  1. Build a Perl RSS Aggregator with Templating Tools
  2. RSS Aggregation
  3. HTML::Mason
  4. Basic Dynamism
By: O'Reilly Media
Rating: starstarstarstarstar / 2
August 21, 2008

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

So where does the templating come in? There are three basic ways of adding templates to Mason pages. Here's the first, a simple modification to our Footer component.

   <hr>
   <div class="footer">
     <address>
        <a href="mailto:webmaster@yourcompany.com"> webmaster@yourcompany.com</a>
     </address>
     Generated: <% scalar localtime %>
  
</div>
  </body>
 </html>

If you wrap some Perl code in <% ... %> tags, the result of the Perl expression is inserted into the resulting HTML.

That's all very well for simple expressions, but what about actual Perl logic? For this, Mason has an ugly hack: a single % at the beginning of a line is interpreted as Perl code. This lets you do things like Example 3-4, to dump out the contents of a hash.

Example 3-4. Hashdump

<table>
 
<tr>
    <th> key </th>
    <th>value</th>
 
</tr>

% for (keys %hash) {
 
<tr>
    <td> <% $_ %> </td>
    <td> <% $hash{$_} %> </td>
  </tr>
% }
</table>
<%ARGS>
%hash => undef
</%ARGS>

There's a few things to notice in this example. First, see how we intersperse ordinary HTML with logic, using % ... , and evaluated Perl expressions, using
<% ... %>. The only places % is special are at the start of a line and as part of the <% ... %> tag; the % of %hash is plain Perl.

The second thing to notice in the example is how we get the hash into the component in the first place. That's the purpose of the <%ARGS> section--it declares arguments to pass to the component. And how do we pass in those arguments? Here's something that might call Hashdump:

  % my %foo = ( one => 1, two => 2 );

  <& /Hashdump, hash => %foo &>

So altogether, we have an example of declaring my variables inside a component, passing a named parameter to another component, and having that component receive the parameter and make use of it. Mason will try to do something sensible if you pass parameters of different types than the types you've declared in the <%ARGS> section of the receiving component (here we passed a hash to fill in the %hash parameter, for instance), but life is easier if you stick to the same types.

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



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