Is File :: Spec Really Necessary?

I know everything about the history of different OSs that have different path formats, but at the moment there seems to be a general agreement (with one concomitant mismatch) on how the paths work. I believe the whole path of managing the File::Spec trace is an awkward and useless pain.

Is it really worth using this baroque feature set for path management? Please convince me that I am shortsighted.

* Unacceptable, because even MS Windows allows you to forward slashes in tracks, which means that the only funky thing is volumes at the beginning, and this has never been a problem for me.

+6
source share
5 answers

The two main systems have volumes. What is parent C :? On unix, this is C:/.. On Windows, this is C:.. (Unfortunately, most people use File :: Spec incorrectly to break this.)

On major systems, there are three different sets of path separators. The fact that Windows supports "/" can make paths easier to create, but that doesn't help in parsing or canoning them.

The :: Spec file also provides useful functions that make it useful even if each system uses the same style of paths, for example one that turns the path into a relative path.

However, I never use File :: Spec. Instead, I use Path :: Class . Without sacrificing any usability or utility, Path :: Class provides a much better interface. And this prevents users from disassembling volumes.

+7
source

Yes, absolutely.

The golden rule of programming is never strict code string literals.
Change One of the best ways to avoid migration problems is to avoid specific OS constants, especially in the form of embedded literals.

ie for example, drive + ": /" + path + "/" + file name

It is still bad practice. We all commit these horrors in a hurry of the moment or because it is not important for this part of the code. The file :: Spec exists when a programmer adheres to gospel programming.

In addition, it provides values ​​for special and commonly used system directories, for example, tmp or devnull, which can vary from one distribution / OS to another.

If something is possible, perhaps with some other members added to it as a user to point to the users home directory

+3
source

For normal file management inside Perl, File :: Spec is not required, and using intercepted slides everywhere brings much less pain and works on Win32 anyway.

cpanminus is a good example used by many people, and it has proven itself on the win32 platform. it does not use File :: Spec for most file manipulations and simply uses slashes - this was even suggested by experienced Perl-Win32 developers.

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

Meanwhile, File :: Spec provides useful functions, such as canonical and rel2abs, which are not "necessary" as such, but really useful.

+3
source

makepp (makepp.sourceforge.net) has a variable makefile $ /, which is either / or \ (on non-Cygwin Win). The reason is that Win accepts / in file names, but not in command names (where it runs the option).

+1
source

From http://perldoc.perl.org/File/Spec.html :

catdir

Combine two or more directory names to form the full path ending with the directory. But remove the trailing slash from the resulting string because it doesn't look good, is unnecessary, and confuses OS / 2. Of course, if it's the root directory, don't trim the trailing slash :-)

So, for example, in the example, I will not need a regular expression to remove the trailing slash if I use catdir .

0
source

Source: https://habr.com/ru/post/892724/


All Articles