in reply to Converting all occurrences of \ to \\ oddity

I was doing something like this on win32 (win2k), and I found that if you simply use forward slashes in single quotes, the code becomes easier to read, and DOS still understands it.Such as:

$dirname='c:/dir/textfile.txt'

I eventually wanted to convert the slashes to backslashes for logging (so that I could copy log output directly to a command prompt). I inserted this little bit into the end:
('/' to '\')

foreach (@files){ s/\//\\/g; }
conversely '\' to '/':
foreach (@files){ s/\\/\//g; }
and your answer('\' to '\\'):
foreach (@files){ s/\\/\\\\/g; }

I would still simply avoid this and use the forward slash if you can...

-ozzyosbourne

Replies are listed 'Best First'.
RE: RE: Converting all occurrences of \ to \\ oddity
by Adam (Vicar) on Sep 12, 2000 at 20:50 UTC
    This is true on some Win32 platforms, but not Win2k. Win2k uses a forward slash to start a flag, and does not look for a space before that slash, causing a problem with using forward slashes in your paths. On the other hand it works great for NT4. Just beware if (when) you change to Win2k.

      Actually, it is a problem under Win NT as well. But it is only a problem if you pass the file name on the command line to a program that will interpret the / as you have described. Many, many Win NT programs don't have this problem and even cmd.exe supports using / as a directory separator (but cmd.exe's built-in commands don't). But many (most?) programs from Micrsoft interpret / as a command-line option. If you don't pass the filename to an external command, then the / should always work.

      Based on your wording, it sounds like the problem under Windows 2000 is similar. I haven't played with W2K, so I'd appreciate some clarification.

      Note that there are at least two places in the Win32 API where / can't be used in place of \: Registry calls and DefineDosDevice().

      Update: ...and for the first case, that is why we have Win32::TieRegistry::Delimiter("/").

              - tye (but my friends call me "Tye")