saintmike has asked for the wisdom of the Perl Monks concerning the following question:

The latest versions of ActiveState Perl (5.8.6+) on Win32 have this strange ambiguity between using slashes or backslashes in path names.

For example, calling

my ($package, $file, $line) = caller();
in a module will result in $file holding something like C:/perl/Mytest.pm (note: forward slashes) while a call to
File::Spec->catfile("C:", "perl", "MyTest.pm")
will return
C:\perl\MyTest.pm
(note: backslashes). This is quite confusing. I guess that either sticking to backslashes or slashes would be OK, but mixing them is definitely wrong.

Anyone know where to complain about this?

Replies are listed 'Best First'.
Re: ActiveState Perl: Forward or Backward Slash?
by Corion (Patriarch) on Sep 20, 2005 at 07:17 UTC

    What is there to complain? The Win32 API understands both the forward and the backward slash as path separator (unlike the prevalent shell, cmd.exe). Of course, this makes writing scripts that check for file name equivalence harder, but if you're doing file name equivalence tests, you have the problem of a case insensitive (but case preserving) file system anyway.

    So, depending on what your actual problem is (besides the aestethic), there is likely a solution in defining and then coding a normalization, but that normalization will likely depend on the OS and your requirements for equality.

      Off topic a little but cmd does understand forward slashes:

      H:\>cd /perl H:\perl>

      A lot of command line utilties don't though, so your point is valid, in that if you use system() to run external Windows programs then you should probably use escaped backslashes unless you know it's going to work. I've been caught out a few times :)

        puploki's screen capture appears to demo cmd's behavior under WinXP;
        monarch asserts [perhaps just a tad abrasively] what I see under w2k (and, IIRC, earlier 'doze).
        What are you smoking?
        D:\Temp>cd /temp The syntax of the command is incorrect. D:\Temp>cd \temp D:\Temp>

        Updated: tested on WinNT

      Just because an external entity accepts both forms doesn't mean that two perl-internal functions should return the value of the same external entity in two different forms.

      You still wouldn't complain if caller() returned C:/foo/bar or C:\FoO\BaR, depending on if it's April 1st or not? ;)

      Seriously, the problem is that in the current implementation, it's unnecessarily complex to check the return value of caller() in a platform-independent way. File::Spec->catfile() offers this canonical way, but doesn't work in this case.

Re: ActiveState Perl: Forward or Backward Slash?
by polypompholyx (Chaplain) on Sep 20, 2005 at 09:51 UTC

    Just to make it even more confusing, you can happily mix up backslashes and forward slashes within Perl...

    print "Yes\n" if -e "C:\\WINNT/System32\\cmd.exe";

    Yes

    Although, as other posters have said, running system with anything other than pure backslashes is likely not to work.

      uhmmm, er...

            "...can happily mix...."

      Slap_happily?

Re: ActiveState Perl: Forward or Backward Slash?
by ikegami (Patriarch) on Sep 20, 2005 at 14:15 UTC

    File::Spec uses the official "\". Other Perl functions.... not so well ported. You can always canonize your paths:

    use File::Spec; print File::Spec->canonpath('C:/perl/Mytest.pm'); # Prints: C:\perl\Mytest.pm
Re: ActiveState Perl: Forward or Backward Slash?
by QM (Parson) on Sep 20, 2005 at 18:48 UTC
    Somewhat OT...

    My documentation assumes my users don't know there is a slash/backslash issue, and tells them explicitly to use slashes. Otherwise many (most?) of them would use backslashes, but only single backslashes, and then complain the script is broken.

    Getting strings back from other code, or feeding strings to other code (e.g., system) should be checked. If you do this a lot, you might create some data structure that you can use to filter requests and returns. When something changes, you change your File::Backslash module. (Or better, have it do the right thing based on versions.)

    -QM
    --
    Quantum Mechanics: The dreams stuff is made of