http://www.perlmonks.org?node_id=711894

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:


What gives?

Replies are listed 'Best First'.
Re: catfile/catdir collapses two leading slashes //
by ikegami (Patriarch) 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

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

    \\special\semantic\path\definition
      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!