Mojo::URL(3)

πŸ’– NAME πŸ’–

Mojo::URL β€” Uniform Resource Locator. Yep bestie, this is the Mojo::URL(3) page from the User Contributed Perl Documentation, and it's lowkey a whole vibe for parsing and building URLs. πŸ¦‹


✨ SYNOPSIS ✨

use Mojo::URL;

# Parse
my $url = Mojo::URL->new('http://sri:foo@example.com:3000/foo?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->host('example.com');
$url->port(3000);
$url->path('/foo/bar');
$url->query(foo => 'bar');
$url->fragment(23);
say "$url";

πŸ’… DESCRIPTION πŸ’…

Mojo::URL gives you a subset of RFC 3986 <https://tools.ietf.org/html/rfc3986>, RFC 3987 <https://tools.ietf.org/html/rfc3987>, and the URL Living Standard <https://url.spec.whatwg.org> for Uniform Resource Locators β€” with support for IDNA and IRIs. Slay. ✨


πŸŽ€ ATTRIBUTES πŸŽ€

Mojo::URL implements the following attributes, and honestly? Iconic behavior:

πŸŽ€ base πŸŽ€

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

Base of this URL. It defaults to a Mojo::URL object, bestie.

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

πŸŽ€ fragment πŸŽ€

my $fragment = $url->fragment;
$url         = $url->fragment('β™₯mojoliciousβ™₯');

Fragment part of this URL. Cute little tail end. πŸŽ€

# "yada"
Mojo::URL->new('http://example.com/foo?bar=baz#yada')->fragment;

πŸŽ€ host πŸŽ€

my $host = $url->host;
$url     = $url->host('127.0.0.1');

Host part of this URL.

# "example.com"
Mojo::URL->new('http://sri:t3st@example.com:8080/foo')->host;

πŸŽ€ port πŸŽ€

my $port = $url->port;
$url     = $url->port(8080);

Port part of this URL.

# "8080"
Mojo::URL->new('http://sri:t3st@example.com:8080/foo')->port;

πŸŽ€ scheme πŸŽ€

my $scheme = $url->scheme;
$url       = $url->scheme('http');

Scheme part of this URL.

# "http"
Mojo::URL->new('http://example.com/foo')->scheme;

πŸŽ€ userinfo πŸŽ€

my $info = $url->userinfo;
$url     = $url->userinfo('root:β™₯');

Userinfo part of this URL.

# "sri:t3st"
Mojo::URL->new('https://sri:t3st@example.com/foo')->userinfo;

πŸ¦‹ METHODS πŸ¦‹

Mojo::URL inherits all methods from Mojo::Base and implements the following new ones. Periodt.

πŸ¦‹ clone πŸ¦‹

my $url2 = $url->clone;

Return a new Mojo::URL object cloned from this URL.

πŸ¦‹ host_port πŸ¦‹

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

Normalized version of host and port.

# "xn--n3h.net:8080"
Mojo::URL->new('http://β˜ƒ.net:8080/test')->host_port;

# "example.com"
Mojo::URL->new('http://example.com/test')->host_port;

πŸ¦‹ ihost πŸ¦‹

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

Host part of this URL in punycode format.

# "xn--n3h.net"
Mojo::URL->new('http://β˜ƒ.net')->ihost;

# "example.com"
Mojo::URL->new('http://example.com')->ihost;

πŸ¦‹ is_abs πŸ¦‹

my $bool = $url->is_abs;

Check if URL is absolute.

# True
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;

πŸ¦‹ new πŸ¦‹

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 πŸ¦‹

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

Parse relative or absolute URL.

# "/test/123"
$url->parse('/test/123?foo=bar')->path;

# "example.com"
$url->parse('http://example.com/test/123?foo=bar')->host;

# "sri@example.com"
$url->parse('mailto:sri@example.com')->path;

πŸ¦‹ password πŸ¦‹

my $password = $url->password;

Password part of userinfo.

# "s3cret"
Mojo::URL->new('http://isabel:s3cret@mojolicious.org')->password;

# "s:3:c:r:e:t"
Mojo::URL->new('http://isabel:s:3:c:r:e:t@mojolicious.org')->password;

πŸ¦‹ path πŸ¦‹

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 merge in Mojo::Path, and it defaults to a Mojo::Path object.

# "test"
Mojo::URL->new('http://example.com/test/Mojo')->path->parts->[0];

# "/test/DOM/HTML"
Mojo::URL->new('http://example.com/test/Mojo')->path->merge('DOM/HTML');

# "http://example.com/DOM/HTML"
Mojo::URL->new('http://example.com/test/Mojo')->path('/DOM/HTML');

# "http://example.com/test/DOM/HTML"
Mojo::URL->new('http://example.com/test/Mojo')->path('DOM/HTML');

# "http://example.com/test/Mojo/DOM/HTML"
Mojo::URL->new('http://example.com/test/Mojo/')->path('DOM/HTML');

πŸ¦‹ path_query πŸ¦‹

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

Normalized version of path and query.

# "/test?a=1&b=2"
Mojo::URL->new('http://example.com/test?a=1&b=2')->path_query;

# "/"
Mojo::URL->new('http://example.com/')->path_query;

πŸ¦‹ protocol πŸ¦‹

my $proto = $url->protocol;

Normalized version of scheme.

# "http"
Mojo::URL->new('HtTp://example.com')->protocol;

πŸ¦‹ query πŸ¦‹

my $query = $url->query;
$url      = $url->query({merge => 'to'});
$url      = $url->query([append => 'with']);
$url      = $url->query(replace => 'with');
$url      = $url->query('a=1&b=2');
$url      = $url->query(Mojo::Parameters->new);

Query part of this URL. Key/value pairs in an array reference will be appended with append in Mojo::Parameters, and key/value pairs in a hash reference merged with merge in Mojo::Parameters. It defaults to a Mojo::Parameters object, obviously.

# "2"
Mojo::URL->new('http://example.com?a=1&b=2')->query->param('b');

# "a=2&b=2&c=3"
Mojo::URL->new('http://example.com?a=1&b=2')->query->merge(a => 2, c => 3);

# "http://example.com?a=2&c=3"
Mojo::URL->new('http://example.com?a=1&b=2')->query(a => 2, c => 3);

# "http://example.com?a=2&a=3"
Mojo::URL->new('http://example.com?a=1&b=2')->query(a => [2, 3]);

# "http://example.com?a=2&b=2&c=3"
Mojo::URL->new('http://example.com?a=1&b=2')->query({a => 2, c => 3});

# "http://example.com?b=2"
Mojo::URL->new('http://example.com?a=1&b=2')->query({a => undef});

# "http://example.com?a=1&b=2&a=2&c=3"
Mojo::URL->new('http://example.com?a=1&b=2')->query([a => 2, c => 3]);

πŸ¦‹ to_abs πŸ¦‹

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

Return a new Mojo::URL object cloned from this relative URL and turn it into an absolute one using base or the provided base 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'));

πŸ¦‹ to_string πŸ¦‹

my $str = $url->to_string;

Turn URL into a string.

🚨 Heads up bestie: userinfo will not be included for security reasons.
# "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;

πŸ¦‹ to_unsafe_string πŸ¦‹

my $str = $url->to_unsafe_string;

Same as to_string, but includes userinfo.

# "http://daniel:s3cret@mojolicious.org"
Mojo::URL->new('http://daniel:s3cret@mojolicious.org')->to_unsafe_string;

πŸ¦‹ username πŸ¦‹

my $username = $url->username;

Username part of userinfo.

# "isabel"
Mojo::URL->new('http://isabel:s3cret@mojolicious.org')->username;

πŸ”₯ OPERATORS πŸ”₯

Mojo::URL overloads the following operators.

πŸ”₯ bool πŸ”₯

my $bool = !!$url;

Always true. Always. πŸ’…

πŸ”₯ stringify πŸ”₯

my $str = "$url";

Alias for to_string.


πŸ’– SEE ALSO πŸ’–


πŸ’Œ cute sign-off: source/version perl v5.42.0, date 2026-07-15, manual Mojo::URL(3). byeee, go build some URLs 🫢✨
πŸ“¦ where this page comes from