Goodbye Path::Class, hello Path::Tiny

Reading time: 2 minutes

I like Path::Class, but it’s clunky and slow. So I wrote Path::Tiny to scratch my itch.

It’s smaller (roughly half the lines of code), comes in a single file, and is generally faster. Among other things, it has lots of handy UTF-8 input and output methods.

The downside is that it’s less portable and less extensible, but let’s be honest, most of us are developing only for Unix or Windows anyway. And when was the last time you subclassed Path::Class for something? I’ll bet never. YAGNI.

Here’s the synopsis:

use Path::Tiny;
 
# creating Path::Tiny objects
 
$dir = path("/tmp");
$foo = path("foo.txt");
 
$subdir = $dir->child("foo");
$bar = $subdir->child("bar.txt");
 
# reading files
 
$guts = $file->slurp;
$guts = $file->slurp_utf8;
 
@lines = $file->lines;
@lines = $file->lines_utf8;
 
$head = $file->lines( {count => 1} );
 
# writing files
 
$bar->spew( @data );
$bar->spew_utf8( @data );
 
# reading directories
 
for ( $dir->children ) { ... }
 
$iter = $dir->iterator;
while ( my $next = $iter->() ) { ... }

It does require a very new File::Spec that fixes some ugly, tricky bugs, but, otherwise, it’s core only for any recent Perl.

Check it out!

Update: If you use Moose, there is also MooseX::Types::Path::Tiny.

Update 2: I didn’t mention it before, but note that stringifying the Path::Tiny returns a (possibly cleaned up) copy of the original path.

•      •      •

If you enjoyed this or have feedback, please let me know by or