• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
My four preferences in Perl Web practices
 

My four preferences in Perl Web practices

on

  • 1,836 views

 

Statistics

Views

Total Views
1,836
Views on SlideShare
1,832
Embed Views
4

Actions

Likes
0
Downloads
12
Comments
0

2 Embeds 4

http://www.slideshare.net 3
http://localhost 1

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    My four preferences in Perl Web practices My four preferences in Perl Web practices Presentation Transcript

    • My Four Preferences
    • My Four Preferences in my Perl Web practice
    • Architecture and music
    • Danube
    • My Four Preferences in my Perl Web practice
    • 1. Parsing URLs with grammar 2. .ini configuration files 3. WWW::Page 4. XSLT
    • 0. Perl 5.10
    • 1. Parsing URLs with grammar
    • URL is . . .
    • URL is . . . a string
    • URL is . . . a string, a set of parts
    • URL is . . . a string, a set of parts, a container of parameters
    • Blog URL scheme
    • Blog URL scheme /
    • Blog URL scheme / /message‐uri/
    • Blog URL scheme / /message‐uri/ /tag/tag‐name/
    • Blog URL scheme / /message‐uri/ /tag/tag‐name/ /post/
    • Blog URL scheme / /message‐uri/ /tag/tag‐name/ /post/ /message‐uri/post/
    • Blog URL scheme / /message‐uri/ /tag/tag‐name/ /post/ /message‐uri/post/ /message‐uri/comments/
    • How to parse?
    • How to parse? RewriteRules in .htaccess
    • How to parse? RewriteRules in .htaccess ^$ /index.pl?page=home
    • How to parse? RewriteRules in .htaccess ^$ /index.pl?page=home ^post/?$ /post.pl
    • How to parse? RewriteRules in .htaccess ^$ /index.pl?page=home ^post/?$ /post.pl ^([^/]+)/? /index.pl?page=$1
    • How to parse? RewriteRules in .htaccess ^$ /index.pl?page=home ^post/?$ /post.pl ^([^/]+)/? /index.pl?page=$1 etc.
    • How to parse? RewriteRules in .htaccess Bad: involves programming outside Perl
    • How to parse? Regular expressions
    • How to parse? Regular expressions given ($uri) {    when (/^/$/) {...} }
    • How to parse? Regular expressions given ($uri) {    when (/^/$/) {...}    when (/^/post/?/) {...} }
    • How to parse? Regular expressions given ($uri) {    when (/^/$/) {...}    when (/^/post/?/) {...}    # (regexes as in .htaccess) }
    • How to parse? Regular expressions Bad: rules are not obvious
    • How to parse? split builtin
    • How to parse? split builtin @parts = split m{/}, $uri; given (scalar @parts) {    when (1) {...}    when (2) {...} }
    • How to parse? split builtin Bad: boring
    • How to parse? Grammars
    • How to parse? Grammars Cool!
    • How to parse? Grammars Cool! Easy to maintain
    • How to parse? Grammars Cool! Easy to maintain But might be slow
    • How to parse? Grammars Cool! Easy to maintain But might be slow . . . up to 100 times over regexes.
    • Gramars Parse::RecDescent today Perl 6 grammars tomorrow
    • Gramars Parse::RecDescent today Perl 6 grammars after Christmas
    • uri          : post EOL              | view EOL
    • uri          : post EOL              | view EOL post         : post‐message              | post‐comment
    • uri          : post EOL              | view EOL post         : post‐message              | post‐comment view         : view‐message              | view‐tag
    • uri          : post EOL              | view EOL post         : post‐message              | post‐comment view         : view‐message              | view‐tag view‐message : ‘/’ word ‘/’
    • uri          : post EOL              | view EOL post         : post‐message              | post‐comment view         : view‐message              | view‐tag view‐message : ‘/’ word ‘/’ view‐tag     : ‘/tag/’ word ‘/’
    • uri          : post EOL              | view EOL post         : post‐message              | post‐comment view         : view‐message              | view‐tag view‐message : ‘/’ word ‘/’ view‐tag     : ‘/tag/’ word ‘/’ word         : /w+/
    • uri          : post EOL              | view EOL post         : post‐message              | post‐comment view         : view‐message              | view‐tag view‐message : ‘/’ word ‘/’ view‐tag     : ‘/tag/’ word ‘/’ word         : /w+/ post‐message : ‘/post/’
    • uri          : post EOL              | view EOL post         : post‐message              | post‐comment view         : view‐message              | view‐tag view‐message : ‘/’ word ‘/’ view‐tag     : ‘/tag/’ word ‘/’ word         : /w+/ post‐message : ‘/post/’ post‐comment : view‐message ‘/post/’
    • Actions in grammars
    • uri : post EOL     | view EOL
    • uri : post EOL {          $action = ‘post’;       }     | view EOL
    • uri : post EOL {          $action = ‘post’;       }     | view EOL {          $action = ‘view’;       }
    • view‐message : ‘/’ word ‘/’ view‐tag     : ‘/tag/’ word ‘/’
    • view‐message : ‘/’ word ‘/’ {                   $msg_sid = $item{word};                } view‐tag     : ‘/tag/’ word ‘/’
    • view‐message : ‘/’ word ‘/’ {                   $msg_sid = $item{word};                } view‐tag     : ‘/tag/’ word ‘/’ {                   $action = ‘view‐tag’;                   $tag = $item{word};                }
    • http://example.com/twincity/ $action = ‘view’; $msg_sid = ‘twincity’;
    • http://example.com/twincity/ $action = ‘view’; $msg_sid = ‘twincity’; http://example.com/tag/workshop/ $action = ‘view‐tag’; $tag = ‘workshop’;
    • my %uri = (     '/'              => {                        'type' => 'index',                      },     '/alpha/'        => {                        'type' => 'section',                        'sectionUri' => 'alpha',                        'sectionPage' => 1,                      },     '/beta‐2/34/14/' => {                        'type' => 'message',                        'sectionUri' => 'beta',                        'sectionPage' => 2,                        'threadID' => 34,                        'threadPage' => 1,                        'messageID' => 14,                      },     '/‐/'            => {                        'type' => '404',                      });
    • for $uri (keys %uri) {         $parser‐>parse($uri);     cmp_deeply(         $parser‐>{data},         $uri{$uri},         $uri     ); }
    • Grammars are logical
    • Grammars are logical, easy to extend
    • Grammars are logical, easy to extend, cacheable
    • Grammars are logical, easy to extend, cacheable, easy to test
    • 2. .ini configuration files
    • Application = code + configuration
    • Example: blog number of messages per page
    • #!/usr/bin/perl my $MSG_PER_PAGE = 10;
    • #!/usr/bin/perl my $MSG_PER_PAGE = 10;
    • Load configuration from external file
    • Load configuration from external non-Perl file
    • <config>    <page_length>10</page_length> </config>
    • Question: XML or JSON or YAML ?
    • Answer: Windows INI
    • [page] length=10
    • [section] page_length=10 [thread] page_length=10 [obsene] replacement=*** [preview] last_posts=31 last_message_length=200
    • global.ini local.ini
    • global.ini local.ini [page] length=10 [db] [db] host=db.int host=localhost port=3333 port=3306
    • use Config::INI::Access; config‐>load(quot;conf/config.iniquot;);
    • use Config::INI::Access; config‐>load(quot;conf/config.iniquot;); config‐>load(quot;conf/local.iniquot;);
    • use Config::INI::Access; config‐>load(quot;conf/config.iniquot;); config‐>load(quot;conf/local.iniquot;); say config‐>db‐>host;
    • [db] host=localhost port=3306 say config‐>db‐>host;
    • 3. WWW::Page
    • WWW::Page is a kinda MVC
    • http://example.com/some/page
    • http://example.com/some/page /www/example.com/some/page/index.xml
    • http://example.com/some/page /www/example.com/some/page/index.xml XML
    • http://example.com/some/page /www/example.com/some/page/index.xml XML Perl XML
    • http://example.com/some/page /www/example.com/some/page/index.xml XML Perl XML XSLT HTML
    • http://example.com/some/page /www/example.com/some/page/index.xml XML Perl XML XSLT HTML Browser
    •     <?xml version=quot;1.0quot; encoding=quot;UTF‐8quot;?>     <page         import=quot;Import::Clientquot;         transform=quot;view.xslquot;         xmlns:page=quot;urn:www‐pagequot;>              <manifest>             <title>WWW::Page Web‐Site</title>             <locale>en‐gb</locale>             <page:keyword‐list/>         </manifest>              <content>             <page:month‐calendar/>         </content>     </page>
    •     use WWW::Page;     use encoding 'utf‐8';     my $page = new WWW::Page ({         'xslt‐root'       => quot;../data/xslquot;,     });     print $page‐>as_string();
    •     <?xml version=quot;1.0quot; encoding=quot;UTF‐8quot;?>     <page         import=quot;Import::Clientquot;         transform=quot;view.xslquot;         xmlns:page=quot;urn:www‐pagequot;>              <manifest>             <title>WWW::Page Web‐Site</title>             <locale>en‐gb</locale>             <page:keyword‐list/>         </manifest>              <content>             <page:month‐calendar/>         </content>     </page>
    •     <?xml version=quot;1.0quot; encoding=quot;UTF‐8quot;?>     <page         import=quot;Import::Clientquot;         transform=quot;view.xslquot;         xmlns:page=quot;urn:www‐pagequot;>              <manifest>             <title>WWW::Page Web‐Site</title>             <locale>en‐gb</locale>             <page:keyword‐list/>         </manifest>              <content>             <page:month‐calendar/>         </content>     </page>
    •     <?xml version=quot;1.0quot; encoding=quot;UTF‐8quot;?>     <page             sub Import::Client::keywordList         transform=quot;view.xslquot;         xmlns:page=quot;urn:www‐pagequot;>              <manifest>             <title>WWW::Page Web‐Site</title>             <locale>en‐gb</locale>             <page:keyword‐list/>         </manifest>              <content>             <page:month‐calendar/>         </content>     </page>
    •    package Import::Client;    sub keywordList    {        my ($this, $page, $node, $args) = @_;            my $sth = $dbh‐>prepare(            quot;select keyword, uri from keywordsquot;);        $sth‐>execute();        while (my ($keyword, $uri) = $sth‐>fetchrow_array())        {            my $item =                 $page‐>{'xml'}‐>createElement ('item');            $item‐>appendText($keyword);            $item‐>setAttribute('uri', $uri);            $node‐>appendChild($item);        }            return $node;    }
    •    package Import::Client;    sub keywordList    {        my ($this, $page, $node, $args) = @_;            my $sth = $dbh‐>prepare(            quot;select keyword, uri from keywordsquot;);        $sth‐>execute();        while (my ($keyword, $uri) = $sth‐>fetchrow_array())        {            my $item =                 $page‐>{'xml'}‐>createElement ('item');            $item‐>appendText($keyword);            $item‐>setAttribute('uri', $uri);            $node‐>appendChild($item);        }            return $node;    }
    •    package Import::Client;    sub keywordList    {        my ($this, $page, $node, $args) = @_;            my $sth = $dbh‐>prepare(            quot;select keyword, uri from keywordsquot;);        $sth‐>execute();        while (my ($keyword, $uri) = $sth‐>fetchrow_array())        {            my $item =                 $page‐>{'xml'}‐>createElement ('item');            $item‐>appendText($keyword);            $item‐>setAttribute('uri', $uri);            $node‐>appendChild($item);        }            return $node;    }
    •    package Import::Client;    sub keywordList    {        my ($this, $page, $node, $args) = @_;            my $sth = $dbh‐>prepare(            quot;select keyword, uri from keywordsquot;);        $sth‐>execute();        while (my ($keyword, $uri) = $sth‐>fetchrow_array())        {            my $item =                 $page‐>{'xml'}‐>createElement ('item');            $item‐>appendText($keyword);            $item‐>setAttribute('uri', $uri);            $node‐>appendChild($item);        }            return $node;    }
    •     <?xml version=quot;1.0quot; encoding=quot;UTF‐8quot;?>     <page         import=quot;Import::Clientquot;         transform=quot;view.xslquot;         xmlns:page=quot;urn:www‐pagequot;>              <manifest>             <title>WWW::Page Web‐Site</title>             <locale>en‐gb</locale>             <page:keyword‐list/>         </manifest>              <content>             <page:month‐calendar/>         </content>     </page>
    •     <?xml version=quot;1.0quot; encoding=quot;UTF‐8quot;?>     <page         import=quot;Import::Clientquot;         transform=quot;view.xslquot;         xmlns:page=quot;urn:www‐pagequot;>              <manifest>             <title>WWW::Page Web‐Site</title>             <locale>en‐gb</locale>             <page:keyword‐list/>         </manifest>              <content>             <page:month‐calendar/>         </content>     </page>
    • <keyword‐list>     <item uri=quot;http://perl.org/quot;>Perl</item>     <item uri=quot;http://dev.perl.org/quot;>Perl 6</item>     <item uri=quot;http://bbc.co.uk/quot;>Perl on rails</item> </keyword‐list>
    •     <?xml version=quot;1.0quot; encoding=quot;UTF‐8quot;?>     <page         import=quot;Import::Clientquot;         transform=quot;view.xslquot;         xmlns:page=quot;urn:www‐pagequot;>              <manifest>             <title>WWW::Page Web‐Site</title>             <locale>en‐gb</locale>             <keyword‐list> . . . </keyword‐list>         </manifest>              <content>             <month‐calendar> . . . </month‐calendar>         </content>     </page>
    • XML XSLT HTML Browser
    • Possible improvements
    •     <?xml version=quot;1.0quot; encoding=quot;UTF‐8quot;?>     <page         import=quot;Import::Clientquot;         transform=quot;view.xslquot;         xmlns:page=quot;urn:www‐pagequot;>              <manifest>             <page:keyword‐list remote=quot;host2quot;>         </manifest>              <content>             <page:month‐calendar/>         </content>     </page>
    •     <?xml version=quot;1.0quot; encoding=quot;UTF‐8quot;?>     <page         import=quot;Import::Clientquot;         transform=quot;view.xslquot;         xmlns:page=quot;urn:www‐pagequot;>              <manifest>             <page:keyword‐list remote=quot;host2quot;>         </manifest>              <content>             <page:month‐calendar cache=quot;memcachedquot;/>         </content>     </page>
    •     <?xml version=quot;1.0quot; encoding=quot;UTF‐8quot;?>     <page         import=quot;Import::Clientquot;         transform=quot;view.xslquot;         xmlns:page=quot;urn:www‐pagequot;>              <manifest>             <page:keyword‐list remote=quot;host2quot;>         </manifest>              <content>             <page:month‐calendar cache=quot;xmlquot;/>         </content>     </page>
    •     <?xml version=quot;1.0quot; encoding=quot;UTF‐8quot;?>     <page         import=quot;Import::Clientquot;         transform=quot;view.xslquot;         xmlns:page=quot;urn:www‐pagequot;>              <manifest>             <page:keyword‐list remote=quot;host2quot;>         </manifest>              <content>             <page:month‐calendar transform=quot;other.xslquot;/>         </content>     </page>
    • 4. XSLT
    • Not .WHAT XSLT is but .WHY I use it
    • 4a. XSLT: divide and power
    • website = logic + layout
    • website = Perl-code + HTML
    • website = Perl-code + XML+XSLT
    • website = Perl-code + XML+XSLT Perl programmer XSLT coder
    • website = Perl-code + XML+XSLT Perl programmer XSLT coder
    • 4b. XSLT: multiple languages
    • en.xml  <strings>     <months>        <item>January</item>        <item>February</item>           . . .        <item>December</item>     </months> </strings>
    • de.xml  <strings>     <months>        <item>Januar</item>        <item>Februar</item>           . . .        <item>Dezember</item>     </months> </strings>
    • at.xml  <strings>     <months>        <item>Jänner</item>        <item>Februar</item>           . . .        <item>Dezember</item>     </months> </strings>
    • <xsl:variable name=quot;strquot;>     <xsl:copy‐of select=quot;        document(           concat($lang, ‘.xml’)        )quot;/> </xsl:variable>
    • <xsl:variable name=quot;strquot;>     <xsl:copy‐of select=quot;        document(           concat($lang, ‘.xml’)        )/stringsquot;/> </xsl:variable>
    • <date day=quot;7quot; month=quot;11quot; year=quot;2008quot;/>
    • <date day=quot;7quot; month=quot;11quot; year=quot;2008quot;/> <xsl:value‐of select=quot;@dayquot;/>
    • <date day=quot;7quot; month=quot;11quot; year=quot;2008quot;/> <xsl:value‐of select=quot;@yearquot;/>
    • <date day=quot;7quot; month=quot;11quot; year=quot;2008quot;/> <xsl:value‐of      select=quot;$str/months/item[@month]quot;/>
    • 4b. XSLT: multiple layouts
    •     <?xml version=quot;1.0quot; encoding=quot;UTF‐8quot;?>     <page         import=quot;Import::Clientquot;         transform=quot;view.xslquot;         xmlns:page=quot;urn:www‐pagequot;>              <manifest>             <title>WWW::Page Web‐Site</title>             <locale>en‐gb</locale>             <page:keyword‐list/>         </manifest>              <content>             <page:month‐calendar/>         </content>     </page>
    •     <?xml version=quot;1.0quot; encoding=quot;UTF‐8quot;?>     <page         import=quot;Import::Clientquot;         transform=quot;pda/view.xslquot;         xmlns:page=quot;urn:www‐pagequot;>              <manifest>             <title>WWW::Page Web‐Site</title>             <locale>en‐gb</locale>             <page:keyword‐list/>         </manifest>              <content>             <page:month‐calendar/>         </content>     </page>
    •     <?xml version=quot;1.0quot; encoding=quot;UTF‐8quot;?>     <page         import=quot;Import::Clientquot;         transform=quot;iphone/view.xslquot;         xmlns:page=quot;urn:www‐pagequot;>              <manifest>             <title>WWW::Page Web‐Site</title>             <locale>en‐gb</locale>             <page:keyword‐list/>         </manifest>              <content>             <page:month‐calendar/>         </content>     </page>
    •     <?xml version=quot;1.0quot; encoding=quot;UTF‐8quot;?>     <page         import=quot;Import::Clientquot;         transform=quot;rss/view.xslquot;         xmlns:page=quot;urn:www‐pagequot;>              <manifest>             <title>WWW::Page Web‐Site</title>             <locale>en‐gb</locale>             <page:keyword‐list/>         </manifest>              <content>             <page:month‐calendar/>         </content>     </page>
    • website = Perl-code + XML+XSLT Perl programmer XSLT coder
    • website = Perl-code + XML+XSLT Perl programmer XSLT coder XSLT coder XSLT coder XSLT coder
    • __END__ Andrew Shitov http://andy.sh | mail@andy.sh