Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is File::Spec really necessary?

Tags:

perl

I know all about the history of different OSes having different path formats, but at this point in time there seems to be a general agreement (with one sorta irrelevant holdout*) about how paths work. I find the whole File::Spec route of path management to be clunky and a useless pain.

Is it really worth having this baroque set of functions to manipulate paths? Please convince me I am being shortsighted.

* Irrelevant because even MS Windows allows forward slashes in paths, which means the only funky thing is the volume at the start and that has never really been a problem for me.

like image 406
Chas. Owens Avatar asked Jul 14 '11 02:07

Chas. Owens


2 Answers

Two major systems have volumes. What's the parent of C:? In unix, it's C:/... In Windows, it's C:... (Unfortunately, most people misuse File::Spec to the point of breaking this.)

There are three different set of path separators in the major systems. The fact that Windows supports "/" could simplify building paths, but it doesn't help in parsing them or to canonising them.

File::Spec also provides useful functions that make it useful even if every system did use the same style of paths, such as the one that turns a path into a relative path.

That said, I never use File::Spec. I use Path::Class instead. Without sacrificing any usability or usefulness, Path::Class provides a much better interface. And it doesn't let users mishandle volumes.

like image 59
ikegami Avatar answered Sep 21 '22 10:09

ikegami


For usual file management inside Perl, No, File::Spec is not necessary and using forward slahes everywhere makes much less pain and works on Win32 anyways.

cpanminus is a good example used by lots of people and have been proved work great on win32 platform. it doesn't use File::Spec for most file path manipulation and just uses forward slashes - that was even suggested so by the experienced Perl-Win32 developers.

The only place I had to use File::Spec's catfile in cpanm, though, is where I extract file paths from a perl error message (Can't locate File\Path.pm blah blah) and create a file path to pass to the command line (i.e. cmd.exe).

Meanwhile File::Spec provides useful functions such as canonical and rel2abs - that's not "necessary" per se but really useful.

like image 37
miyagawa Avatar answered Sep 20 '22 10:09

miyagawa