Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

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'

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?

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


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

      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'

        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?

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2021-12-02 07:28 GMT
Find Nodes?
    Voting Booth?
    R or B?

    Results (17 votes). Check out past polls.