This week I updated the Recommended Libraries chapter of Minimum Viable Perl.

There are thousands of modules on MetaCPAN. Some are abandoned, some were an experiment that never took off, some are discouraged, and sometimes its just not obvious which module solves your problem best. In the Recommended Libraries chapter I’m creating a curated guide to MetaCPAN.

I’ve included the latest article below but the canonical (and most up to date version) is available here: Working with files.


If you are working with files in Perl, be sure to consider these fine CPAN modules.

Table Of Contents

High Level Modules

  • Path::Tiny
  • IO::All

Misc Useful Modules

  • Path::Iterator::Rule
  • File::chmod
  • File::pushd
  • File::ReadBackwards

Lower Level Modules

  • Cwd
  • File::Basename
  • File::Copy
  • File::Copy::Recursive
  • File::MimeInfo
  • File::Path
  • File::Slurp
  • File::Slurper
  • File::Spec
  • File::stat
  • File::Temp


High Level Modules

These high level modules making working with files and directories easier by providing convenience and safety in a single unified object oriented interface. For most situations choosing one of these is probably the best way to go.

Path::Tiny

MetaCPAN: Path::Tiny
++ rating: 126
Last update: 2017
Maintainer: DAGOLDEN

Path::Tiny is a small fast library with a fairly traditional object oriented interface for dealing with files and directories. Its probably the most popular high level module.

Path::Tiny began life as a minimalist rewrite of the venerable Path::Class module with a improved user interface. Over the years its grown in popularity and has become a mature, full featured library and one of the most common choices for dealing with files. These days anyone interested in Path::Class is probably better served using Path::Tiny.

IO::All

MetaCPAN: IO::All
++ rating: 58
Last update: 2017
Maintainer: FREW

IO::All is the other library for dealing with files and directories. It is also a good solution and nearly as popular as Path::Tiny.

While Path::Tiny aims to be somewhat minimalist and focused, IO::All does everything. It does file and directory IO but it also has functionality from IO::Socket and MLDBM and LWP. You can use it to create a web server, get files from ftp sites, or send email.

The user interface is somewhat contraversial. Depending on your point of view its powerful, concise, and convenient or makes for hard to read code at times.

Developers visiting Perl from another language are probably going to be more comfortable with Path::Tiny.


Misc Useful Modules

Path::Iterator::Rule

++ rating: 64
Last update: 2018
Maintainer: DAGOLDEN

There are many libraries for iterating over files. But the bottom line is if you need to walk a directory tree, select some files, and do stuff you should almost certainly be using Path::Iterator::Rule. The user interface is fabulous and the performance is good.

Alternatives include File::Find which is part of core (distributed with Perl). File::Find has similarly good performance but is a lot less fun to work with due to its user interface. For benchmarks and a good description of some alternatives see this rjbs article.

File::chmod

MetaCPAN: File::chmod
++ rating: 3
Last update: 2015
Maintainer: XENO

The Perl builtin function chmod() only accepts octal permissons. File::chmod replaces chmod() with its own version that accepts octal, symbolic, or “ls” permissions.

File::chdir

MetaCPAN: File::chdir
++ rating: 9
Last update: 2016
Maintainer: DAGOLDEN

The Perl builtin function chdir() changes the current working directory globally. If any part of your application or and library calls chdir(), the cwd for the entire application changes. File::chdir provides a mechanism for changing directory locally.

File::pushd

MetaCPAN: File::pushd
++ rating: 9
Last update: 2016
Maintainer: DAGOLDEN

File::pushd provides a way to change directory temporarily for a limited scope.

File::ReadBackwards

++ rating: 6
Last update: 2011
Maintainer: URI

File::ReadBackwards can read in file contents backwards line by line.


Lower Level Modules

Modules like Path::Tiny and IO::All are built on top of lower level modules such as these. None of these provide an object oriented user interface unless otherwise noted.

Cwd

MetaCPAN: Cwd
++ rating: 64
Last update: 2018
Maintainer: XSAWYERX

Cwd is a core module (distributed with Perl) which gets the pathname of the current working directory.

File::Basename

MetaCPAN: File::Basename

File::Basename is a core module (distributed with Perl) for parsing paths into directory, filename, and suffix.

File::Copy

MetaCPAN: File::Copy

File::Copy is a core module (distributed with Perl) which provides copy() and move() functions.

File::Copy::Recursive

++ rating: 13
Last update: 2018
Maintainer: DMUEY

File::Copy::Recursive provides methods for copying directories recursively.

File::MimeInfo

MetaCPAN: File::MimeInfo
++ rating: 7
Last update: 2017
Maintainer: FREW

File::MimeInfo can determine the mimetype from a given filename.

File::Path

MetaCPAN: File::Path
++ rating: 38
Last update: 2017
Maintainer: JKEENAN

File::Path can create or remove directory trees.

File::Slurp

MetaCPAN: File::Slurp
++ rating: 62
Last update: 2011
Maintainer: URI

File::Slurp is not recommended. This was a popular module for a while so you may see this in older code. It’s abandoned and has critical flaws which are not fixed including issues with unicode. If you like this functionality try File::Slurper (or better yet Path::Tiny or IO::All).

File::Slurper

MetaCPAN: File::Slurper
++ rating: 35
Last update: 2018
Maintainer: LEONT

File::Slurper provides a way to do fast and correct slurping (reading) and spewing (writing). All functions throw exceptions on errors.

File::Spec

MetaCPAN: File::Spec
++ rating: 64
Last update: 2018
Maintainer: XSAWYERX

File::Spec is a core module (distributed with Perl) for portably manipulating filenames. It’s not OO.

File::stat

MetaCPAN: File::stat
++ rating: 58
Last update: 2017
Maintainer: FREW

Perl’s builtin stat() function returns a list of 13 file attributes: size, mode, uid, gid, etc. All you need to do is remember the order of all those elements. File::stat adds sanity by providing an object oriented user interface to stat().

File::Temp

MetaCPAN: File::Temp
++ rating: 50
Last update: 2013
Maintainer: DAGOLDEN

File::Temp provides an interface for returning the name and handle of a temporary file safely. The best part is it automatically cleans up the temporary file once the variable has gone out of scope.