On Sun, Nov 16, 2003 at 08:39:27PM +0100, Michael Lemke wrote:
> I am not so sure either. The observed behavior on VMS led me to think
> catdir could do what I want. But taking this a bit further, how would
> I create a top level directory on Unix with catdir other than having
> the first argument empty? As you say, space and empty is not the same
> and creates different results on Unix:
> catdir('','a','b') -> '/a/b'
> catdir(' ','a','b) -> ' /a/b'
> catdir('a','b') -> 'a/b'
That would appear to be a bug/quirk in the implementation that an empty
string is considered to make it absoulte.
To do it portably you'd use rootdir.
File::Spec->catdir(File::Spec->rootdir, 'a', 'b');
However, I don't know how useful this because VMS doesn't really have
a concept of a single unified root directory like Unix does. The concept
doesn't really map. VMS needs a volume. Unix has no concept of volumes.
Unix has a rootdir. VMS has no concept of a single root dir.
So I still think catpath() is the best way to go. On systems without
volumes it will ignore the volume argument.
File::Spec->catpath('somevolume', 'a', 'b');
But on Unix this does still leave you with a/b. So you simply should throw
in an extra bit of logic...
File::Spec->catdir(File::Spec->rootdir, $path) unless
File::Spec->file_name_is_absolute($path);
VMS and Unix use different concepts for absolute paths, so there's no
single function to map between the two.
> >at File::Spec->splitpath(). As Michael suggested, also look at
> >catpath() for putting the pieces back together.
>
> In fact I had and that is where I got the problem.
>
> A400> typ x.pl
> use File::Spec::Functions qw/splitpath catpath catdir/;
> ($v,$d,$f) = splitpath('root:[a]');
> $x = catpath( $v, catdir( $d, 'b'), $f );
> print "$x\n";
>
> A400> perl x.pl
> root:[a.b]
>
> But:
>
> A400> typ x.pl
> use File::Spec::Functions qw/splitpath catpath catdir/;
> ($v,$d,$f) = splitpath('root:');
> $x = catpath( $v, catdir( $d, 'b'), $f );
> print "$x\n";
>
> A400> perl x.pl
> root:[.b]
>
> And this is only because catdir tosses off the empty $d.
Work around the quirk by stripping empty arguments off.
sub my_catdir {
my @dirs = grep /\S/, @_;
return File::Spec->catdir(@dirs);
}
--
Michael G Schwern [EMAIL PROTECTED] http://www.pobox.com/~schwern/
Hold on while I slip into something a little more naked.