my $path = File::Spec->catfile('dir', 'subdir', 'file.txt');
This method, or a similar one involving Path::Class, is the most recommended approach and has been adopted by application development frameworks like Dancer (which has a wrapper method for it, named
path) and Catalyst (with its
The slight problem that I see with this method is that it makes code a bit more complicated, and thus a bit less readable. Paths become lists of parameters and no longer look like paths.
I wrote a simple module that tries to address this by allowing you to write paths the traditional way — as strings, using a directory separator of your choice (
/ being the default), while the
catfile stuff happens behind the scenes. You can just say:
my $path = path 'dir/subdir/file.txt';
What it does is it splits the path string on each occurrence of the forward slash and feeds the resulting list of path components to
File::Spec->catfile, which reassembles them using the appropriate OS-specific directory separator, and constructs the OS-specific path that you want.
The module is up on Github, and should also be available on CPAN shortly.