Example-5 - A complete web server based RSS-client
The following example is a complete web server based RSS-Client. The XSL stylesheet from the previous example should work perfectly for the example to function. The example is slightly larger, so it has been broken down into smaller logical units. The listing here works well as either an Apache::Registry or a plain CGI application. The example also includes server side data caching - for simple low volume usage, something that a dedicated caching-proxy is better able to provide in high volume use.
Apache::Registry
The typical Perl pragma and CGI modules are loaded. The Cache::FileCache module is loaded to provide light-weight data caching. The hash %config stores configuration data, this data could easily be loaded in from one of the many configuration file modules.
Cache::FileCache
%config
The input parameters are extracted using CGI.pm and returned as a reference to a hash.
CGI.pm
The normal HTTP headers and the top of the HTML is sent to the browser using CGI. Should any error occur now, the browser should get a message, rather than a 500 error.
CGI
A cache key is created from the RSS feed URI and the name of the XSL stylesheet. A FileCache object is created. If the user requested an uncached object then the cache object and key are returned and any data stored against the cache key is removed from the cache. If the user did not request new data, then the cache is queried for data, and if any is found it is returned along with the cache object and key, otherwise only the cache key and object are returned.
FileCache
A XML::RSS::Tools object is created. It is configured for no conversion, and to use the HTTP client HTTP::Lite. The XSL stylesheet is loaded first, then the URI of the RSS feed is passed to the object. Finally transformation is performed, and the resultant XHTML returned. If any process fails then a fatal error is raised via the tad Terminate And Die method.
XML::RSS::Tools
HTTP::Lite
tad
CGI.pm is used to build the bottom of the page, and generate some navigation buttons. Note the http://www.mozilla.org/ a view-source URI prefix, you may need to remove this if your browser cannot support this construct.
view-source
In listing 7, is a generic failure handler. Any error condition is reported by CGI::Carp to the server error logs, a helpful message is sent to the user, and the %ENV hash is cleaned up.
CGI::Carp
%ENV
CGI, Apache::Registry, Cache::FileCache, CGI::Carp, HTTP::Lite
To install XML::RSS::Tools, copy and paste the appropriate command in to your terminal.
cpanm
cpanm XML::RSS::Tools
CPAN shell
perl -MCPAN -e shell install XML::RSS::Tools
For more information on module installation, please visit the detailed CPAN module installation guide.