develooper Front page | perl.perl5.porters | Postings from August 2001

File::Spec::catfile and relativity

Thread Next
Chris Nandor
August 29, 2001 20:29
File::Spec::catfile and relativity
Message ID:
We were having a discussion on macperl-porters about File::Spec.  A problem
that keeps cropping up is that there is no way, that we know of, to
portably create a path with catdir() or catfile() that is marked as either
relative, or absolute.  Essentially, Unix/Windows creates a relative path
by default, whereas Mac OS creates an absolute one by default.


In Mac OS, you can tell it to be relative by making the first argument a
leading empty string.  So catfile("a", "b") is absolute, while catfile("",
"a", "b") is relative.  In Unix/Windows, it is exactly the opposite: the
default is relative, but adding a leading empty string makes it absolute!


Both behaviors make sense given the way each platform works with its specs,
I think, but don't lend themselves to portability.   As I understand it, in
VMS a leading empty string has no effect, which also seems to make sense.

This causes some significant problems in writing portable code.  For example:

>            if ($Is_MacOS) {
>                # create a relative path
>                $dir_path = catdir(':', 'ivp',$File::Find::dir,$_);
>            } else {
>                $dir_path = catdir('ivp',$File::Find::dir,$_);
>            }

So the proposal:

* Make relative paths the default for catfile() and catdir().
File::Spec::Mac will need to change, and this will cause breakage for some
Mac-specific code.  This is already the default for other platforms.

* If the first argument to catfile() and catdir() contains
platform-specific path text that denotes an absolute path, make sure the
path returned is absolute (e.g., catdir('a:', 'b', 'c') in Mac OS would
return 'a:b:c:').  No known changes required for other platforms.

* A leading argument of an empty string to catfile() and catdir() will
result in an absolute path.  Windows and Unix already do this (though by
accident). and would need to be patched.

The bottom line: changes quite a bit and causes breakage;
changes a little bit, but there should be no breakage since the previous
behavior was undefined anyway; no other changes needed for other modules,
except for perhaps some sanity and error checking (and some additional
tests, too).  Then, of course, there needs to be documentation of the

When this is done, we can get rid of existing code that requires if/elses
for catfile/catdir (or the multiple cases of inserting curdir() as the
first argument; those will still work, they just won't be necessary;
will need to take care not to make catdir(qw(: a b)) into "::a:b").

I (or Thomas Wegner) can supply a Mac OS patch; I believe Peter Prymmer can
supply a VMS patch.  One of us, or Barrie, can do a doc patch.  I don't
know about any other platforms, how they would be affected.  Barrie, would
you look into it, if we go this route?


Chris Nandor            
Open Source Development Network

Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About