以下のプログラムはごく簡単なWeb掲示板のCGIプログラムである。
#!/usr/bin/env perl # http ヘッダの出力 print "Content-Type: text/html\n\n"; # ボードファイル名 $bfile ="board.dat"; &decode; $mes = $tags{'message'}; $name = $tags{'name'}; if ($mes) { $mes = &unspecial($mes); } if ($name) { $name = &unspecial($name); } else { $name = "名無し"; } # メッセージがあったら、名前と共にボードファイルに追加 if ($mes) { open(FH, ">>" . $bfile); print FH <<"END"; <dt>${name} さんの書き込み:</dt> <dd>${mes}</dd> END close(FH); } # HTMLファイルの先頭部分を出力 print "<html><body>\n"; print "<h1>Very simple Web board</h1><hr><dl>\n"; # 間にボードファイルの内容を挟み込む open(FH, $bfile); while (<FH>) { print $_; } close(FH); # HTMLファイルの末尾部分(フォーム含む)を出力 print <<"END"; </dl> <hr> <form method=post action=board.cgi> お名前: <input type=text size=20 name="name" value="$name"><br> 書き込み欄: <br> <textarea name=message rows=5 cols=60> </textarea><br> <input type=submit value="書き込み"> <input type=reset value="クリア"> </form> </body> </html> END #------------------------------------------------------------ # cgi.pl - decode CGI input. # by ETO Kouichirou <t91069ke@sfc.keio.ac.jp> # date 1994/05/11 # # Usage: # # &decode; # ... # $name = $tags{'name'}; sub decode { local($args, $n_read, *terms, $tag, $value); if ($ENV{'REQUEST_METHOD'} eq "POST"){ $n_read = sysread(STDIN, $args, $ENV{'CONTENT_LENGTH'}); } else { $args = $ENV{'QUERY_STRING'}; } @terms = split('&', $args); foreach (@terms) { ($tag, $value) = split(/=/, $_, 2); $otags{$tag} = $value; # original tags $tags{$tag} = &unpack($value); # tags is global } } sub unpack { local($value) = @_; $value =~ s/\+/ /g; $value =~ s/%(..)/pack("c", hex($1))/ge; return $value; } sub unspecial { local($value) = @_; $value =~ s/&/&/g; $value =~ s/</</g; $value =~ s/>/>/g; return $value; }
備考: