in reply to Trouble using a Win32 file path in an array

This doesn't appear to have been mentioned before so here is a Windows tidbit:

Perl on windows can use "/" instead of the windows backslash in path names just fine. So when calling a Perl function on Windows, C:/dir/dir2 is just fine.A Perl function under Windows will also return forward slash names instead of backslash names.

You need the backslash when you go to the windows shell (with backticks, etc), but otherwise use the forwardslash "/". The code is more portable and this solves some problems when running regex'es on pathnames since you don't have to worry about "escaping" the backslash with yet another backslash.

  • Comment on Re: Trouble using a Win32 file path in an array

Replies are listed 'Best First'.
Re^2: Trouble using a Win32 file path in an array
by ikegami (Patriarch) on Sep 28, 2009 at 02:20 UTC

    You need the backslash when you go to the windows shell

    Windows accepts both "\" and "/" as directory separators.

    The shell and many shell commands treat "/" as an option starter, but quoting usually removes that meaning:

    >"c:\progs\perl5101\bin\perl" -le"print 'Hi!'" Hi! >"c:/progs/perl5101/bin/perl" -le"print 'Hi!'" Hi!
    >echo foo > "/foo.txt" >move "/foo.txt" "./bar.txt" >type "c:/documents and settings/ikegami/bar.txt" foo >del "./bar.txt"
      Great info! On older versions of Windows, it didn't work that way, but you are correct on Win XP.

      Update: not quite right...

      C:\PROJECTS\xyz2009>cd c:/projects The system cannot find the path specified. C:\PROJECTS\xyz009>cd c:\projects C:\PROJECTS>
      Another Update:
      Revising my post in response to ikegami's post.
      Apologize for being too cryptic.

      1. First there are a whole lot of Windows things between Win 3.1 and Win XP: Win 98, WinNT, Win2K. I was thinking more about NT and 2K when I made the comment about previous versions of windows. At one time I had all 4 versions (98,NT,2K,XP) on my network for testing, but am down to just XP now. There are lots of quirks between these various versions.

      2. I did some testing at the command prompt on XP and found:

      C:\> C:\>cd projects/replyto C:\PROJECTS\replyto>
      That was a "WOW" moment as I hadn't expected that forward slash to work with the cd command! But it did!

      3. Then I found out that there is something special about the Drive: root- the forward slash doesn't work in this situation:

      C:\PROJECTS\replyto>cd c:/projects The system cannot find the path specified. (didn't work) C:\PROJECTS\replyto>cd c:\projects (does work) C:\PROJECTS>
      That little discovery is what prompted the update: "not quite right" comment.

      4. More examples:

      C:\PROJECTS>type testing/test.pl (doesn't work) The syntax of the command is incorrect. C:\PROJECTS>type "testing/test.pl" (doesn't work either) The system cannot find the file specified. C:\PROJECTS>type "testing\test.pl" (works) C:\PROJECTS>type testing\test.pl (works) C:\PROJECTS>cd ./testing (works) C:\PROJECTS\testing> C:\PROJECTS>cd /testing (works) C:\PROJECTS\testing>
      Evidently sometimes the "forwards slash" works at the command line and sometimes it doesn't. There appears to be something special about drive root and I suppose some of these commands like "type" can get confused between path and "/" options.

      5. In an attempt to try and avoid confusion, when writing Perl, I always use forward slash. That works with all Perl functions even with a path like c:/projects. As long as you don't go the the shell with say a command('blah'), forward slash is the way to go.

        [ In response to the update ]

        there are a whole lot of Windows things between Win 3.1 and Win XP: Win 98, WinNT, Win2K. I was thinking more about NT and 2K when I made the comment about previous versions of windows.

        Yes, except you talked of older versions where "/" wasn't accepted as a directory separator. I'm pretty sure none of those qualify.

        sometimes the "forwards slash" works at the command line and sometimes it doesn't.

        Yes, I mentioned that earlier in the thread, in addition to giving the reason and a workaround that works in most instances.

        In an attempt to try and avoid confusion, when writing Perl, I always use forward slash.

        Do you realize that Perl passes paths to Windows exactly as you provide them to Perl? It does NOT convert the slash first.

        On older versions of Windows, it didn't work that way, but you are correct on Win XP.

        Honestly, Windows 3.1 is a whole other world. It wasn't even an OS.

        Not 100% sure, so prove me wrong if a newer Windows doesn't support "/" as the dir separator. Test code below.

        Update: not quite right...

        What isn't quite right? You (falsely) make it sound like something I said was wrong.


        Test to determine if your Windows supports "/" as the directory separator:

Re^2: Trouble using a Win32 file path in an array
by Anonymous Monk on Sep 28, 2009 at 01:10 UTC
    A Perl function under Windows will also return forward slash names instead of backslash names.

    Depends on the function.