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 🌸

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