名称

Mojo::URL-统一资源定位器

简介

使用Mojo::URL;#分析my$url=Mojo::url->new('http://sri:foo@example.com:3000/foo?foo=棒#23');说$url->scheme;说$url->userinfo;说$url->host;说$url->port;说$url->path;说$url->query;说$url->fragment;#生成my$url=Mojo::url->新建;$url->方案('http');$url->host('example.com');$url->port(3000);$url->path('/foo/bar');$url->查询(foo=>“bar”);$url->片段(23);说出“$url”;

说明

Mojo::URL实现的子集副本请求3986,副本请求3987URL生活标准用于支持IDNA和IRI的统一资源定位器。

属性

Mojo::URL实现以下属性。

基础

my$base=$url->base;$url=$url->base(Mojo::url->new);

此URL的基,默认为Mojo::URL对象。

"http://example.com/a/b?c"Mojo::URL->new(“/a/b?c”)->base(Mojo:网址:http://example.com“)->to_abs;

片段

我的$fragment=$url->片段;$url=$url->片段('令人兴奋的♥');

此URL的片段部分。

#“雅达”Mojo::URL->new('http://example.com/foo?bar=baz#yada')->片段;

主办

我的$host=$url->host;$url=$url->host('127.0.0.1');

此URL的宿主部分。

#“example.com”Mojo::URL->new('http://sri:t3st网址@example.com:8080/foo')->主机;

港口

我的$port=$url->port;$url=$url->端口(8080);

此URL的端口部分。

#“8080”Mojo::URL->new('http://sri:t3st网址@example.com:8080/foo')->端口;

方案

我的$scheme=$url->方案;$url=$url->方案(“http”);

此URL的方案部分。

#“http”Mojo::URL->new('http://example.com/foo')->方案;

用户信息

我的$info=$url->userinfo;$url=$url->userinfo('root:♥');

此URL的Userinfo部分。

#“sri:t3st”Mojo::URL->new('https://sri:t3st@example.com/foo')->用户信息;

方法

Mojo::URL从继承所有方法Mojo::基础并实现了以下新功能。

克隆

我的$url2=$url->克隆;

返回新的Mojo::URL从该URL克隆的对象。

主机端口

我的$host_port=$url->host_port;$url=$url->host_port('example.com:8080');

的规范化版本“主机”“端口”.

#“xn--n3h.net:8080”Mojo::URL->new('http://☃.net:8080/test')->主机端口;#“example.com”Mojo::URL->new('http://example.com/test(http://example.com/test)')->主机端口;

可能性

我的$ihost=$url->ihost;$url=$url->ihost('xn---bcher-kva.ch');

此URL的主机部分采用punycode格式。

#“xn--n3h.net”Mojo::URL->new('http://☃.net’)->主机;#“example.com”Mojo::URL->new('网址:http://example.com')->主机;

是(_A)

我的$bool=$url->is_abs;

检查URL是否为绝对URL。

#真的Mojo::URL->new('网址:http://example.com')->is_abs;Mojo::URL->new('http://example.com/test/index.html')->is_abs;#False(错误)Mojo::URL->new('test/index.html’)->is_abs;Mojo::URL->new('/test/index.html')->is_abs;Mojo::URL->new(“//example.com/test/index.html”)->is_abs;

新的

my$url=Mojo::url->new;my$url=Mojo::url->new('http://127.0.0.1:3000/foo?f=b&baz=2#foo');

构建新的Mojo::URL对象和“解析”URL(如有必要)。

解析

$url=$url->parse('http://127.0.0.1:3000/foo/bar?fo=o&baz=23#foo');

解析相对或绝对URL。

#“/test/123”$url->parse('/test/123?foo=bar')->路径;#“example.com”$url->解析('http://example.com/test/123?foo=bar')->主机;# "sri@example.com"$url->parse('mailto:sri@example.com')->路径;

密码

我的$password=$url->password;

的密码部分“用户信息”.

#“第三条秘密”Mojo::URL->new('http://isabel:s3cret(伊莎贝尔:秘密)@mojolicious.org')->密码;#“s:3:c:r:e:t”Mojo::URL->new('http://isabel:s:3:c:r:e:t@mojolicious.org')->密码;

路径

我的$path=$url->path;$url=$url->path('foo/bar');$url=$url->path('/foo/bar');$url=$url->path(Mojo::path->new);

此URL的路径部分,相对路径将与合并Mojo::Path中的“合并”,默认为Mojo::路径对象。

#“测试”Mojo::URL->new('http://example.com/test/Mojo')->路径->部件->[0];#“/test/DOM/HTML”Mojo::URL->new('http://example.com/test/Mojo')->路径->合并('DOM/HTML');# "http://example.com/DOM/HTML"Mojo::URL->新建('http://example.com/test/Mojo(http://example.com/test/Mojo)')->路径('/DOM/HTML');# "http://example.com/test/DOM/HTML"Mojo::URL->new('http://example.com/test/Mojo')->路径('DOM/HTML');# "http://example.com/test/Mojo/DOM/HTML"Mojo::URL->new('http://example.com/test/Mojo/')->路径('DOM/HTML');

路径查询

我的$pathquery=$url->pathquery;$url=$url->path_query('/foo/bar?a=1&b=2');

的规范化版本“路径”“查询”.

#“/测试?a=1&b=2”Mojo::URL->new('http://example.com/test?a=1&b=2')->路径查询;# "/"Mojo::URL->new('http://example.com/')->路径查询;

协议

我的$proto=$url->协议;

的规范化版本“方案”.

#“http”Mojo::URL->new('HtTp://example.com')->协议;

查询

我的$query=$url->查询;$url=$url->查询({merge=>“to”});$url=$url->查询([append=>“with”]);$url=$url->查询(替换=>“with”);$url=$url->查询('a=1&b=2');$url=$url->query(Mojo::Parameters->new);

此URL的查询部分,数组引用中的键/值对将附加Mojo::Parameters中的“append”,以及与合并的哈希引用中的键/值对Mojo中的“merge”:参数,默认为Mojo::参数对象。

# "2"Mojo::URL->new('http://example.com?a=1&b=2')->查询->参数('b');#“a=2&b=2&c=3”Mojo::URL->new('http://example.com?a=1&b=2')->查询->合并(a=>2,c=>3);# "http://example.com?a=2&c=3"Mojo::URL->new('http://example.com?a=1&b=2')->查询(a=>2,c=>3);# "http://example.com?a=2&a=3"Mojo::URL->new('http://example.com?a=1&b=2')->查询(a=>[2,3]);# "http://example.com?a=2&b=2&c=3"Mojo::URL->new('http://example.com?a=1&b=2')->查询({a=>2,c=>3});# "http://example.com?b=2"Mojo::URL->new('http://example.com?a=1&b=2')->查询({a=>undef});# "http://example.com?a=1&b=2&a=2&c=3"Mojo::URL->new('http://example.com?a=1&b=2')->查询([a=>2,c=>3]);

收件人(_A)

我的$abs=$url->to_abs;my$abs=$url->to_abs(Mojo::url->new('网址:http://example.com/foo'));

返回新的Mojo::URL从该相对URL克隆的对象,并使用将其转换为绝对URL“基础”或提供的基本URL。

# "http://example.com/foo/baz.xml?test=123"Mojo::URL->new('baz.xml?test=123')->to_abs(Mojo::URL->new('http://example.com/foo/bar.html'));# "http://example.com/baz.xml?test=123"Mojo::URL->new('/baz.xml?test=123')->to_abs(Mojo::URL->new('http://example.com/foo/bar.html'));# "http://example.com/foo/baz.xml?test=123"Mojo::URL->new(“//example.com/foo/baz.xml?test=123”)->to_abs(Mojo::URL->new('http://example.com/foo/bar.html'));

到字符串(_S)

我的$str=$url->to_string;

将URL转换为字符串。请注意“用户信息”出于安全原因,将不包括在内。

# "http://mojolicious.org"Mojo::URL->new->scheme('http')->host('mojolicious.org')->to_string;# "http://mojolicious.org"Mojo::URL->new('http://daniel:s3cret(丹尼尔)@mojolicious.org')->to_string;

到不安全字符串

我的$str=$url->to_unsafe_string;

等同于“字符串”,但包括“用户信息”.

#”网址:http://daniel:s3cret@mojolicuis.org“Mojo::URL->new('http://daniel:s3cret(丹尼尔)@mojolicious.org')->to_unsafe_string;

用户名

我的$username=$url->username;

的用户名部分“用户信息”.

#“伊莎贝尔”Mojo::URL->new('http://isabel:s3cret(伊莎贝尔:秘密)@mojolicious.org')->用户名;

操作员

Mojo::URL重载以下运算符。

布尔

我的$bool=$网址;

始终正确。

纤细的

my$str=“$url”;

的别名“字符串”.

另请参阅

莫乔利奇,Mojolicious::指南,https://mojolicious.org.