http://www.perlmonks.org?node_id=503926

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>

Replies are listed 'Best First'.
Re: a quick and dirty Wiki
by eyepopslikeamosquito (Archbishop) on Oct 30, 2005 at 20:06 UTC