hitode909の日記

趣味はマリンスポーツですの日記です

URI->newについて

Perlで,URI->newに空文字列を渡すと,URIのクラスがいい感じになって,URI::_genericというクラスが返る.HTTPだと思って$uri->hostとか呼んでると,URI::_genericにはhostメソッドとかないので死ぬ.
第二引数に'http'とか書くとURI::httpが返ってくるのでhostとか呼べる.

$ URI->new('')->host
Can't locate object method "host" via package "URI::_generic" at (eval) line 1.
default
$ URI->new('', 'http')->host
[
    [0] undef
]

The $scheme argument is only used when $str is a relative URI. It can be either a simple string that denotes the scheme, a string containing an absolute URI reference, or an absolute URI object. If no $scheme is specified for a relative URI $str, then $str is simply treated as a generic URI (no scheme-specific methods available).

URI - Uniform Resource Identifiers (absolute and relative) - metacpan.org - Perl programming language