Create a gist now

Instantly share code, notes, and snippets.

mini memo cgi script like notepad on web browser.
Options ExecCGI
AddHandler cgi-script .cgi
DirectoryIndex mini-memo.cgi
#!/usr/bin/perl
# mini memo cgi ver.0.1 (2011-03-26)
# Ver.0.1 2011-03-26 new.
# Ver.0.2 2011-03-27 change wrap of textarea to soft.
use strict;
use warnings;
use POSIX 'strftime';
use CGI;
use CGI::Carp 'fatalsToBrowser';
use HTML::Template;
use utf8;
my $q=CGI->new;
my $app="mini memo ver.0.1 (2011-03-26)";
my $title="mini memo";
my $fname='memo.txt';
my $memo;
my $log;
sub load_memo {
open my $fh,'<',$fname or die "$fname:$!";
my $s=do{local $/;<$fh>};
close $fh;
return $s;
}
my $cmd=$q->request_method();
if($cmd eq 'GET'){
$memo=load_memo;
}elsif($cmd eq 'POST'){
$memo=$q->param('memo');
if(length $memo == 0){
$log="error: memo cannot be empty";
$memo=load_memo;
}else{
open my $fh,'>',$fname or die "$fname:$!";
print $fh $memo;
close $fh;
}
}
my $template=join('',<DATA>);
my $t=HTML::Template->new(scalarref=>\$template);
$t->param(log=>$log) if($log);
$t->param(app=>$app);
$t->param(title=>$title);
$t->param(memo=>$memo);
$t->param(mtime=>strftime "%F %T",localtime((stat $fname)[9]));
print $q->header(-type=>'text/html',-charset=>'utf-8'), $t->output;
__DATA__
<html lang="ja">
<head>
<title><TMPL_VAR name=title></title>
<style type="text/css"><!--
* {margin:0;padding:0;}
body {font-size:small;margin:5px;}
.log {font-style:italic;color:red;margin:5px 0;}
.time {font-style:italic;color:gray;}
.bar {margin:5px 0;}
-->
</style>
</head>
<body>
<h1><TMPL_VAR name=title></h1>
<!--log-->
<TMPL IF name=log>
<div class="log"><TMPL_VAR name=log></div>
</TMPL IF>
<!--memo-->
<form method="post">
<div class="bar">
<input type="submit" value="write">
<input type="button" value="cancel" onclick="location.reload();">
&nbsp;<span class="time">(<TMPL_VAR name=mtime>)</span></div>
<textarea cols="80" rows="20" name="memo" wrap="soft"><TMPL_VAR name=memo></textarea>
<div class="bar">
<input type="submit" value="write">
<input type="button" value="cancel" onclick="location.reload();"></div>
</form>
<hr/>
<small><i><TMPL_VAR name=app></i></small>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment