Mojo::File - File system paths
use Mojo::File; # Portably deal with file system paths my $path = Mojo::File->new('/home/sri/.vimrc'); say $path->slurp; say $path->basename; say $path->dirname->child('.bashrc'); # Use the alternative constructor use Mojo::File 'path'; my $path = path('/tmp/foo/bar')->make_path; $path->child('test.txt')->spurt('Hello Mojo!');
Mojo::File is a scalar-based container for file system paths that provides a friendly API for dealing with different operating systems.
# Access scalar directly to manipulate path my $path = Mojo::File->new('/home/sri/test'); $$path .= '.txt';
Mojo::File implements the following functions, which can be imported individually.
my $path = path; my $path = path('/home/sri/.vimrc'); my $path = path('/home', 'sri', '.vimrc'); my $path = path(File::Temp->newdir);
Construct a new scalar-based Mojo::File object, defaults to using the current working directory.
# "foo/bar/baz.txt" (on UNIX) path('foo', 'bar', 'baz.txt');
my $path = tempdir; my $path = tempdir('tempXXXXX');
Construct a new scalar-based Mojo::File object for a temporary directory with File::Temp.
# Longer version my $path = Mojo::File->new(File::Temp->newdir('tempXXXXX'));
Mojo::File implements the following methods.
my $name = $path->basename; my $name = $path->basename('.txt');
Return the last level of the path with File::Basename.
# ".vimrc" (on UNIX) Mojo::File->new('/home/sri/.vimrc')->basename; # "test" (on UNIX) Mojo::File->new('/home/sri/test.txt')->basename('.txt');
my $child = $path->child('.vimrc');
Return a new Mojo::File object relative to the path.
# "/home/sri/.vimrc" (on UNIX) Mojo::File->new('/home')->child('sri', '.vimrc');
my $name = $path->dirname;
Return all but the last level of the path with File::Basename as a Mojo::File object.
# "/home/sri" (on UNIX) Mojo::File->new('/home/sri/.vimrc')->dirname;
my $bool = $path->is_abs;
Check if the path is absolute.
# True (on UNIX) Mojo::File->new('/home/sri/.vimrc')->is_abs; # False (on UNIX) Mojo::File->new('.vimrc')->is_abs;
my $collection = $path->list; my $collection = $path->list({hidden => 1});
List all files in the directory and return a Mojo::Collection object containing the results as Mojo::File objects.
# List files say for Mojo::File->new('/home/sri/myapp')->list->each;
These options are currently available:
dir => 1
Include directories.
hidden => 1
Include hidden files.
my $collection = $path->list_tree; my $collection = $path->list_tree({hidden => 1});
List all files recursively in the directory and return a Mojo::Collection object containing the results as Mojo::File objects.
# List all templates say for Mojo::File->new('/home/sri/myapp/templates')->list_tree->each;
Include hidden files and directories.
$path = $path->make_path;
Create the directories if they don't already exist with File::Path.
$path = $path->move_to('/home/sri/.vimrc.backup');
Move the file.
my $path = Mojo::File->new; my $path = Mojo::File->new('/home/sri/.vimrc'); my $path = Mojo::File->new('/home', 'sri', '.vimrc'); my $path = Mojo::File->new(File::Temp->newdir);
Construct a new Mojo::File object, defaults to using the current working directory.
# "foo/bar/baz.txt" (on UNIX) Mojo::File->new('foo', 'bar', 'baz.txt');
my $bytes = $path->slurp;
Read all data at once from the file.
$path = $path->spurt($bytes);
Write all data at once to the file.
$path = $path->tap(sub {...});
Alias for "tap" in Mojo::Base.
my $absolute = $path->to_abs;
Return the canonical path as a Mojo::File object.
my $parts = $path->to_array;
Split the path on directory separators.
# "home:sri:.vimrc" (on UNIX) join ':', @{Mojo::File->new('/home/sri/.vimrc')->to_array};
my $relative = $path->to_rel('/some/base/path');
Return a relative path from the original path to the destination path as a Mojo::File object.
# "sri/.vimrc" (on UNIX) Mojo::File->new('/home/sri/.vimrc')->to_rel('/home');
my $str = $path->to_string;
Stringify the path.
Mojo::File overloads the following operators.
my @parts = @$path;
Alias for "to_array".
my $bool = !!$path;
Always true.
my $str = "$path";
Alias for "to_string".
Mojolicious, Mojolicious::Guides, http://mojolicious.org.
To install Mojolicious, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Mojolicious
CPAN shell
perl -MCPAN -e shell install Mojolicious
For more information on module installation, please visit the detailed CPAN module installation guide.