Beefy Boxes and Bandwidth Generously Provided by pair Networks Joe
Perl Monk, Perl Meditation
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
This is a quick and dirty hack of mine (in 52 lines) with the primary purpose to show what can be done using hashes and the secondary purpose to show the usage of some popular modules for doing cgi-scripts.

The caveats: It has no sanitizing, it isn't taint-safe, Storable isn't good for bigger amounts of data (I could have used one of those nice ORM-modules on cpan, but I would have lost the simplicity of hashes I'd liked to show) and it has nearly no features.

I really would like to hear your comments and advices. So here is the code:

#!/usr/bin/perl use strict; use warnings; use CGI; use CGI::Carp qw/warningsToBrowser fatalsToBrowser/; use HTML::Template; use FindBin qw/$Bin/; use Storable; use Text::Textile qw(textile); my $q = new CGI; my $form = $q->Vars(); my $action = $form->{'action'}; my $wiki = -e "$Bin/wiki.db" ? retrieve("$Bin/wiki.db") : {} ; my $wikiwords = qr/\b([A-Z]\w+[A-Z]\w+)/; my $topic = $form->{topic} || 'FrontPage'; my $entry = $form->{entry} || $wiki->{$topic}; my $template = HTML::Template->new(filename => "$Bin/wiki.html"); my $actions = { 'index' => sub { $entry = join "<br />\n", sort keys %$wiki; $topic = 'OverView'; show(); }, 'new' => sub { $template->param( edit => 1, topic => '', entry => '' ); return $q->header(), $template->output(); }, 'edit' => sub { $template->param( edit => 1, topic => $topic, entry => $entry ) +; return $q->header(), $template->output(); }, 'update' => sub { die "No topic no entry!\n" unless $topic; die "Topic $topic isn't a WikiWord!\n" unless $topic =~ m/$wikiw +ords/; $wiki->{$topic} = $entry; store $wiki, "$Bin/wiki.db" or die "Can't store wiki.dat: $!\n"; show(); }, }; sub show { $actions->{'edit'}() unless $entry; $entry =~ s#$wikiwords#<a href="wiki.pl?topic=$1">$1</a>#g; $template->param( topic => $topic, entry => textile($entry) ); return $q->header(), $template->output(); } print $actions->{$action} ? $actions->{$action}() : show();

and the template:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/ +> <title>wiki</title> <style type="text/css" media="screen"> /* <![CDATA[ */ .default { font-family: Verdana, Helvetica, sans-serif, Arial; font-size: 11px; color: #245199; background-color: #aaa; } #navigation { float: right; width: 150px; background-color: #EEE; padding: 4px; padding-bottom: 20px; border-style: solid; border-width: 0px; border-left-width: 2px; } a { color: #245199; text-decoration: underline } a:hover { color: #000; text-decoration: none } a:active { color: #245199; text-decoration: none } /* ]]> */ </style> </head> <body class="default"> <div id="navigation"> <h2>navigation</h2> <a href="wiki.pl?topic=<TMPL_VAR NAME="topic">;action=edit">edit +</a><br /> <a href="wiki.pl?action=new">new</a><br /> <!-- <a href="wiki.pl?topic=<TMPL_VAR NAME="topic">;action=delet +e">delete</a><br /> --> <a href="wiki.pl?action=index">overview</a><br /> </div> <div id="content"> <h1 id="topic"><TMPL_VAR NAME="topic"></h1> <TMPL_IF NAME="edit"> <form action="wiki.pl" method="POST"> Topic:&nbsp;<input name="topic" size="30" value="<TMPL_VAR + NAME="topic">" /> <p> Entry:&nbsp;<textarea name="entry" cols="80" rows="24"><TM +PL_VAR NAME="entry"></textarea></p> <p> <input type="submit" value="update" name="action"/> </p> </form> <TMPL_ELSE> <div id="entry"> <TMPL_VAR NAME="entry"> </div> </TMPL_IF> </div> </body> </html>


In reply to a quick and dirty Wiki by neniro

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others perusing the Monastery: (7)
    As of 2014-04-18 06:18 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      April first is:







      Results (462 votes), past polls