Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
mini memo cgi script like notepad on web browser.
Options ExecCGI
AddHandler cgi-script .cgi
DirectoryIndex mini-memo.cgi
# 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'){
}elsif($cmd eq 'POST'){
if(length $memo == 0){
$log="error: memo cannot be empty";
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(mtime=>strftime "%F %T",localtime((stat $fname)[9]));
print $q->header(-type=>'text/html',-charset=>'utf-8'), $t->output;
<html lang="ja">
<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;}
<h1><TMPL_VAR name=title></h1>
<TMPL IF name=log>
<div class="log"><TMPL_VAR name=log></div>
<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>
<small><i><TMPL_VAR name=app></i></small>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment