1Mojo::URL(3)          User Contributed Perl Documentation         Mojo::URL(3)
2
3
4

NAME

6       Mojo::URL - Uniform Resource Locator
7

SYNOPSIS

9         use Mojo::URL;
10
11         # Parse
12         my $url = Mojo::URL->new('http://sri:foo@example.com:3000/foo?foo=bar#23');
13         say $url->scheme;
14         say $url->userinfo;
15         say $url->host;
16         say $url->port;
17         say $url->path;
18         say $url->query;
19         say $url->fragment;
20
21         # Build
22         my $url = Mojo::URL->new;
23         $url->scheme('http');
24         $url->host('example.com');
25         $url->port(3000);
26         $url->path('/foo/bar');
27         $url->query(foo => 'bar');
28         $url->fragment(23);
29         say "$url";
30

DESCRIPTION

32       Mojo::URL implements a subset of RFC 3986
33       <http://tools.ietf.org/html/rfc3986>, RFC 3987
34       <http://tools.ietf.org/html/rfc3987> and the URL Living Standard
35       <https://url.spec.whatwg.org> for Uniform Resource Locators with
36       support for IDNA and IRIs.
37

ATTRIBUTES

39       Mojo::URL implements the following attributes.
40
41   base
42         my $base = $url->base;
43         $url     = $url->base(Mojo::URL->new);
44
45       Base of this URL, defaults to a Mojo::URL object.
46
47         "http://example.com/a/b?c"
48         Mojo::URL->new("/a/b?c")->base(Mojo::URL->new("http://example.com"))->to_abs;
49
50   fragment
51         my $fragment = $url->fragment;
52         $url         = $url->fragment('♥mojolicious♥');
53
54       Fragment part of this URL.
55
56         # "yada"
57         Mojo::URL->new('http://example.com/foo?bar=baz#yada')->fragment;
58
59   host
60         my $host = $url->host;
61         $url     = $url->host('127.0.0.1');
62
63       Host part of this URL.
64
65         # "example.com"
66         Mojo::URL->new('http://sri:t3st@example.com:8080/foo')->host;
67
68   port
69         my $port = $url->port;
70         $url     = $url->port(8080);
71
72       Port part of this URL.
73
74         # "8080"
75         Mojo::URL->new('http://sri:t3st@example.com:8080/foo')->port;
76
77   scheme
78         my $scheme = $url->scheme;
79         $url       = $url->scheme('http');
80
81       Scheme part of this URL.
82
83         # "http"
84         Mojo::URL->new('http://example.com/foo')->scheme;
85
86   userinfo
87         my $info = $url->userinfo;
88         $url     = $url->userinfo('root:♥');
89
90       Userinfo part of this URL.
91
92         # "sri:t3st"
93         Mojo::URL->new('https://sri:t3st@example.com/foo')->userinfo;
94

METHODS

96       Mojo::URL inherits all methods from Mojo::Base and implements the
97       following new ones.
98
99   clone
100         my $url2 = $url->clone;
101
102       Return a new Mojo::URL object cloned from this URL.
103
104   host_port
105         my $host_port = $url->host_port;
106         $url          = $url->host_port('example.com:8080');
107
108       Normalized version of "host" and "port".
109
110         # "xn--n3h.net:8080"
111         Mojo::URL->new('http://☃.net:8080/test')->host_port;
112
113         # "example.com"
114         Mojo::URL->new('http://example.com/test')->host_port;
115
116   ihost
117         my $ihost = $url->ihost;
118         $url      = $url->ihost('xn--bcher-kva.ch');
119
120       Host part of this URL in punycode format.
121
122         # "xn--n3h.net"
123         Mojo::URL->new('http://☃.net')->ihost;
124
125         # "example.com"
126         Mojo::URL->new('http://example.com')->ihost;
127
128   is_abs
129         my $bool = $url->is_abs;
130
131       Check if URL is absolute.
132
133         # True
134         Mojo::URL->new('http://example.com')->is_abs;
135         Mojo::URL->new('http://example.com/test/index.html')->is_abs;
136
137         # False
138         Mojo::URL->new('test/index.html')->is_abs;
139         Mojo::URL->new('/test/index.html')->is_abs;
140         Mojo::URL->new('//example.com/test/index.html')->is_abs;
141
142   new
143         my $url = Mojo::URL->new;
144         my $url = Mojo::URL->new('http://127.0.0.1:3000/foo?f=b&baz=2#foo');
145
146       Construct a new Mojo::URL object and "parse" URL if necessary.
147
148   parse
149         $url = $url->parse('http://127.0.0.1:3000/foo/bar?fo=o&baz=23#foo');
150
151       Parse relative or absolute URL.
152
153         # "/test/123"
154         $url->parse('/test/123?foo=bar')->path;
155
156         # "example.com"
157         $url->parse('http://example.com/test/123?foo=bar')->host;
158
159         # "sri@example.com"
160         $url->parse('mailto:sri@example.com')->path;
161
162   password
163         my $password = $url->password;
164
165       Password part of "userinfo".
166
167         # "s3cret"
168         Mojo::URL->new('http://isabel:s3cret@mojolicious.org')->password;
169
170         # "s:3:c:r:e:t"
171         Mojo::URL->new('http://isabel:s:3:c:r:e:t@mojolicious.org')->password;
172
173   path
174         my $path = $url->path;
175         $url     = $url->path('foo/bar');
176         $url     = $url->path('/foo/bar');
177         $url     = $url->path(Mojo::Path->new);
178
179       Path part of this URL, relative paths will be merged with "merge" in
180       Mojo::Path, defaults to a Mojo::Path object.
181
182         # "perldoc"
183         Mojo::URL->new('http://example.com/perldoc/Mojo')->path->parts->[0];
184
185         # "/perldoc/DOM/HTML"
186         Mojo::URL->new('http://example.com/perldoc/Mojo')->path->merge('DOM/HTML');
187
188         # "http://example.com/DOM/HTML"
189         Mojo::URL->new('http://example.com/perldoc/Mojo')->path('/DOM/HTML');
190
191         # "http://example.com/perldoc/DOM/HTML"
192         Mojo::URL->new('http://example.com/perldoc/Mojo')->path('DOM/HTML');
193
194         # "http://example.com/perldoc/Mojo/DOM/HTML"
195         Mojo::URL->new('http://example.com/perldoc/Mojo/')->path('DOM/HTML');
196
197   path_query
198         my $path_query = $url->path_query;
199         $url           = $url->path_query('/foo/bar?a=1&b=2');
200
201       Normalized version of "path" and "query".
202
203         # "/test?a=1&b=2"
204         Mojo::URL->new('http://example.com/test?a=1&b=2')->path_query;
205
206         # "/"
207         Mojo::URL->new('http://example.com/')->path_query;
208
209   protocol
210         my $proto = $url->protocol;
211
212       Normalized version of "scheme".
213
214         # "http"
215         Mojo::URL->new('HtTp://example.com')->protocol;
216
217   query
218         my $query = $url->query;
219         $url      = $url->query({merge => 'to'});
220         $url      = $url->query([append => 'with']);
221         $url      = $url->query(replace => 'with');
222         $url      = $url->query('a=1&b=2');
223         $url      = $url->query(Mojo::Parameters->new);
224
225       Query part of this URL, key/value pairs in an array reference will be
226       appended with "append" in Mojo::Parameters, and key/value pairs in a
227       hash reference merged with "merge" in Mojo::Parameters, defaults to a
228       Mojo::Parameters object.
229
230         # "2"
231         Mojo::URL->new('http://example.com?a=1&b=2')->query->param('b');
232
233         # "a=2&b=2&c=3"
234         Mojo::URL->new('http://example.com?a=1&b=2')->query->merge(a => 2, c => 3);
235
236         # "http://example.com?a=2&c=3"
237         Mojo::URL->new('http://example.com?a=1&b=2')->query(a => 2, c => 3);
238
239         # "http://example.com?a=2&a=3"
240         Mojo::URL->new('http://example.com?a=1&b=2')->query(a => [2, 3]);
241
242         # "http://example.com?a=2&b=2&c=3"
243         Mojo::URL->new('http://example.com?a=1&b=2')->query({a => 2, c => 3});
244
245         # "http://example.com?b=2"
246         Mojo::URL->new('http://example.com?a=1&b=2')->query({a => undef});
247
248         # "http://example.com?a=1&b=2&a=2&c=3"
249         Mojo::URL->new('http://example.com?a=1&b=2')->query([a => 2, c => 3]);
250
251   to_abs
252         my $abs = $url->to_abs;
253         my $abs = $url->to_abs(Mojo::URL->new('http://example.com/foo'));
254
255       Return a new Mojo::URL object cloned from this relative URL and turn it
256       into an absolute one using "base" or provided base URL.
257
258         # "http://example.com/foo/baz.xml?test=123"
259         Mojo::URL->new('baz.xml?test=123')
260           ->to_abs(Mojo::URL->new('http://example.com/foo/bar.html'));
261
262         # "http://example.com/baz.xml?test=123"
263         Mojo::URL->new('/baz.xml?test=123')
264           ->to_abs(Mojo::URL->new('http://example.com/foo/bar.html'));
265
266         # "http://example.com/foo/baz.xml?test=123"
267         Mojo::URL->new('//example.com/foo/baz.xml?test=123')
268           ->to_abs(Mojo::URL->new('http://example.com/foo/bar.html'));
269
270   to_string
271         my $str = $url->to_string;
272
273       Turn URL into a string. Note that "userinfo" will not be included for
274       security reasons.
275
276         # "http://mojolicious.org"
277         Mojo::URL->new->scheme('http')->host('mojolicious.org')->to_string;
278
279         # "http://mojolicious.org"
280         Mojo::URL->new('http://daniel:s3cret@mojolicious.org')->to_string;
281
282   to_unsafe_string
283         my $str = $url->to_unsafe_string;
284
285       Same as "to_string", but includes "userinfo".
286
287         # "http://daniel:s3cret@mojolicious.org"
288         Mojo::URL->new('http://daniel:s3cret@mojolicious.org')->to_unsafe_string;
289
290   username
291         my $username = $url->username;
292
293       Username part of "userinfo".
294
295         # "isabel"
296         Mojo::URL->new('http://isabel:s3cret@mojolicious.org')->username;
297

OPERATORS

299       Mojo::URL overloads the following operators.
300
301   bool
302         my $bool = !!$url;
303
304       Always true.
305
306   stringify
307         my $str = "$url";
308
309       Alias for "to_string".
310

SEE ALSO

312       Mojolicious, Mojolicious::Guides, <https://mojolicious.org>.
313
314
315
316perl v5.30.1                      2020-01-30                      Mojo::URL(3)
Impressum