1Mojo::File(3)         User Contributed Perl Documentation        Mojo::File(3)
2
3
4

NAME

6       Mojo::File - File system paths
7

SYNOPSIS

9         use Mojo::File;
10
11         # Portably deal with file system paths
12         my $path = Mojo::File->new('/home/sri/.vimrc');
13         say $path->slurp;
14         say $path->dirname;
15         say $path->basename;
16         say $path->sibling('.bashrc');
17
18         # Use the alternative constructor
19         use Mojo::File 'path';
20         my $path = path('/tmp/foo/bar')->make_path;
21         $path->child('test.txt')->spurt('Hello Mojo!');
22

DESCRIPTION

24       Mojo::File is a scalar-based container for file system paths that
25       provides a friendly API for dealing with different operating systems.
26
27         # Access scalar directly to manipulate path
28         my $path = Mojo::File->new('/home/sri/test');
29         $$path .= '.txt';
30

FUNCTIONS

32       Mojo::File implements the following functions, which can be imported
33       individually.
34
35   path
36         my $path = path;
37         my $path = path('/home/sri/.vimrc');
38         my $path = path('/home', 'sri', '.vimrc');
39         my $path = path(File::Temp->newdir);
40
41       Construct a new scalar-based Mojo::File object, defaults to using the
42       current working directory.
43
44         # "foo/bar/baz.txt" (on UNIX)
45         path('foo', 'bar', 'baz.txt');
46
47   tempdir
48         my $path = tempdir;
49         my $path = tempdir('tempXXXXX');
50
51       Construct a new scalar-based Mojo::File object for a temporary
52       directory with File::Temp.
53
54         # Longer version
55         my $path = path(File::Temp->newdir('tempXXXXX'));
56
57   tempfile
58         my $path = tempfile;
59         my $path = tempfile(DIR => '/tmp');
60
61       Construct a new scalar-based Mojo::File object for a temporary file
62       with File::Temp.
63
64         # Longer version
65         my $path = path(File::Temp->new(DIR => '/tmp'));
66

METHODS

68       Mojo::File implements the following methods.
69
70   basename
71         my $name = $path->basename;
72         my $name = $path->basename('.txt');
73
74       Return the last level of the path with File::Basename.
75
76         # ".vimrc" (on UNIX)
77         path('/home/sri/.vimrc')->basename;
78
79         # "test" (on UNIX)
80         path('/home/sri/test.txt')->basename('.txt');
81
82   child
83         my $child = $path->child('.vimrc');
84
85       Return a new Mojo::File object relative to the path.
86
87         # "/home/sri/.vimrc" (on UNIX)
88         path('/home')->child('sri', '.vimrc');
89
90   chmod
91         $path = $path->chmod(0644);
92
93       Change file permissions.
94
95   copy_to
96         my $destination = $path->copy_to('/home/sri');
97         my $destination = $path->copy_to('/home/sri/.vimrc.backup');
98
99       Copy file with File::Copy and return the destination as a Mojo::File
100       object.
101
102   dirname
103         my $name = $path->dirname;
104
105       Return all but the last level of the path with File::Basename as a
106       Mojo::File object.
107
108         # "/home/sri" (on UNIX)
109         path('/home/sri/.vimrc')->dirname;
110
111   is_abs
112         my $bool = $path->is_abs;
113
114       Check if the path is absolute.
115
116         # True (on UNIX)
117         path('/home/sri/.vimrc')->is_abs;
118
119         # False (on UNIX)
120         path('.vimrc')->is_abs;
121
122   list
123         my $collection = $path->list;
124         my $collection = $path->list({hidden => 1});
125
126       List all files in the directory and return a Mojo::Collection object
127       containing the results as Mojo::File objects. The list does not include
128       "."  and "..".
129
130         # List files
131         say for path('/home/sri/myapp')->list->each;
132
133       These options are currently available:
134
135       dir
136           dir => 1
137
138         Include directories.
139
140       hidden
141           hidden => 1
142
143         Include hidden files.
144
145   list_tree
146         my $collection = $path->list_tree;
147         my $collection = $path->list_tree({hidden => 1});
148
149       List all files recursively in the directory and return a
150       Mojo::Collection object containing the results as Mojo::File objects.
151       The list does not include "." and "..".
152
153         # List all templates
154         say for path('/home/sri/myapp/templates')->list_tree->each;
155
156       These options are currently available:
157
158       dir
159           dir => 1
160
161         Include directories.
162
163       dont_use_nlink
164           dont_use_nlink => 1
165
166         Force File::Find to always stat directories.
167
168       hidden
169           hidden => 1
170
171         Include hidden files and directories.
172
173   make_path
174         $path = $path->make_path;
175         $path = $path->make_path({mode => 0711});
176
177       Create the directories if they don't already exist, any additional
178       arguments are passed through to File::Path.
179
180   move_to
181         my $destination = $path->move_to('/home/sri');
182         my $destination = $path->move_to('/home/sri/.vimrc.backup');
183
184       Move file with File::Copy and return the destination as a Mojo::File
185       object.
186
187   new
188         my $path = Mojo::File->new;
189         my $path = Mojo::File->new('/home/sri/.vimrc');
190         my $path = Mojo::File->new('/home', 'sri', '.vimrc');
191         my $path = Mojo::File->new(File::Temp->new);
192         my $path = Mojo::File->new(File::Temp->newdir);
193
194       Construct a new Mojo::File object, defaults to using the current
195       working directory.
196
197         # "foo/bar/baz.txt" (on UNIX)
198         Mojo::File->new('foo', 'bar', 'baz.txt');
199
200   open
201         my $handle = $path->open('+<');
202         my $handle = $path->open('r+');
203         my $handle = $path->open(O_RDWR);
204         my $handle = $path->open('<:encoding(UTF-8)');
205
206       Open file with IO::File.
207
208         # Combine "fcntl.h" constants
209         use Fcntl qw(O_CREAT O_EXCL O_RDWR);
210         my $handle = path('/home/sri/test.pl')->open(O_RDWR | O_CREAT | O_EXCL);
211
212   realpath
213         my $realpath = $path->realpath;
214
215       Resolve the path with Cwd and return the result as a Mojo::File object.
216
217   remove_tree
218         $path = $path->remove_tree;
219         $path = $path->remove_tree({keep_root => 1});
220
221       Delete this directory and any files and subdirectories it may contain,
222       any additional arguments are passed through to File::Path.
223
224   sibling
225         my $sibling = $path->sibling('.vimrc');
226
227       Return a new Mojo::File object relative to the directory part of the
228       path.
229
230         # "/home/sri/.vimrc" (on UNIX)
231         path('/home/sri/.bashrc')->sibling('.vimrc');
232
233         # "/home/sri/.ssh/known_hosts" (on UNIX)
234         path('/home/sri/.bashrc')->sibling('.ssh', 'known_hosts');
235
236   slurp
237         my $bytes = $path->slurp;
238
239       Read all data at once from the file.
240
241   spurt
242         $path = $path->spurt($bytes);
243         $path = $path->spurt(@chunks_of_bytes);
244
245       Write all data at once to the file.
246
247   tap
248         $path = $path->tap(sub {...});
249
250       Alias for "tap" in Mojo::Base.
251
252   to_abs
253         my $absolute = $path->to_abs;
254
255       Return absolute path as a Mojo::File object, the path does not need to
256       exist on the file system.
257
258   to_array
259         my $parts = $path->to_array;
260
261       Split the path on directory separators.
262
263         # "home:sri:.vimrc" (on UNIX)
264         join ':', @{path('/home/sri/.vimrc')->to_array};
265
266   to_rel
267         my $relative = $path->to_rel('/some/base/path');
268
269       Return a relative path from the original path to the destination path
270       as a Mojo::File object.
271
272         # "sri/.vimrc" (on UNIX)
273         path('/home/sri/.vimrc')->to_rel('/home');
274
275   to_string
276         my $str = $path->to_string;
277
278       Stringify the path.
279
280   with_roles
281         my $new_class = Mojo::File->with_roles('Mojo::File::Role::One');
282         my $new_class = Mojo::File->with_roles('+One', '+Two');
283         $path         = $path->with_roles('+One', '+Two');
284
285       Alias for "with_roles" in Mojo::Base.
286

OPERATORS

288       Mojo::File overloads the following operators.
289
290   array
291         my @parts = @$path;
292
293       Alias for "to_array".
294
295   bool
296         my $bool = !!$path;
297
298       Always true.
299
300   stringify
301         my $str = "$path";
302
303       Alias for "to_string".
304

SEE ALSO

306       Mojolicious, Mojolicious::Guides, <https://mojolicious.org>.
307
308
309
310perl v5.28.0                      2018-09-14                     Mojo::File(3)
Impressum