The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Util::Medley::File - utility file methods

VERSION

version 0.061

SYNOPSIS

my $util = Util::Medley::File->new;

my $basename = $util->basename($path);
my $dirname  = $util->dirname($path);
my $newpath  = $util->trimSuffix($path);

my ($dir, $utilname, $suffix) = $util->parsePath($path);

$util->cp($src, $dest);
$util->mv($src, $dest);
$util->chmod($path);
$util->mkdir($path);
$util->rmdir($path);
$util->unlink($path);

my $prev_dir = $util->chdir($path);
my $type     = $util->fileType($path);
my @found    = $util->find($path);
my $cwd      = $util->getcwd;

DESCRIPTION

Provides frequently used file operation methods. Many of these are pass-through to another module. Others offer variations on the originals. All methods output debug logging statements when enabled. Any errors are bubbled up with Carp::confess(). Use eval as appropriate.

METHODS

basename

Pass-through to File::Path::basename().

usage:
$basename = $util->basename($path);

$basename = $util->basename(path => $path);
args:
path [Str]

The file path.

chdir

Pass-through to CORE::chdir(), but differs in that it returns the original dir.

usage:
$previous_dir = $util->chdir($path);

$previous_dir = $util->chdir(path => $path);
args:
$path [Str]

Destination directory.

chmod

Pass-through to CORE::chmod().

usage:
$util->chmod(0755, $path);

$util->chmod(perm => 0755, path => $path);
args:
perm [Str]

Numeric mode.

file [Str]

Location of the file to update.

cp

Pass-through to File::Copy::copy().

usage:
$util->cp($src, $dest);

$util->cp(src => $src, dest => $dest);
args:
src [Str]

Source file.

dest [Str]

Destination file.

dirname

Pass-through to File::Path::dirname().

usage:
$dir = $util->dirname($path);

$dir = $util->dirname(path => $path);
args:
path [Str]

The file path.

fileType

Get the filetype of a file.

usage:
$type = $util->fileType($path);

$type = $util->fileType(path => $path);
args:
path [Str]

Path of the file you wish to interrogate.

find

Pass-through to Path::Iterator::Rule. Returns a list of all files and directories. Note this does NOT return the dir passed in.

usage:
@files = $util->find($dir);

@files = $util->find( dir => $dir,
                     [minDepth => $minDepth],
                     [maxDepth => $maxDepth] );
args:
dir [Str]

The directory path you wish to search.

minDepth [Int]

Minimum directory depth to traverse. Not availble for positional based method.

maxDepth [Int]

Maximum directory depth to traverse. Not availble for positional based method.

findFiles

Returns a list of all files under a given directory. Just a convenience wrapper around find.

usage:
@files = $util->findFiles($dir);

@files = $util->findFiles( dir => $dir, 
                          [minDepth => $minDepth],
                          [maxDepth => $maxDepth],
                          [extension => $extension] );
args:
dir [Str]

The directory path you wish to search.

minDepth [Int]

Minimum directory depth to traverse. Not availble for positional based method.

maxDepth [Int]

Maximum number of directeries (in terms of depth) to traverse. Not availble for positional based method.

extension [Str]

Only return files with the given extension.

findDirs

Returns a list of all directories under a given directory. Just a convenience wrapper around find.

usage:
@dirs = $util->findDirs($dir);

@dirs = $util->findDirs( dir      => $dir,
                        [minDepth => $minDepth],
                        [maxDepth => $maxDepth] );
args:
dir [Str]

The directory path you wish to search.

minDepth [Int]

Minimum directory depth to traverse. Not availble for positional based method.

maxDepth [Int]

Maximum number of directeries (in terms of depth) to traverse. Not availble for positional based method.

getcwd

Pass-through to Cwd::getcwd().

usage:
$cwd = $util->getcwd;

mkdir

Pass-through to File::Path::make_path().

usage:
$util->mkdir($path, [$perm]);

$util->mkdir(path => $path, [perm => $perm]);
args:
path [Str]

The directory path.

perm [Str]

Numeric mode.

mv

Pass-through to File::Copy::move().

usage:
$util->mv($src, $dest);

$util->mv(src => $src, dest => $dest);
args:
src [Str]

The source path.

dest [Str]

The destination path.

parsePath

Parse a file path into directory, filename, and extension. This is a pass-through to File::Basename::fileparse, but it additional trims the '.' from the extension and extraneous trailing /'s in the dir.

usage:
($dir, $name, $ext) = $util->parsePath($path);

($dir, $name, $ext) = $util->parsePath(path => $path);
args:
path [Str]

The file path for which you wish to parse.

read

Just a pass-through to File::Slurp::read_file().

usage:
$contents = $util->read($file, [0|1]);
@contents = $util->read($file, [0|1]);

$contents = $util->read(path => $file, trim => [0|1]);
@contents = $util->read(path => $file, trim => [0|1]);
args:
path [Str]

File to read.

trim [Bool]

Trim newlines. Default 0.

rmdir

Delete a directory and any contents. Pass-through to File::Path::remove_tree().

usage:
$util->rmdir($dir);

$util->rmdir(dir => $dir);
args:
dir [Str]

Directory to remove.

slurp (deprecated)

Just a pass-through to File::Slurp::read_file().

usage:
$contents = $util->slurp($file, [0|1]);
@contents = $util->slurp($file, [0|1]);

$contents = $util->slurp(path => $file, trim => [0|1]);
@contents = $util->slurp(path => $file, trim => [0|1]);
args:
path [Str]

File to slurp.

trim [Bool]

Trim newlines. Default 0.

touch

Just a pass-through to File::Touch.

usage:
$util->touch($file);

$util->touch(path => $file);
args:
path [Str]

File or directory to touch.

trimExt

Trim the file extension from a filename.

usage:
$filename_no_ext = $util->trimExt($filename);

$filename_no_ext = $util->trimExt(name => $filename);
args:
name [Str]

The filename for which you want to remove the extension.

Pass-through to built-in unlink().

usage:
$util->unlink($path);

$util->unlink(path => $path);
args:
path [Str]

Path of the file you wish to delete.

which

Wrapper around File::Which::which()

usage:
$path = $util->which($exe);
@path = $util->which($exe);

$path = $util->which(exe => $exe);
@path = $util->which(exe => $exe);
 
args:
exe [Str]

Name of the executable you are searching for.

write

Just a pass-through to File::Slurp::write_file().

usage:
$util->write($file, $content, [ {opts} ]);

$util->write(path => $file, content => $content, [ opts => {} ]);
args:
path [Str]

File to write.

content [Str|ArrayRef]

File content.

opts [HashRef]

Additional opts to pass to write_file.