Front page | perl.perl5.porters |
Postings from August 2001
File::Spec::catfile and relativity
Thread Next
From:
Chris Nandor
Date:
August 29, 2001 20:29
Subject:
File::Spec::catfile and relativity
Message ID:
p05100301b7a01dd37631@[209.192.217.51]
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.
Yikes.
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!
Yikes!
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). VMS.pm and Mac.pm would need to be patched.
The bottom line: Mac.pm changes quite a bit and causes breakage; VMS.pm
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
behavior.
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; Mac.pm
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?
Thoughts?
--
Chris Nandor pudge@pobox.com http://pudge.net/
Open Source Development Network pudge@osdn.com http://osdn.com/
Thread Next
-
File::Spec::catfile and relativity
by Chris Nandor