Name

SPVM::Mojo::Path - Path

Description

Mojo::Path class in SPVM is a container for paths used by Mojo::URL, based on RFC 3986.

Usage

use Mojo::Path;

# Parse
my $path = Mojo::Path->new("/foo%2Fbar%3B/baz.html");
my $parts_list = $path->parts_list;
say $parts_list->get(0);

# Build
my $path = Mojo::Path->new("/i/♥");
my $parts_list = $path->parts_list;
$parts_list->push("mojolicious");
say $path->to_string;

Fields

parts

has parts : virtual rw string[]

The path parts.

This is a virtual field. The value is got from and stored to "parts_list".

parts_list

has parts_list : rw StringList;

The path parts. Note that this method will normalize the path and that %2F will be treated as / for security reasons.

Examples:

# Part with slash
$path->parts_list->push("foo/bar");

leading_slash

has leading_slash : rw byte;

Path has a leading slash. Note that this method will normalize the path and that %2F will be treated as / for security reasons.

Examples:

# "/foo/bar"
Mojo::Path->new("foo/bar")->set_leading_slash(1);

# "foo/bar"
Mojo::Path->new("/foo/bar")->set_leading_slash(0);

trailing_slash

has trailing_slash : rw byte;

Path has a trailing slash. Note that this method will normalize the path and that %2F will be treated as / for security reasons.

Examples:

# "/foo/bar/"
Mojo::Path->new("/foo/bar")->set_trailing_slash(1);

# "/foo/bar"
Mojo::Path->new("/foo/bar/")->set_trailing_slash(0);

Class Methods

new

static method new : Mojo::Path ($path_string : string = undef);

Construct a new Mojo::Path object and "parse" path if necessary.

Examples:

my $path = Mojo::Path->new;
my $path = Mojo::Path->new("/foo%2Fbar%3B/baz.html");

Instance Methods

canonicalize

method canonicalize : void ();

Canonicalize path by resolving . and .., in addition ... will be treated as . to protect from path traversal attacks.

Examples:

# "/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

method clone : Mojo::Path ();

Return a new Mojo::Path object cloned from this path.

contains

method contains : int ($string : string);

Check if path contains given prefix.

Examples:

# 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");

merge

method merge : void ($path : object of string|Mojo::Path);

Merge paths. Note that this method will normalize both paths if necessary and that %2F will be treated as / for security reasons.

# "/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");

parse

method parse : void ($path : string);

Parse path.

to_abs_string

method to_abs_string : string ();

Turn 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;

to_dir

method to_dir : Mojo::Path ();

Clone path and remove everything after the right-most slash.

# "/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

method to_route : string ();

Turn 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;

to_string

method to_string : string ();

Turn 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;

See Also

Copyright & License

Copyright (c) 2025 Yuki Kimoto

MIT License