in reply to Re: Any way to simulate a Windows path handling for File::Spec without Windows?
in thread Any way to simulate a Windows path handling for File::Spec without Windows?

I originally had it split with the volume as a separate return value. See original code. But it turns out I don't need it. splitdir includes the volume in the returned array, which works for me.

catfile was needed on *unix to generate the slash when used with an empty string (but it does not work like that on Windows).

$PM = "Perl Monk's";
$MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate Priest Vicar";
$nysus = $PM . ' ' . $MCF;
Click here if you love Perl Monks

  • Comment on Re^2: Any way to simulate a Windows path handling for File::Spec without Windows?
  • Download Code

Replies are listed 'Best First'.
Re^3: Any way to simulate a Windows path handling for File::Spec without Windows?
by jcb (Parson) on Sep 08, 2019 at 05:59 UTC

    That will work on POSIX, where the volume is null, and on Windows, where the volume is syntactically equivalent to a leading directory name. If I recall correctly, that will not work on VMS, where the filename syntax clearly distinguishes between volume, directories, and (leaf) filename, and there may be other systems where that also breaks. It is best to go all the way with File::Spec if you can.

    Of course, if you do not care about portability to VMS, that will not be a problem. :-)

      I'm doing my best to try to use File::Spec but it's giving me headaches when the same method gives me different results on different platforms. PIA. I'll probably just change the algorithm so it looks at raw file path strings instead of directory components. It'll be slightly slower but I won't have to worry about this nonsense.

      $PM = "Perl Monk's";
      $MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate Priest Vicar";
      $nysus = $PM . ' ' . $MCF;
      Click here if you love Perl Monks

        The reason that File::Spec gives different results on different platforms is that File::Spec is intended to adapt to the platform that your program is running on — and different platforms expect different filenames!

        If you are only processing filenames within your program, and do not need to map back from these "internal names" to file names (because you are storing the actual local filenames separately), you could use File::Spec to split the names and then specifically use File::Spec::Unix on all platforms to construct *nix-style "internal names" for processing. If I understand correctly, File::Spec::Unix will load on all platforms, so it is always available.