satoshiabe.jp : Introduction to CGI.pm
HOME > DOCUMENTS > PROGRAMMING > Perl > Introduction to CGI.pm
まえがき
CGI.pm を使用するといろいろできるそうだが、そのほとんどを理解していない。 ただ、個人的に、 ・POST されたパラメータの獲得 ・HTML ファイルの生成 ・die のトラップ について便利だと思うので CGI.pm を使用することにしている。
CGI.pm をインストールする
Perl のバージョンが 5.004 以降であれば、標準のモジュールとしてインストールされているはずだ。 念のため確認したいなら、コマンドラインで以下を実行してみれば確認できる。
% perl -v This is perl, v5.8.8 built for i686-linux Copyright 1987-2006, Larry Wall Perl may be copied only under the terms of either the Artistic License or the GNU General Public License, which may be found in the Perl 5 source kit. Complete documentation for Perl, including FAQ lists, should be found on this system using "man perl" or "perldoc perl". If you have access to the Internet, point your browser at http://www.perl.org/, the Perl Home Page. %
もし、インストールされていなかったら、CGI - Simple Common Gateway Interface Class からダウンロードし、インストールすること。
% ls CGI.pm-3.20.tar.gz CGI.pm-3.20.tar.gz % tar xzvf CGI.pm-3.20.tar.gz % cd CGI.pm-3.20 % perl Makefile.pl % make % make test % su # make install
Apache HTTP Server をインストールする
既に Apache HTTP Server はインストールされていることを想定しているので省略する。 ここを参考にする。
CGI を有効にする
httpd.conf に追記する。 CGI スクリプトを保存するディレクトリ /usr/local/apache2/cgi-bin/ と www.example.com/cgi-bin/ をマッピングする。
mod_alias - Apache HTTP サーバ (apache.org)
ScriptAlias /cgi-bin/ /usr/local/apache2/cgi-bin/
CGI スクリプトを作成する
ここでは仮に CGI スクリプトのファイル名を sample01 として作成する。 vi を実行する。
% vi sample01
スクリプトの内容は以下のとおり。 ちなみに、本ページのスクリプトでは、CGI.pm でオブジェクト指向の構文を使用している。
#!/usr/bin/perl -wT # T オプションで taint モードを有効する use strict; # CGI モジュールをロードする use CGI qw(:standard); # オブジェクトを生成し参照変数 $q に代入する my $q = new CGI; print $q->header( -type=>"text/html", -charset=>"UTF-8" ), $q->start_html( -title=>"Replace your title", -encoding=>"UTF-8", ), $q->("hogehoge"), $q->end_html;
vi を終了する。
:wq
実行権限を付与しておく。
% chmod 0755 sample01
確認する
まずは、簡単なスクリプトが確実に実行されることを確認しておく。 http://host_name/cgi-bin/sample01 Internal Error が表示されるなら、/usr/local/apache2/log/error_log に内容が出力されているはずだ。 tail -f しておくとわかりやすい。
尚、先ほどの Perl スクリプトが以下の HTML ファイルを生成する。 この HTML ファイルは valid だ! デフォルトでは XHTML で生成される。
<!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" lang="en-US" xml:lang="en-US"> <head> <title>Replace your title</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> </head> <body> <p>hogehoge</p> </body> </html>
いくつかのメソッドたち
CGI.pm は、シンプルなメソッドを定義している。 頻繁に使用すると考えられるメソッドを書いておく。
Create the HTTP header
HTTP ヘッダを生成するメソッド header。
$q->header ( -type=>"text/html", -charset=>"UTF-8", )
start the html
html を開始するメソッド start_html。
$q->start_html( -title=>"Replace your title", -encording=>"UTF-8", -lang=>"ja", )
creating a self-referencing url
a href と同じ
$q->a({href=>"http://host_name/index.html"}, "top page")
creating a table
table を作成する
$q->start_table({-border=>"1"}), $q->Tr($q->th(['Vegetable', 'Breakfast','Lunch','Dinner'])), $q->Tr($q->td(['Tomatoes' , 'no', 'yes', 'yes']), $q->Tr($q->td(['Broccoli' , 'no', 'no', 'yes']), $q->Tr($q->td(['Onions' , 'yes','yes', 'yes']) $q->end_table,
end the html
html を終了するメソッド end_html。
$q->end_html
パラメータを取得する
以下の記述で、フォームから POST されてきた値を取得できる。 name はパラメータの変数名(という呼び方で正しいか?を確認する)だ。
$q->param("name")
die をトラップする
後ほど説明を追記する。
#!/usr/bin/perl -wT use strict; use CGI; use CGI::Carp qw( fatalsToBrowser ); BEGIN { sub carp_error { my $error_message = shift; my $q = new CGI; print $q->start_html("error"), $q->p("error occured"), $q->p( $q->i( $error_message ) ), $q->end_html; } CGI::Carp::set_message( \&carp_error ); } my $q = new CGI; print $q->header( -type=>"text/html", -charset=>"UTF-8" ), $q->start_html( -title=>"Replace your title", -encoding=>"UTF-8", ), $q->p("done"), $q->end_html;
リンク
CGI - Simple Common Gateway Interface Class (cpan.org)