Name
SPVM::Mojo::URL - Uniform Resource Locator
Description
Mojo::URL class in SPVM implements a subset of RFC 3986, RFC 3987 and the URL Living Standard for Uniform Resource Locators with support for IDNA and IRIs.
Usage
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->set_scheme("http");
$url->set_host("example.com");
$url->set_port(3000);
$url->set_path("/foo/bar");
$url->set_query(Mojo::Parameters->new({foo => "bar"}));
$url->set_fragment(23);
say $url->to_string;
Fields
base
has base : rw Mojo::URL;
Base of this URL, defaults to a Mojo::URL object.
# "http://example.com/a/b?c"
my $url = Mojo::URL->new("/a/b?c");
$url->set_base(Mojo::URL->new("http://example.com"))
$url->to_abs;
fragment
has fragment : rw string;
Fragment part of this URL.
Examples:
# "yada"
Mojo::URL->new("http://example.com/foo?bar=baz#yada")->fragment;
host
has host : rw string;
Host part of this URL.
# "example.com"
Mojo::URL->new("http://sri:t3st@example.com:8080/foo")->host;
port
has port : rw int;
Port part of this URL.
# "8080"
Mojo::URL->new("http://sri:t3st@example.com:8080/foo")->port;
scheme
has scheme : rw string;
Scheme part of this URL.
# "http"
Mojo::URL->new("http://example.com/foo")->scheme;
userinfo
has userinfo : rw string;
Userinfo part of this URL.
# "sri:t3st"
Mojo::URL->new("https://sri:t3st@example.com/foo")->userinfo;
path
has path : rw Mojo::Path;
Path part of this URL, relative paths will be merged with Mojo::Path#merge method, defaults to a Mojo::Path object.
The setter also receives a string.
Examples:
# "test"
Mojo::URL->new("http://example.com/test/Mojo")->path->parts_list->get(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")->set_path("/DOM/HTML");
# "http://example.com/test/DOM/HTML"
Mojo::URL->new("http://example.com/test/Mojo")->set_path("DOM/HTML");
# "http://example.com/test/Mojo/DOM/HTML"
Mojo::URL->new("http://example.com/test/Mojo/")->set_path("DOM/HTML");
query
has query : rw Mojo::Parameters;
Query part of this URL, key/value pairs in an array reference will be appended with Mojo::Parameters#append method, and key/value pairs in a hash reference merged with Mojo::Parameters#merge method, defaults to a Mojo::Parameters object.
The setter also receives an object of object[] type. If the object is options, the merge operation is performed. Otherwise the append operation is performed.
Examples:
# "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")->set_query(Mojo::Parameters->new({a => 2, c => 3}));
# "http://example.com?a=2&a=3"
Mojo::URL->new("http://example.com?a=1&b=2")->set_query(Mojo::Parameters->new({a => [2, 3]});
# "http://example.com?a=2&b=2&c=3"
Mojo::URL->new("http://example.com?a=1&b=2")->set_query({a => 2, c => 3});
# "http://example.com?b=2"
Mojo::URL->new("http://example.com?a=1&b=2")->set_query({a => undef});
# "http://example.com?a=1&b=2&a=2&c=3"
Mojo::URL->new("http://example.com?a=1&b=2")->set_query([(object)a => 2, c => 3]);
Class Methods
new
static method new : Mojo::URL ($url : string = undef);
Construct a new Mojo::URL object and "parse" URL if necessary.
Examples:
my $url = Mojo::URL->new;
my $url = Mojo::URL->new("http://127.0.0.1:3000/foo?f=b&baz=2#foo");
Instance Methods
clone
method clone : Mojo::URL ();
Return a new Mojo::URL object cloned from this URL.
host_port
method host_port : string ();
Normalized version of "host" and "port".
Examples:
# "xn--n3h.net:8080"
Mojo::URL->new("http://☃.net:8080/test")->host_port;
# "example.com"
Mojo::URL->new("http://example.com/test")->host_port;
set_host_port
method set_host_port : void ($host_port : string);
Set "host_port".
ihost
method ihost : string ();
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;
set_ihost
method set_ihost : void ($ihost : string);
Set "ihost".
is_abs
method is_abs : int ();
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;
parse
method parse : void ($url : string);
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
method password : string ();
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_query
method path_query : string ();
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;
set_path_query
method set_path_query : void ($pass_query : string);
Set "path_query"
protocol
method protocol : string ();
Normalized version of "scheme".
# "http"
Mojo::URL->new("HtTp://example.com")->protocol;
to_abs
method to_abs : Mojo::URL ($base : Mojo::URL = undef);
Return a new Mojo::URL object cloned from this relative URL and turn it into an absolute one using "base" or 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
method to_string : string ();
Turn URL into a string. Note that "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
method to_unsafe_string : 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
method username : string ();
Username part of "userinfo".
# "isabel"
Mojo::URL->new("http://isabel:s3cret@mojolicious.org")->username;
See Also
Copyright & License
Copyright (c) 2025 Yuki Kimoto
MIT License