Aug/090
use Path::Class;
The CPAN (Comprehensive Perl Archive Network) distribution, Path::Class, provides a simple OO API alternative to the built-in perl file IO functions such as open, opendir, stat, etc. Code written to use Path::Class tends to be smaller (concise, easier to maintain, etc) than similar code that uses the standard built-in functions.
Here is an example of how a small utility might be written without Path::Class:
And here it is re-written to use Path::Class:
Using Path::Class is a no-brainer when you compare `(stat "$dir/$file")[7]` with `$file->stat->size()`.
One of Path::Class’s greatest stengths is that it is very simple and many of the features it provides are done so by glueing with other well-tested modules that have been on CPAN for a long time. These modules include IO::Dir, IO::File, File::Path, and File::stat.
Some other highlights include:
- Has a recurse() method that provides an enhanced File::Find-like interface.
- Works equally well with absolute and relative paths, and can correctly convert between the two.
- Symlinks are handled correctly and there are methods to interact with them.
- Works equally well under different operating systems, such as Linux and Windows.
- The interface, for me anyway, is about as intuitive as it gets. Very well designed to DWIM.
I’ve been using this module at $work for a while now and am very happy with it and have had coworkers tell me, after they gave it a try, how much cleaner their code ended up being because they used this module.
Enjoy!
No comments yet.
Leave a comment
No trackbacks yet.
