For developers. Examples.
We'll make a static page "hello world" at http://your.site/hello
To do this, lets create a file
hello.sub
in modules folder and fill it the following:
#!/usr/bin/perl use strict; { '/hello' => sub { return "hello, world!"; }, };
Lets make a dynamic page "hello WHATEVER" at http://your.site/hello/WHATEVER
To do this, edit the file
hello.sub
#!/usr/bin/perl use strict; { '/hello/(\w+)' => sub { return "hello, $1!"; }, };
Now you can put any one word in the URL instead of WHATEVER word.
Creating of dynamic page, that shows the weather forecast for London city http://your.site/weather
To do this, we kindly use the RSS from BBC.co.uk site. Please, create file
weather.sub
in modules folder and write:
#!/usr/bin/perl use strict; use XML::RSS::Parser::Lite; use LWP::Simple; { '/weather' => sub { # Taking the rss feed and parsing it my $xml = get( "http://open.live.bbc.co.uk/weather/feeds/en/2643743/3dayforecast.rss" ); my $rp = new XML::RSS::Parser::Lite; $rp->parse($xml); # Template Toolkit loads weather.tpl template with parameters: rp_count - the number of sections in the rss, # rp - function with input data: section number, name of the section, output data is contents of the section $rp->parse($xml);
process( 'weather.tpl', {
rp_count=>$rp->count(),
rp=>sub{ my $it = $rp->get($_[0]); $it->get($_[1]); }
});
}, };
Please, create file
weather.tpl
in templates directory with following:
<h1>Weather in London</h1> [% WHILE rp_count>0 %] [% rp_count = rp_count - 1 %] <b>[% rp(rp_count,'title') %]</b><br /> [% rp(rp_count,'description') %]<br /> <hr /> [% END %]
Now the weather in London is displayed at http://your.site/weather
Finally, you can change the script and template to show the weather forecast in any city, you can cache information until the end of the day, and you can make multilingual weather page. Description for developers in preparation.