Perl
  Home arrow Perl arrow Page 4 - Build a Perl RSS Aggregator with 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? 
PERL

Build a Perl RSS Aggregator with Templating Tools
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 2
    2008-08-21


    Table of Contents:
  • Build a Perl RSS Aggregator with Templating Tools
  • RSS Aggregation
  • HTML::Mason
  • Basic Dynamism

  • 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


    Build a Perl RSS Aggregator with Templating Tools - Basic Dynamism
    ( Page 4 of 4 )

    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 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 5 Hosted by Hostway
    Stay green...Green IT