perl 簡単なWebブラウザ

#簡単なWebブラウザ
#browser.pl

#モジュールの使用宣言
use IO::Socket;

#URLを入力
print "URLを指定してください>";
$url = <STDIN>;

#URLをホスト名とファイルパスに分割
chomp($url);
if($url =~ /http:\/\/([^\/]+)(\/.*)/){
    $host = $1;
    $file = $2;
}else{
    die "そのURLには対応していません。\n";
}

#ソケットオブジェクト作成
$client_socket = new IO::Socket::INET(
    PeerAddr => $host,
    PeerPort => 'http',
    Proto => 'tcp',
    TimeOut => '5'
);
unless($client_socket){
    print "Socket Error:$!\n";
}

#入力をサーバーに送信
print "$host $file" . "\n";
print $client_socket "GET $file HTTP/1.0\n\n";

#出力を取得し、表示(複数行が返ってくるので、取り終わるまで繰り返す)
while($receive = <$client_socket>){
    print $receive;
}

#ソケットを閉じる
$client_socket->close();

#実行結果
# $ perl browser.pl 
# URLを指定してください>http://cruel.org/
# cruel.org /
# HTTP/1.1 200 OK
# Date: Fri, 08 Jul 2016 07:36:18 GMT
# Server: Apache/2.2.11 (Unix) PHP/5.2.9 mod_ssl/2.2.11 OpenSSL/0.9.8e
# Last-Modified: Wed, 15 Apr 2009 10:28:21 GMT
# ETag: "707c09-24-4679568177b40"
# Accept-Ranges: bytes
# Content-Length: 36
# Connection: close
# Content-Type: text/html; charset=UTF-8

# hello
# </body>
# </html>

初めてのPerl 第6版

初めてのPerl 第6版