Mojo::URL - Uniform Resource Locator
use Mojo::URL; # Parse my $url = Mojo::URL->new('http://sri:foobar@example.com:3000/foo/bar?foo=bar#23'); say $url->scheme; say $url->userinfo; say $url->host; say $url->port; say $url->path; say $url->query; say $url->fragment; # Build my $url = Mojo::URL->new; $url->scheme('http'); $url->userinfo('sri:foobar'); $url->host('example.com'); $url->port(3000); $url->path('/foo/bar'); $url->path('baz'); $url->query->param(foo => 'bar'); $url->fragment(23); say "$url";
Mojo::URL implements a subset of RFC 3986 and RFC 3987 for Uniform Resource Locators with support for IDNA and IRIs.
Mojo::URL implements the following attributes.
my $base = $url->base; $url = $url->base(Mojo::URL->new);
Base of this URL.
my $data = $url->data; $url = $url->data('foo')
Data preserved for unknown schemes.
# "sri@example.com" Mojo::URL->new('mailto:sri@example.com')->data;
my $fragment = $url->fragment; $url = $url->fragment('foo');
Fragment part of this URL.
my $host = $url->host; $url = $url->host('127.0.0.1');
Host part of this URL.
my $port = $url->port; $url = $url->port(8080);
Port part of this URL.
my $scheme = $url->scheme; $url = $url->scheme('http');
Scheme part of this URL.
my $userinfo = $url->userinfo; $url = $url->userinfo('root:pass%3Bw0rd');
Userinfo part of this URL.
Mojo::URL inherits all methods from Mojo::Base and implements the following new ones.
my $url = Mojo::URL->new; my $url = Mojo::URL->new('http://127.0.0.1:3000/foo?f=b&baz=2#foo');
Construct a new Mojo::URL object and parse URL if necessary.
parse
my $authority = $url->authority; $url = $url->authority('root:pass%3Bw0rd@localhost:8080');
Authority part of this URL.
my $url2 = $url->clone;
Clone this URL.
my $ihost = $url->ihost; $url = $url->ihost('xn--bcher-kva.ch');
Host part of this URL in punycode format.
# "xn--da5b0n.net" Mojo::URL->new('http://☃.net')->ihost;
my $success = $url->is_abs;
Check if URL is absolute.
$url = $url->parse('http://127.0.0.1:3000/foo/bar?fo=o&baz=23#foo');
Parse relative or absolute URL for the http, https, ws as well as wss schemes and preserve scheme data for all unknown ones.
http
https
ws
wss
# "/test/123" $url->parse('/test/123?foo=bar')->path; # "example.com" $url->parse('http://example.com/test/123?foo=bar')->host; # "mailto:sri@example.com" $url->parse('mailto:sri@example.com')->to_string;
my $path = $url->path; $url = $url->path('/foo/bar'); $url = $url->path('foo/bar'); $url = $url->path(Mojo::Path->new);
Path part of this URL, relative paths will be merged with the existing path, defaults to a Mojo::Path object.
# "http://mojolicio.us/DOM/HTML" Mojo::URL->new('http://mojolicio.us/perldoc/Mojo')->path('/DOM/HTML'); # "http://mojolicio.us/perldoc/DOM/HTML" Mojo::URL->new('http://mojolicio.us/perldoc/Mojo')->path('DOM/HTML'); # "http://mojolicio.us/perldoc/Mojo/DOM/HTML" Mojo::URL->new('http://mojolicio.us/perldoc/Mojo/')->path('DOM/HTML');
my $proto = $url->protocol;
Normalized version of scheme.
scheme
# "http" Mojo::URL->new('HtTp://mojolicio.us')->protocol;
my $query = $url->query; $url = $url->query(replace => 'with'); $url = $url->query([merge => 'with']); $url = $url->query({append => 'to'}); $url = $url->query(Mojo::Parameters->new);
Query part of this URL, pairs in an array will be merged and pairs in a hash appended, defaults to a Mojo::Parameters object.
# "2" Mojo::URL->new('http://mojolicio.us?a=1&b=2')->query->param('b'); # "http://mojolicio.us?a=2&c=3" Mojo::URL->new('http://mojolicio.us?a=1&b=2')->query(a => 2, c => 3); # "http://mojolicio.us?a=2&a=3" Mojo::URL->new('http://mojolicio.us?a=1&b=2')->query(a => [2, 3]); # "http://mojolicio.us?a=2&b=2&c=3" Mojo::URL->new('http://mojolicio.us?a=1&b=2')->query([a => 2, c => 3]); # "http://mojolicio.us?b=2" Mojo::URL->new('http://mojolicio.us?a=1&b=2')->query([a => undef]); # "http://mojolicio.us?a=1&b=2&a=2&c=3" Mojo::URL->new('http://mojolicio.us?a=1&b=2')->query({a => 2, c => 3});
my $abs = $url->to_abs; my $abs = $url->to_abs(Mojo::URL->new('http://example.com/foo'));
Clone relative URL and turn it into an absolute one.
my $rel = $url->to_rel; my $rel = $url->to_rel(Mojo::URL->new('http://example.com/foo'));
Clone absolute URL and turn it into a relative one.
my $string = $url->to_string; my $string = "$url";
Turn URL into a string.
Mojolicious, Mojolicious::Guides, http://mojolicio.us.
To install Mojolicious, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Mojolicious
CPAN shell
perl -MCPAN -e shell install Mojolicious
For more information on module installation, please visit the detailed CPAN module installation guide.