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.
1: Main core of application
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.
2: Processing Input parameters
The input parameters are extracted using
CGI.pm and returned as a reference to a hash.
3: Sending the HTTP headers and starting the HTML
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.
4: Creating the Cache object
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.
5: Processing the RSS Feed
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.
6: End the HTML
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.
7: Something went wrong, report the error and clean up
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.