1Mojo::Path(3) User Contributed Perl Documentation Mojo::Path(3)
2
3
4
6 Mojo::Path - Path
7
9 use Mojo::Path;
10
11 # Parse
12 my $path = Mojo::Path->new('/foo%2Fbar%3B/baz.html');
13 say $path->[0];
14
15 # Build
16 my $path = Mojo::Path->new('/i/♥');
17 push @$path, 'mojolicious';
18 say "$path";
19
21 Mojo::Path is a container for paths used by Mojo::URL, based on RFC
22 3986 <https://tools.ietf.org/html/rfc3986>.
23
25 Mojo::Path implements the following attributes.
26
27 charset
28 my $charset = $path->charset;
29 $path = $path->charset('UTF-8');
30
31 Charset used for encoding and decoding, defaults to "UTF-8".
32
33 # Disable encoding and decoding
34 $path->charset(undef);
35
37 Mojo::Path inherits all methods from Mojo::Base and implements the
38 following new ones.
39
40 canonicalize
41 $path = $path->canonicalize;
42
43 Canonicalize path by resolving "." and "..", in addition "..." will be
44 treated as "." to protect from path traversal attacks.
45
46 # "/foo/baz"
47 Mojo::Path->new('/foo/./bar/../baz')->canonicalize;
48
49 # "/../baz"
50 Mojo::Path->new('/foo/../bar/../../baz')->canonicalize;
51
52 # "/foo/bar"
53 Mojo::Path->new('/foo/.../bar')->canonicalize;
54
55 clone
56 my $clone = $path->clone;
57
58 Return a new Mojo::Path object cloned from this path.
59
60 contains
61 my $bool = $path->contains('/i/♥/mojolicious');
62
63 Check if path contains given prefix.
64
65 # True
66 Mojo::Path->new('/foo/bar')->contains('/');
67 Mojo::Path->new('/foo/bar')->contains('/foo');
68 Mojo::Path->new('/foo/bar')->contains('/foo/bar');
69
70 # False
71 Mojo::Path->new('/foo/bar')->contains('/f');
72 Mojo::Path->new('/foo/bar')->contains('/bar');
73 Mojo::Path->new('/foo/bar')->contains('/whatever');
74
75 leading_slash
76 my $bool = $path->leading_slash;
77 $path = $path->leading_slash($bool);
78
79 Path has a leading slash. Note that this method will normalize the path
80 and that %2F will be treated as "/" for security reasons.
81
82 # "/foo/bar"
83 Mojo::Path->new('foo/bar')->leading_slash(1);
84
85 # "foo/bar"
86 Mojo::Path->new('/foo/bar')->leading_slash(0);
87
88 merge
89 $path = $path->merge('/foo/bar');
90 $path = $path->merge('foo/bar');
91 $path = $path->merge(Mojo::Path->new);
92
93 Merge paths. Note that this method will normalize both paths if
94 necessary and that %2F will be treated as "/" for security reasons.
95
96 # "/baz/yada"
97 Mojo::Path->new('/foo/bar')->merge('/baz/yada');
98
99 # "/foo/baz/yada"
100 Mojo::Path->new('/foo/bar')->merge('baz/yada');
101
102 # "/foo/bar/baz/yada"
103 Mojo::Path->new('/foo/bar/')->merge('baz/yada');
104
105 new
106 my $path = Mojo::Path->new;
107 my $path = Mojo::Path->new('/foo%2Fbar%3B/baz.html');
108
109 Construct a new Mojo::Path object and "parse" path if necessary.
110
111 parse
112 $path = $path->parse('/foo%2Fbar%3B/baz.html');
113
114 Parse path.
115
116 to_abs_string
117 my $str = $path->to_abs_string;
118
119 Turn path into an absolute string.
120
121 # "/i/%E2%99%A5/mojolicious"
122 Mojo::Path->new('/i/%E2%99%A5/mojolicious')->to_abs_string;
123 Mojo::Path->new('i/%E2%99%A5/mojolicious')->to_abs_string;
124
125 parts
126 my $parts = $path->parts;
127 $path = $path->parts([qw(foo bar baz)]);
128
129 The path parts. Note that this method will normalize the path and that
130 %2F will be treated as "/" for security reasons.
131
132 # Part with slash
133 push @{$path->parts}, 'foo/bar';
134
135 to_dir
136 my $dir = $route->to_dir;
137
138 Clone path and remove everything after the right-most slash.
139
140 # "/i/%E2%99%A5/"
141 Mojo::Path->new('/i/%E2%99%A5/mojolicious')->to_dir->to_abs_string;
142
143 # "i/%E2%99%A5/"
144 Mojo::Path->new('i/%E2%99%A5/mojolicious')->to_dir->to_abs_string;
145
146 to_route
147 my $route = $path->to_route;
148
149 Turn path into a route.
150
151 # "/i/♥/mojolicious"
152 Mojo::Path->new('/i/%E2%99%A5/mojolicious')->to_route;
153 Mojo::Path->new('i/%E2%99%A5/mojolicious')->to_route;
154
155 to_string
156 my $str = $path->to_string;
157
158 Turn path into a string.
159
160 # "/i/%E2%99%A5/mojolicious"
161 Mojo::Path->new('/i/%E2%99%A5/mojolicious')->to_string;
162
163 # "i/%E2%99%A5/mojolicious"
164 Mojo::Path->new('i/%E2%99%A5/mojolicious')->to_string;
165
166 trailing_slash
167 my $bool = $path->trailing_slash;
168 $path = $path->trailing_slash($bool);
169
170 Path has a trailing slash. Note that this method will normalize the
171 path and that %2F will be treated as "/" for security reasons.
172
173 # "/foo/bar/"
174 Mojo::Path->new('/foo/bar')->trailing_slash(1);
175
176 # "/foo/bar"
177 Mojo::Path->new('/foo/bar/')->trailing_slash(0);
178
180 Mojo::Path overloads the following operators.
181
182 array
183 my @parts = @$path;
184
185 Alias for "parts". Note that this will normalize the path and that %2F
186 will be treated as "/" for security reasons.
187
188 say $path->[0];
189 say for @$path;
190
191 bool
192 my $bool = !!$path;
193
194 Always true.
195
196 stringify
197 my $str = "$path";
198
199 Alias for "to_string".
200
202 Mojolicious, Mojolicious::Guides, <https://mojolicious.org>.
203
204
205
206perl v5.36.0 2022-07-22 Mojo::Path(3)