Mojo::Path(3)
π Mojo::Path π
okay bestie, welcome to Mojo::Path(3) from the User Contributed Perl Documentation β literally the cutest little container for paths, brought to you by the Mojo fam. pull up a chair, we're doing this π«Ά
β¨ NAME β¨
Mojo::Path β it's a Path. that's the whole vibe. a path. iconic behavior honestly π
πΈ SYNOPSIS πΈ
like, here's the whole thing in action:
use Mojo::Path;
# Parse
my $path = Mojo::Path->new('/foo%2Fbar%3B/baz.html');
say $path->[0];
# Build
my $path = Mojo::Path->new('/i/β₯');
push @$path, 'mojolicious';
say "$path";
see how we parse it, then build it? chef's kiss. no notes π
π¦ DESCRIPTION π¦
Mojo::Path is a container for paths used by Mojo::URL, and it's based on RFC 3986 <https://tools.ietf.org/html/rfc3986>. so yeah, it's standards-compliant and gorgeous. we love a queen who reads the spec πβ¨
π ATTRIBUTES π
Mojo::Path implements the following attributes. yes, attributes. plural. we're fancy.
π charset
my $charset = $path->charset;
$path = $path->charset('UTF-8');
this is the charset used for encoding and decoding, and it defaults to "UTF-8". so if you do nothing, you get UTF-8. slay.
# Disable encoding and decoding
$path->charset(undef);
^ and that's how you turn encoding and decoding off entirely, if you're feeling chaotic π
π METHODS π
Mojo::Path inherits all methods from Mojo::Base and implements the following new ones. yes, it inherited the good genes and then got its own glow-up.
β canonicalize
$path = $path->canonicalize;
canonicalize the path by resolving "." and ".." β and heads up, "..." gets treated as "." too, specifically to protect from path traversal attacks. safety first, bestie π₯
# "/foo/baz"
Mojo::Path->new('/foo/./bar/../baz')->canonicalize;
# "/../baz"
Mojo::Path->new('/foo/../bar/../../baz')->canonicalize;
# "/foo/bar"
Mojo::Path->new('/foo/.../bar')->canonicalize;
π§ clone
my $clone = $path->clone;
returns a brand new Mojo::Path object cloned from this path. a whole twin. main character and their understudy π
π contains
my $bool = $path->contains('/i/β₯/mojolicious');
checks whether the path contains the given prefix. simple as that.
# True
Mojo::Path->new('/foo/bar')->contains('/');
Mojo::Path->new('/foo/bar')->contains('/foo');
Mojo::Path->new('/foo/bar')->contains('/foo/bar');
# False
Mojo::Path->new('/foo/bar')->contains('/f');
Mojo::Path->new('/foo/bar')->contains('/bar');
Mojo::Path->new('/foo/bar')->contains('/whatever');
look at those true/false groups. ate. π
π leading_slash
my $bool = $path->leading_slash;
$path = $path->leading_slash($bool);
does the path have a leading slash or not. but bestie, real talk β this method will normalize the path, and %2F will be treated as "/" for security reasons. important. write it down. π¨
# "/foo/bar"
Mojo::Path->new('foo/bar')->leading_slash(1);
# "foo/bar"
Mojo::Path->new('/foo/bar')->leading_slash(0);
π«Ά merge
$path = $path->merge('/foo/bar');
$path = $path->merge('foo/bar');
$path = $path->merge(Mojo::Path->new);
merge paths together. note that this method will normalize both paths if necessary, and again, %2F will be treated as "/" for security reasons. same energy as before, still mandatory π‘οΈ
# "/baz/yada"
Mojo::Path->new('/foo/bar')->merge('/baz/yada');
# "/foo/baz/yada"
Mojo::Path->new('/foo/bar')->merge('baz/yada');
# "/foo/bar/baz/yada"
Mojo::Path->new('/foo/bar/')->merge('baz/yada');
three whole outcomes, each one a little plot twist π
π new
my $path = Mojo::Path->new;
my $path = Mojo::Path->new('/foo%2Fbar%3B/baz.html');
constructs a new Mojo::Path object, and it will "parse" the path if necessary. yes, "parse" in quotes, that's how the docs say it, don't come for me π
π« parse
$path = $path->parse('/foo%2Fbar%3B/baz.html');
parses the path. boom. done. π₯
π¦ to_abs_string
my $str = $path->to_abs_string;
turns the path into an absolute string.
# "/i/%E2%99%A5/mojolicious"
Mojo::Path->new('/i/%E2%99%A5/mojolicious')->to_abs_string;
Mojo::Path->new('i/%E2%99%A5/mojolicious')->to_abs_string;
notice both of those give you the same absolute result. that's the "abs" part doing its thing β¨
π parts
my $parts = $path->parts;
$path = $path->parts([qw(foo bar baz)]);
the path parts. and yeah, this method will normalize the path too, and %2F will be treated as "/" for security reasons. i know, i know, it's a recurring theme π
# Part with slash
push @{$path->parts}, 'foo/bar';
π to_dir
my $dir = $route->to_dir;
clones the path and removes everything after the right-most slash. so it becomes a directory path. very demure, very directory.
# "/i/%E2%99%A5/"
Mojo::Path->new('/i/%E2%99%A5/mojolicious')->to_dir->to_abs_string;
# "i/%E2%99%A5/"
Mojo::Path->new('i/%E2%99%A5/mojolicious')->to_dir->to_abs_string;
π to_route
my $route = $path->to_route;
turns the path into a route.
# "/i/β₯/mojolicious"
Mojo::Path->new('/i/%E2%99%A5/mojolicious')->to_route;
Mojo::Path->new('i/%E2%99%A5/mojolicious')->to_route;
look at that heart decoded back to β₯. we love a glow-up π₯Ί
π₯ to_string
my $str = $path->to_string;
turns the path into a string.
# "/i/%E2%99%A5/mojolicious"
Mojo::Path->new('/i/%E2%99%A5/mojolicious')->to_string;
# "i/%E2%99%A5/mojolicious"
Mojo::Path->new('i/%E2%99%A5/mojolicious')->to_string;
one keeps the leading slash, one doesn't. they're different and that's valid πΈ
π§ trailing_slash
my $bool = $path->trailing_slash;
$path = $path->trailing_slash($bool);
does the path have a trailing slash. and yep β this method will normalize the path, and %2F will be treated as "/" for security reasons. last time i promise (no i don't) π€
# "/foo/bar/"
Mojo::Path->new('/foo/bar')->trailing_slash(1);
# "/foo/bar"
Mojo::Path->new('/foo/bar/')->trailing_slash(0);
π OPERATORS π
Mojo::Path overloads the following operators. yes, overloads. it's doing the most and honestly? it should.
π array
my @parts = @$path;
this is an alias for "parts". and note that this will normalize the path, with %2F being treated as "/" for security reasons. (we've been through this, it's still true π)
say $path->[0];
say for @$path;
β bool
my $bool = !!$path;
always true. always. in every timeline. forever true. periodt. π
π« stringify
my $str = "$path";
this is an alias for "to_string". easy π
πΈ SEE ALSO πΈ
MojoliciousMojolicious::Guides- <https://mojolicious.org>
go read those, bestie, they're great i promise π¦
and that's the whole page! made with perl v5.42.0, dated 2026-07-15, and yeah it's Mojo::Path(3) β love that for us. drink some water, hydrate, and go build some paths πβ¨ππ«Ά
π¦ where this page comes from
- opensuse-leap 16.0: perl-Mojolicious 9.480.0-bp160.2.1 Β· Artistic-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms