Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Directory Slash Format for Perl on Windows

Status
Not open for further replies.

Kirsle

Programmer
Jan 21, 2006
1,179
US
Maybe this will settle the confusion about whether to use a backslash \ or a forward slash / when using a path name for Perl on Windows.

Every Windows I've ever run Perl on (Windows 2000 and newer) have accepted the forward / just fine, even when using absolute path names like "C:/Users/Kirsle/Desktop/test.txt", and I only would've thought that "pure" DOS systems would be the only ones that don't like forward slashes.

So, I decided to test it on DOS. I have a virtual machine that's running MS-DOS 6.22 with Windows 3.1 and I installed Perl 5.003_93 from the CPAN ports page. Here is my test on directory delimiters:

Code:
C:\>cd C:\usr\bin

C:\USR\BIN>perl -v

This is perl, version 5.003_93

Copyright 1987-1997, Larry Wall

OS/2 port Copyright (c) 1990, 1991, Raymond Chen, Kai Uwe Rommel
Version 5 port Copyright (c) 1994-1997, Andreas Kaiser, Ilya Zakharevich

Perl may be copied only under the terms of either the Artistic License or the
GNU General Public License, which may be found in the Perl 5.0 source kit.


C:\USR\BIN>type C:\tests\pathtest.pl
#!/usr/bin/perl -w

print "-== Test #1 ==-\n";
open (WRITE, ">C:/WINDOWS/TEST/ONE.TXT") or warn "Can't write file: $!";
print WRITE "This is test #1 being successful";
close (WRITE);

print "-== Test #2 ==-\n";
open (WRITE, ">C:\\WINDOWS\\TEST\\TWO.TXT") or warn "Can't write file: $!";
print WRITE "This is test #2 being successful";
close (WRITE);

print "-== Test #3 ==-\n";
open (WRITE, ">../../WINDOWS/TEST/THREE.TXT") or warn "Can't write file: $!";
print WRITE "This is test #3 being successful";
close (WRITE);

C:\USR\BIN>perl C:\tests\pathtest.pl
-== Test #1 ==-
-== Test #2 ==-
-== Test #3 ==-

C:\USR\BIN>cd C:\windows\test

C:\WINDOWS\TEST>dir

 Volume in drive C is MS-DOS_6
 Volume Serial Number is 3A4C-5BEF
 Directory of C:\WINDOWS\TEST

.            <DIR>         06-04-09  12:01p
..           <DIR>         06-04-09  12:01p
ONE      TXT            32 06-04-09  12:09p
TWO      TXT            32 06-04-09  12:09p
THREE    TXT            32 06-04-09  12:09p
        5 file(s)             96 bytes
                     995,213,312 bytes free

C:\WINDOWS\TEST>type one.txt
This is test #1 being successful
C:\WINDOWS\TEST>type two.txt
This is test #2 being successful
C:\WINDOWS\TEST>type three.txt
This is test #3 being successful
C:\WINDOWS\TEST>

The results? Forward slashes and backward slashes are interchangeable. Also note that the "../../" format worked fine too, even though (even nowadays in the Windows 7 command prompt), it doesn't like you to use that format on the command line.

Code:
C:\USR\BIN>cd ../../WINDOWS/TEST
Invalid switch - /..

So, use forward slashes in your path names in Perl: it's easier to read (/ vs \\) and is portable to non-Windows platforms, provided you use relative pathnames and not drive letters.

Cuvou.com | My personal homepage
Code:
perl -e '$|=$i=1;print" oo\n<|>\n_|_";x:sleep$|;print"\b",$i++%2?"/":"_";goto x;'
 
Windows stopped shipping with DOS a long while back. DOS has always had the capability of using forward slashes in dorectery paths (at least after DOS 1 which was only run on a floppy disk so there were no directories, just the root directory).

It depends on how you run a program with DOS. If you run it from the terminal you are using cmd.exe which does not support the backslash for directory paths, only as switches. When you run a perl program using DOS you are not using cmd.exe.

Rule of thumb is use backslashes in DOS for paths and forward slashes for switches. For Windows it really does not matter, forward slashes will work in nearly all applications, either at the kernel level or the API level.

But to perl it does matter. The backslash is a meta character and has to be used properly.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top