P is for Practical PerlMonks

### catfile/catdir collapses two leading slashes //

by repellent (Priest)
 on Sep 17, 2008 at 07:09 UTC ( #711894=perlquestion: print w/ replies, xml ) Need Help??
repellent has asked for the wisdom of the Perl Monks concerning the following question:

```use File::Spec::Functions qw(catfile splitdir);

my @pathnames = splitdir '//special/semantic//path/definition';

my \$path1 = catfile @pathnames;   # '/special/semantic/path/definition
+'
my \$path2 = join '/', @pathnames; # '//special/semantic//path/definiti
+on'
[download]```

But in Files and Filesystems:

Don't assume that in pathnames you can collapse two leading slashes // into one: some networking and clustering filesystems have special semantics for that. Let the operating system to sort it out.

What gives?

Comment on catfile/catdir collapses two leading slashes //
Select or Download Code
Replies are listed 'Best First'.
Re: catfile/catdir collapses two leading slashes //
by ikegami (Pope) on Sep 17, 2008 at 18:25 UTC

Since File::Spec::Functions is system specific, the warning ("Don't assume ...") doesn't apply.

Since you are using a unix system, "//" can be collapsed, and you get

```/special/semantic/path/definition
[download]```

Since I use a Windows system, leading "\\" is special, and I get

```\\special\semantic\path\definition
[download]```
Then I would suggest Files and Filesystems be updated. After all, it did mention "two leading slashes".

I was also confused, because it mentioned "networking and clustering filesystems". These filesystems are not platform-specific. Shouldn't caution be taken every time, since a *nix system could potentially access a Windows network FS?

It's only used by the volume portion of UNC paths. If you were to mount a Windows share onto a unix system, I don't see why you'd use "\\" (or "//") anywhere. For example, if \\server\docs was mounted as /mount/docs, I presume \\server\docs\some\path would be addressed as /mount/docs/some/path.

"Files and Filesystems" is correct. You shouldn't assume you can't. That doesn't mean you never can.

Re: catfile/catdir collapses two leading slashes //
by haoess (Curate) on Sep 17, 2008 at 07:16 UTC

The name of a directory is not the same as its pathname, so why don't you use splitpath and catpath?

-- Frank

Sure. But let's not stray off-topic :)
```use File::Spec::Functions qw(catdir catpath splitdir splitpath);

my (\$volume, \$directories, \$file) = splitpath '//special/semantic//pat
+h/definition';

my @dir = splitdir \$directories;

my \$dir1 = catdir @dir;    # '/special/semantic/path'
my \$dir2 = join '/', @dir; # '//special/semantic//path/'

my \$path1 = catpath \$volume, \$dir1, \$file; # '/special/semantic/path/d
+efinition'
my \$path2 = catpath \$volume, \$dir2, \$file; # '//special/semantic//path
+/definition'
[download]```

The name of a directory is not the same as its pathname

There's always confusion with file nomenclature. See File system nomenclature. Death to path!

Log In?
 Username: Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://711894]
Approved by Corion
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (13)
As of 2015-09-02 17:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?