Sweet Path::Class is Coming to Town
File and directory paths. They start off so simple, but then depending on which system the script runs on, they might use slashes or backslashes. And then there are spaces that come to mess everything up. And then there are those systems that use drive letters and volume names. And so on, and so forth.
You know what? Give yourself the gift of simplicity these holidays, and begin to use Path::Class.
Path::Class wraps all those file and directory operations in object-oriented goodness that will warm your DWIM-hungry little heart.
To begin with, creating the objects is pretty straight-forward:
1: | use Path::Class qw/ file dir /; |
And because the dir
and file
objects auto-stringify to their representing strings, it means that you can use them just like any regular path strings:
1: | say "Uh oh" if -f $entry; |
But as soon you discover the methods those objects have, you'll soooo not want to do that. Traveling up and down directory structures will now be a joy:
1: | # down |
Cleaning up complicated paths? A wonder:
1: | say dir( '/home/santa/../santa////children/.//nice' )->resolve; |
Utilities and shortcuts to create tempfiles, iterate through the directory entries or traverse the directory structure? All there:
1: | # create a temporary file |
Regular file operations are likewise simplified via nifty methods:
1: | my $list = file(qw/ home yanick xmas list /); |
Trust me, once you begin using them, there'll be no going back. In fact, you'll probably wish they were also available as Moose types to use them everywhere natively.
And then you'll discover MooseX::Types::Path::Class, and the Christmas bells will be forevermore ringing.
See Also
- Previous
- Next