Name
SPVM::Mojo::File - File system paths
Description
Mojo::File class in SPVM is a scalar-based container for file system paths that provides a friendly API for dealing with different operating systems.
Usage
use Mojo::File;
# Portably deal with file system paths
my $path = Mojo::File->new("/home/sri/.vimrc");
say $path->slurp;
say $path->dirname->to_string;
say $path->basename;
say $path->extname;
say $path->sibling(".bashrc")->to_string;
# Use the alternative constructor
my $path = Mojo::File->new("/tmp/foo/bar")
$path->make_path;
$path->child("test.txt")->spew("Hello Mojo!");
Fields
file
has file : rw object of string|File::Temp|File::Temp::Dir;
A file path or an object with a file path.
Examples:
# Access scalar directly to manipulate path
my $path = Mojo::File->new("/home/sri/test");
$path->set_file($path->file . ".txt");
Class Methods
new
static method new : Mojo::File ($file : object of string|string[]|File::Temp|File::Temp::Dir);
Construct a new Mojo::File object, defaults to using the current working directory.
Examples:
# "foo/bar/baz.txt" (on UNIX)
Mojo::File->new(["foo", "bar", "baz.txt"]);
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->new);
my $path = Mojo::File->new(File::Temp->newdir);
tempdir
static method tempdir : Mojo::File ($options : object[] = undef);
Construct a new scalar-based Mojo::File object for a temporary directory with File::Temp.
Examples:
my $path = Mojo::File->tempdir;
my $path = Mojo::File->tempdir({TEMPLATE => "tempXXXXX"});
# Longer version
my $path = Mojo::File->new(File::Temp->newdir(TEMPLATE => "tempXXXXX"));
tempfile
static method tempfile : Mojo::File ($options : object[] = undef);
Construct a new scalar-based Mojo::File object for a temporary file with File::Temp.
my $path = Mojo::File->tempfile;
my $path = Mojo::File->tempfile({DIR => "/tmp"});
# Longer version
my $path = Mojo::File->new(File::Temp->new({DIR => "/tmp"}));
path
static method path : Mojo::File ($file : object of string|string[]|File::Temp|File::Temp::Dir);
Alias for "new" method.
Instance Methods
basename
method basename : string ();
Return the last level of the path with File::Basename.
Exmaples:
# ".vimrc" (on UNIX)
Mojo::File->new("/home/sri/.vimrc")->basename;
child
method child : Mojo::File ($base_name : object of string|string[]);
Return a new Mojo::File object relative to the path.
Examples:
Mojo::Path->new("/home")->child(".vimrc");
# "/home/sri/.vimrc" (on UNIX)
Mojo::Path->new("/home")->child("sri", ".vimrc");
chmod
method chmod : void ($mode : int);
Change file permissions.
Examples:
$path->chmod(0644);
copy_to
method copy_to : Mojo::File ($to : string);
Copy file with File::Copy and return the destination as a Mojo::File object.
Examples:
my $destination = $path->copy_to("/home/sri");
my $destination = $path->copy_to("/home/sri/.vimrc.backup");
dirname
method dirname : Mojo::File ();
Return all but the last level of the path with File::Basename as a Mojo::File object.
Examples:
# "/home/sri" (on UNIX)
Mojo::Path->new("/home/sri/.vimrc")->dirname;
extname
method extname : string ();
Return file extension of the path.
Examples:
my $ext = $path->extname;
# "js"
Mojo::Path->new("/home/sri/test.js")->extname;
is_abs
method is_abs : int ();
Check if the path is absolute.
Examples:
my $bool = $path->is_abs;
# True (on UNIX)
Mojo::Path->new("/home/sri/.vimrc")->is_abs;
# False (on UNIX)
Mojo::Path->new(".vimrc")->is_abs;
list
method list : Mojo::Collection ($options : object[] = undef);
List all files in the directory and return a Mojo::Collection object containing the results as Mojo::File objects. The list does not include .
and ..
.
Examples:
my $collection = $path->list;
my $collection = $path->list({hidden => 1});
# List files
for my $_ (@{(Mojo::File[])Mojo::File->new("/home/sri/myapp")->list->to_array}) {
say $_->to_string;
}
These options are currently available:
- dir
-
dir => 1
Include directories.
-
hidden => 1
Include hidden files.
list_tree
method list_tree : Mojo::Collection ($options : object[] = undef);
List all files recursively in the directory and return a Mojo::Collection object containing the results as Mojo::File objects. The list does not include .
and ..
.
Examples:
my $collection = $path->list_tree;
my $collection = $path->list_tree({hidden => 1});
# List files
for my $_ (@{(Mojo::File[])Mojo::File->new("/home/sri/myapp/templates")->list_tree->to_array}) {
say $_->to_string;
}
These options are currently available:
- dir
-
dir => 1
Include directories.
-
hidden => 1
Include hidden files and directories.
- max_depth
-
max_depth => 3
Maximum number of levels to descend when searching for files.
lstat
method lstat : Sys::IO::Stat ();
Return a Sys::IO::Stat object for the symlink.
Examples:
my $stat = $path->lstat;
# Get symlink size
say Mojo::Path->new("/usr/sbin/sendmail")->lstat->size;
# Get symlink modification time
say Mojo::Path->new("/usr/sbin/sendmail")->lstat->mtime;
make_path
method make_path : void ($options : object[] = undef);
Create the directories if they don"t already exist, any additional arguments are passed through to File::Path.
Examples:
$path->make_path;
$path->make_path({mode => 0711});
move_to
method move_to : Mojo::File ($to : string);
Examples:
my $destination = $path->move_to("/home/sri");
my $destination = $path->move_to("/home/sri/.vimrc.backup");
Move file with File::Copy and return the destination as a Mojo::File object.
open
method open : IO::File ($mode : object of string|Int)
Open file with IO::File.
Examples:
use Sys::IO::Constant as IOC;
my $handle = $path->open("<");
my $handle = $path->open("r+");
my $handle = $path->open(IOC->O_RDWR);
realpath
method realpath : Mojo::File ();
Resolve the path with Cwd and return the result as a Mojo::File object.
Examples:
my $realpath = $path->realpath;
remove
method remove : void ();
Delete file.
Examples:
$path->remove;
remove_tree
method remove_tree : void ($options : object[] = undef);
Delete this directory and any files and subdirectories it may contain, any additional arguments are passed through to File::Path.
Examples:
$path->remove_tree;
sibling
method sibling : Mojo::File ($base_name : object of string|stirng[]);
Return a new Mojo::File object relative to the directory part of the path.
Examples:
my $sibling = $path->sibling(".vimrc");
# "/home/sri/.vimrc" (on UNIX)
Mojo::Path->new("/home/sri/.bashrc")->sibling(".vimrc");
# "/home/sri/.ssh/known_hosts" (on UNIX)
Mojo::Path->new("/home/sri/.bashrc")->sibling([".ssh", "known_hosts"]);
slurp
method slurp : string ();
Read all data at once from the file. If an encoding is provided, an attempt will be made to decode the content.
Examples:
my $bytes = $path->slurp;
spew
method spew : void ($content : string);
Write all data at once to the file. If an encoding is provided, an attempt to encode the content will be made prior to writing.
Examples:
$path->spew($bytes);
stat
method stat : Sys::IO::Stat ();
Return a Sys::IO::Stat object for the path.
Examples:
# Get file size
say Mojo::Path->new("/home/sri/.bashrc")->stat->size;
# Get file modification time
say Mojo::Path->new("/home/sri/.bashrc")->stat->mtime;
to_abs
method to_abs : Mojo::File ();
Return absolute path as a Mojo::File object, the path does not need to exist on the file system.
Examples:
my $absolute = $path->to_abs;
to_array
method to_array : string[] ();
Split the path on directory separators.
Examples:
# "home:sri:.vimrc" (on UNIX)
Fn->join(":", Mojo::File->new("/home/sri/.vimrc")->to_array);
to_rel
method to_rel : Mojo::File ($rel_file : string);
Return a relative path from the original path to the destination path as a Mojo::File object.
Examples:
my $relative = $path->to_rel("/some/base/path");
# "sri/.vimrc" (on UNIX)
Mojo::Path->new("/home/sri/.vimrc")->to_rel("/home");
to_string
method to_string : string ();
Stringify the path.
Examples:
my $str = $path->to_string;
touch
method touch : void ();
Create file if it does not exist or change the modification and access time to the current time.
Examples:
$path->touch;
# Safely read file
my $path = Mojo::Path->new(".bashrc");
$path->touch;
say $path->slurp;
See Also
Copyright & License
Copyright (c) 2025 Yuki Kimoto
MIT License