blog/content/post/2009/06/03/2009-06-03-00001170.md

12 KiB

title author date wordtwit_post_info categories
フォームを用いたデータの受け渡し kazu634 2009-06-03
O:8:"stdClass":13:{s:6:"manual";b:0;s:11:"tweet_times";i:1;s:5:"delay";i:0;s:7:"enabled";i:1;s:10:"separation";s:2:"60";s:7:"version";s:3:"3.7";s:14:"tweet_template";b:0;s:6:"status";i:2;s:6:"result";a:0:{}s:13:"tweet_counter";i:2;s:13:"tweet_log_ids";a:1:{i:0;i:4635;}s:9:"hash_tags";a:0:{}s:8:"accounts";a:1:{i:0;s:7:"kazu634";}}
Perl

これだけは知っておきたい Webアプリケーションの常識』を読んで、サンプルを打ち込んでいます。仕事でウェブアプリのインフラ周りに関わりそうなので、勉強です。でも、本当はTomcatとかやらないといけないんだよな。。。

html側

Ubuntuだと、/var/www/配下(Apacheで公開するように設定しているディレクトリ)に適当な名前で保存すればいい。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html mlns="http://www.w3.org/1999/xhtml" lang="ja" xml:lang="ja">
<head>
    <metattp-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>フォームの例</title>
  </head>
<body>
<form name="ym" method="post" action="cgi-bin/weekday.pl">
<p>
Year: <input type="text" name="year" size="10" /> <br />
Month: <input type="text" name="month" size="10" />
<input type="submit" name="submit" value="送信" />
</p>
</form>
<address>
<a href="mailto:simoom634@kazu634.local">simoom634</a>
</address>
</body>
</html>

cgi側

Ubuntuだと、/usr/bin/cgi-bin/配下に「weekday.pl」として下のスクリプトを保存する:

#!/usr/bin/perl
# === Libraries ===
use strict;
use warnings;
use CGI;
use Time::Local;
# === Main part ===
my $q = new CGI;
# クエリから年と月を得る
my $year = $q->param('year');
my $month = $q->param('month');
# 指定された月の1日を得る
my $firstday = timelocal(, , , 1, $month - 1, $year - 1900);
# その年の曜日を得る
my ($n_sec, $n_min, $n_hour, $n_day, $n_mon, $n_year, $w_day) = localtime($firstday);
my $wdaystr = ('Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat')[$w_day];
print("Content-type: text/html; char-set=utf-8\n\n");
print("$year年$month月は$wdaystrから始まります\n");

ちなみにhtml側でformの送信メソッドがgetだろうが、postだろうがCGI.pmはよきにはからってくれる。だから、頭を悩ます必要はない。

感想

これまでよくわからずにpostとかgetとかを見てきたけれど、こういう仕組みだったのかと納得した。

これだけは知っておきたい Webアプリケーションの常識

これだけは知っておきたい Webアプリケーションの常識