[Perlメモ]PHPで書いた掲示板をMojolicious::Liteで書いてみる
この間PHPで書いた掲示板みたいなものをMojolicious::Liteで書いて見ました。
[PHPメモ]PHP覚えたので掲示板作ってみた – KUMA TYPE
PHPで書いたソースとほぼ同じような感じです。こっちはデータベースファイルもなければ自動で作成するようになってます。
ソース bbs.pl
#!/usr/bin/env perl
package Model;
use strict;
use ORLite {
file => ‘bbs2.db’,
create => sub {
my $dbh = shift;
$dbh->do(
‘CREATE TABLE bbs (
no INTEGER NOT NULL PRIMARY KEY,
pno INTEGER,
name VARCHAR(128),
title VARCHAR(128),
comment VARCHAR(2000) NOT NULL,
hp VARCHAR(256),
email VARCHAR(128),
ip VARCHAR(40) NOT NULL DEFAULT "",
timestamp TIMESTAMP NOT NULL
)’
);
# just use $dbh->do(…) if you need more
# tables
return 1;
}
};
package main;
use utf8;
use Mojolicious::Lite;
my $cur_page = 1;
my $entry_per_page = 5;
my $cur_page = 1;
my $start_point = 0;
#get ‘/’ => ‘index’;
#get ‘/’ => sub {
# my $self = shift;
# $self->render(‘index’);
#};
get ‘/’ => sub {
my $self = shift;
#総記事数取得
my $total_entrys = Model::Bbs->count;
#ページ指定あり
if(defined $self->param(‘page’)){
$cur_page = sprintf("%d",$self->param(‘page’));
$cur_page = 1 if($cur_page < 1);
$start_point = ($cur_page – 1) * $entry_per_page;
}
$self->stash(
cur_page => $cur_page,
entry_per_page => $entry_per_page,
total_entrys => $total_entrys,
bbs => [Model::Bbs->select(‘order by no desc limit ?,?’,$start_point,$entry_per_page)]
);
} => ‘index’;
post ‘/’ => sub {
my $self = shift;
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
my $now = sprintf("%04d-%02d-%02d %02d:%02d:%02d",$year+1900,$mon+1,$mday,$hour,$min,$sec);
Model::Bbs->create(
name => $self->param(‘name’),
comment => $self->param(‘comment’),
ip => ‘0.0.0.0’,
timestamp => $now
);
$self->redirect_to(‘/’);
};
app->start;
__DATA__
@@ index.html.ep
% use Encode qw/decode_utf8/;
% layout ‘default’;
<div id="header">
<h1>掲示板</h1>
</div>
<div id="body">
<form name="form1" method="post" action="">
<p>名前 :
<input type="text" name="name" id="name" maxlength="128">
</p>
<p> 本文:<br>
<textarea name="comment" cols="50" rows="6" id="comment"></textarea>
</p>
<p>
<input type="submit" name="submit" value="送信" id="submit">
</p>
</form>
<p>
総記事数:<%= $total_entrys %> 件
</p>
% foreach my $oneline(@{$bbs}){
<div class="entrys">
<p class="entry_header">
<%= sprintf("[%05d] %s %s さん",decode_utf8($oneline->no),decode_utf8($oneline->timestamp),decode_utf8($oneline->name)) %>
</p>
<p class="entry_body">
<%= decode_utf8($oneline->comment) %>
</p>
</div>
% }
<p class="page">
% if ($cur_page > 1) {
<a href="?page=<%= ($cur_page-1) %>">[next]</a>
% }else{
[next]
% }
% if ($total_entrys > ($cur_page * $entry_per_page)){
<a href="?page=<%= ($cur_page+1) %>">[prev]</a>
% }else{
[prev]
% }
</p>
</div>
@@ layouts/default.html.ep
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html lang="ja">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta http-equiv="content-style-type" content="text/css">
<meta http-equiv="content-script-type" content="text/javascript">
<meta name="description" content="掲示板">
<meta name="keywords" content="掲示板">
<title>掲示板</title>
<link rel="stylesheet" type="text/css" href="css/bbs.css">
</head>
<body>
<%= content %>
<div id="footer">2011 KUMA TYPE</div>
</body>
</html>
参考
- ORLite – search.cpan.org
- Mojolicious::LiteでData::Modelを使ってみた – 日曜プログラマのそゞろ事
- Working with ORLite inside Mojolicious – GitHub
メモ
[kumacchi@localhost bbs]$ mojo generate lite_app bbs.pl
[exist] /home/kumacchi/mojo/bbs
[write] /home/kumacchi/mojo/bbs/bbs.pl
[chmod] bbs.pl 744
[kumacchi@localhost bbs]$
perl -MCPAN -e shell
cpan[2]> install ORLite