Perl CGI から MongoDB 内の Collection を参照する

前回の Perl による焼き直しw

#!/perl/bin/perl --

use MongoDB;

my $con = MongoDB::Connection -> new;
my $col = $con -> test -> twitter;

print << "HEADER";
Content-Type: text/html; charset=utf-8
Content-Language: ja

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
<title>MongoDB 接続テスト</title>
</head>
<body>
<h1>Twitter アカウント一覧</h1>
<table summary="Twitter アカウント一覧" border="1">
<tr><th>ユーザー名</th><th>パスワード</th></tr>
HEADER

my $cursor = $col -> find();

while(my $obj = $cursor -> next) {
    my $username = $obj -> {username};
    my $pass = $obj -> {pass};
    print "<tr><td>$username</td><td>$pass</td></tr>\n";
}

print << "FOOTER";
</table>
</body>
</html>
FOOTER

出力結果は割愛。ちなみに処理時間は PHP で書いたときと大差はありませんでした。