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

Your code does not work on Windows because you are misusing File::Spec. File::Spec->splitpath returns three items, not two. On *nix, the filename can end up with the directory in the middle, which is why your code appears to work in that case. Read the File::Spec documentation very carefully, and I think you will be able to fix this easily.

Also, you are splitting the directory component, but then using catfile instead of catdir to reassemble those names. Think very carefully about types and what your strings mean, and I think that you will find a solution.

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

    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

      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