URLを触る時は、URI(perlモジュール名)が超便利だと思う。

| 1 Comment | 0 TrackBacks | このエントリーをはてなブックマークに追加 このエントリーのはてなブックマーク件数

こんばんは!


urlにパラメータをつけたり、取り除いたり、パスだけ取得したり、クエリーを作りたいとか、そういった機会に出くわすことが多々ありませんか? そういうときは、URI というモジュールが便利です。


こんな感じで使います。

use URI;
use Data::Dumper;
use strict;
use warnings;

my $uri = URI->new('http://hogehoge.com/hoge/hoge/?hoge=hoge&foo=1&foo=2');

print $uri->as_string . "\n";
print $uri->path . "\n";
print $uri->query . "\n";
print $uri->path_query . "\n";

この表示結果は、こんな風になります。色々なパーツが取れて嬉しいですね。

http://hogehoge.com/hoge/hoge/?hoge=hoge&foo=1&foo=2
/hoge/hoge/
hoge=hoge&foo=1&foo=2
/hoge/hoge/?hoge=hoge&foo=1&foo=2

またurlを構築したいときにも使えます。

use URI;
use Data::Dumper;
use strict;
use warnings;

my $uri = URI->new('http://hogehoge.com/hoge/');

$uri->query_form( hoge => 'hoge' , foo => [1 ,2 ] );

print $uri->as_string . "\n";

結果は、以下のようになりますね。

http://hogehoge.com/hoge/?hoge=hoge&foo=1&foo=2

便利ですね! URL を構築する時には、ぜひ使ってみて下さいね!


:wq!

No TrackBacks

TrackBack URL: http://perl-mongers.org/MT/mt-tb.cgi/58

1 Comment

# $uri->as_string
これはほとんどの場合不要です。試しに
print "$uri\n"
とやってみてください。
# ここを強調しておかないとめんどがられたりするので 
Dan the Perl Monger

Leave a comment

About this Entry

This page contains a single entry by ともひろ published on June 20, 2008 1:40 AM.

perl - use warnings; # -w でなくて was the previous entry in this blog.

URLを触る時は、URI(モジュール名)以外も知っておくと吉 is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.

Categories

Pages

Creative Commons License
This blog is licensed under a Creative Commons License.
Powered by Movable Type 4.21-en