HTML::Mason by example BPW2007 chrisv.cpan.org 1 Saturday 27 October 2007
Dec 18, 2014
HTML::Masonby example
BPW2007chrisv.cpan.org
1Saturday 27 October 2007
embed perl in text(and then some)
2Saturday 27 October 2007
“alternatives”
• Text::Template & HTML::Template
• Template Toolkit
• Apache::ASP
• Embperl
• PHP
• ASP (ActiveState ActivePerl)
3Saturday 27 October 2007
building largedynamic websites
4Saturday 27 October 2007
• Amazon
• IMDB
• del.icio.us
• DynDNS
• jobs.perl.org
• RT
5Saturday 27 October 2007
Apache + mod_perlApache2 + mod_perl2
6Saturday 27 October 2007
httpd.conf
#PerlModule Apache2::CompatPerlModule HTML::Mason::ApacheHandler<LocationMatch "\.html$"> SetHandler perl-script PerlHandler HTML::Mason::ApacheHandler</LocationMatch>
7Saturday 27 October 2007
preload perl modulesstartup.pl
8Saturday 27 October 2007
Apache integration(objects)
9Saturday 27 October 2007
by example
10Saturday 27 October 2007
sample “component”
% my $planet = "World";Hello, <% $planet %>!
11Saturday 27 October 2007
output
Hello, World!
12Saturday 27 October 2007
Greetings, <% ("Earthlings", "Martians")[rand 2] %>
13Saturday 27 October 2007
in-line perl sections<%perl>
my $noun = 'World'; my @time = localtime;</%perl>Hello <% $noun %>,% if ( $time[2] < 12 ) {good morning.% } else {good afternoon.% }
14Saturday 27 October 2007
Mason pushes the boundaries of the term “Templating System”
15Saturday 27 October 2007
“components”:modular design
elements
16Saturday 27 October 2007
header.mas<html>
<head><title>Welcome to Wally World!</title></head><body bgcolor="#CCFFCC">
17Saturday 27 October 2007
footer.mas<center><a href="/">Home</a>
</center></body></html>
18Saturday 27 October 2007
mainpage.html<& header.mas &>
this is body text...
<& footer.mas &>
19Saturday 27 October 2007
mainpage.html<& header.mas, head => "Wally World Home" &>
this is body text...
<& footer.mas &>
20Saturday 27 October 2007
header.mas<%args>
$head</%args><html><head><title><% $head %></title></head><body bgcolor="#CCFFCC"><center><h1><% $head %></h1></center>
21Saturday 27 October 2007
autohandler<html>
<head> <title><% $m->base_comp->attr('head') %></title></head><body bgcolor="#CCFFCC"><center><h1><% $m->base_comp->attr('head') %></h1></center>% $m->call_next;<center><a href="/">Home</a></center></body></html>
22Saturday 27 October 2007
mainpage.html<%attr>
head => "Wally World Home"</%attr>this is body text...
23Saturday 27 October 2007
dhandlerse.g. http://myserver/newsfeeds/LocalNews/Story1
/newsfeeds/LocalNews/Story1 => no such thing/newsfeeds/LocalNews/dhandler => no such thing/newsfeeds/dhandler => found! (search ends)/dhandler
(The found dhandler would read “LocalNews/Story1” from $m->dhandler_arg and use it as a retrieval key into a database of stories)
24Saturday 27 October 2007
components ~ subs<%init>
my $ua = $r->header_in('User-Agent'); return ($ua =~ /Mozilla/i && $ua !~ /MSIE/i) ? 1 : 0;</%init>
25Saturday 27 October 2007
dhandlers vs autohandlers
• use an autohandler when you have a set of components to handle your pages and you want to augment them with a template/filter
• use a dhandler when you want to create a set of “virtual URLs” that don't correspond to any actual components, or to provide default behavior for a directory
26Saturday 27 October 2007
Mason Request ObjectsTwo global per-request objects are available to all components: $r (provides a Perl API to current Apache request) and $m (provides a Perl API to current Mason request)
$r->uri # the HTTP request URI$r->content_type # set or retrieve content-type
$m->caller() # returns the calling component$m->dhandler_arg() # returns url parts$m->redirect(URL, [HTTP status])
27Saturday 27 October 2007
thank you
30Saturday 27 October 2007