|
Posted by PerlFAQ Server on March 13, 2008, 3:03 pm
Please log in for more thread options
This is an excerpt from the latest version perlfaq5.pod, which
comes with the standard Perl distribution. These postings aim to
reduce the number of repeated questions as well as allow the community
to review and update the answers. The latest version of the complete
perlfaq is at http://faq.perl.org .
--------------------------------------------------------------------
5.35: Why can't I use "C:\temp\foo" in DOS paths? Why doesn't `C:\temp\foo.exe`
work?
Whoops! You just put a tab and a formfeed into that filename! Remember
that within double quoted strings ("like\this"), the backslash is an
escape character. The full list of these is in "Quote and Quote-like
Operators" in perlop. Unsurprisingly, you don't have a file called
"c:(tab)emp(formfeed)oo" or "c:(tab)emp(formfeed)oo.exe" on your legacy
DOS filesystem.
Either single-quote your strings, or (preferably) use forward slashes.
Since all DOS and Windows versions since something like MS-DOS 2.0 or so
have treated "/" and "\" the same in a path, you might as well use the
one that doesn't clash with Perl--or the POSIX shell, ANSI C and C++,
awk, Tcl, Java, or Python, just to mention a few. POSIX paths are more
portable, too.
--------------------------------------------------------------------
The perlfaq-workers, a group of volunteers, maintain the perlfaq. They
are not necessarily experts in every domain where Perl might show up,
so please include as much information as possible and relevant in any
corrections. The perlfaq-workers also don't have access to every
operating system or platform, so please include relevant details for
corrections to examples that do not work on particular platforms.
Working code is greatly appreciated.
If you'd like to help maintain the perlfaq, see the details in
perlfaq.pod.
|
|
Posted by szr on March 15, 2008, 3:56 pm
Please log in for more thread options
PerlFAQ Server wrote:
[...]
> Either single-quote your strings, or (preferably) use forward
> slashes. Since all DOS and Windows versions since something like
> MS-DOS 2.0 or so have treated "/" and "\" the same in a path,
Not all DOS and Windows applications treat "/" the same as "\" in a
path:
Different:
C:\Temp>cd /
C:\Temp>cd \
C:\>
C:\>dir C:\temp
Volume in drive C is SR 1
Volume Serial Number is 1D73-1B0A
Directory of C:\temp
01/26/2005 15:43 PM <DIR> .
01/26/2005 15:43 PM <DIR> ..
[...]
2 File(s) 2,188 bytes
2 Dir(s) 2,678,390,784 bytes free
C:\>dir C:/temp
Parameter format not correct - "emp".
Same:
C:\>attrib C:\temp
C:\Temp
C:\>attrib C:/temp
C:\Temp
Point being you can not take for granted which application does and
doesn't unless you reliably know ahead of time. Not an issue with Perl
of course, and it allows for portable code. Then again, C++ also allowed
"/" in path names IIRC.
--
szr
|
|
Posted by Peter J. Holzer on March 15, 2008, 4:32 pm
Please log in for more thread options > PerlFAQ Server wrote:
> [...]
>> Either single-quote your strings, or (preferably) use forward
>> slashes. Since all DOS and Windows versions since something like
^^^^^^^^
>> MS-DOS 2.0 or so have treated "/" and "\" the same in a path,
>
> Not all DOS and Windows applications treat "/" the same as "\" in a
^^^^^^^^^^^^
> path:
Please note the difference between "all DOS and Windows versions" and
"all DOS and Windows applications".
>
> Different:
>
> C:\Temp>cd /
>
> C:\Temp>cd \
It is well known that the DOS/Windows shell treats them differently.
That's irrelevant for perl except for those functions which invoke a
shell (system, qx, open pipe, ...).
In a perl script,
chdir('/');
works just the same as
chdir('\');
> Point being you can not take for granted which application does and
> doesn't unless you reliably know ahead of time.
"Applications" are not the point of this FAQ.
> Not an issue with Perl of course,
Then why do you mention this issue?
> and it allows for portable code. Then again, C++ also allowed "/" in
> path names IIRC.
Almost certainly, yes. The *OS itself* allows it, so it works in any
program which doesn't actively prevent it.
hp
|
|
Posted by Ben Morrow on March 15, 2008, 4:50 pm
Please log in for more thread options
> PerlFAQ Server wrote:
> [...]
> > Either single-quote your strings, or (preferably) use forward
> > slashes. Since all DOS and Windows versions since something like
> > MS-DOS 2.0 or so have treated "/" and "\" the same in a path,
>
> Not all DOS and Windows applications treat "/" the same as "\" in a
> path:
All Win32 API calls allow either / or \. cmd.exe, and programs designed
to be called from cmd (in particular, programs with DOS-style /FOO
options) insist on \. Some NT API calls require \, but AFAIK it's not
possible to call these from Perl.
A simple rule for a Perl program is anything that is invoking an
external command (including Win32::Process) should use solely \, and
anything else should use solely /.
Ben
|
| Similar Threads | Posted | | FAQ 5.33: Why can't I use "C:\temp\foo" in DOS paths? Why doesn't `C:\temp\foo.exe` work? | December 12, 2004, 12:03 am |
| FAQ 5.33 Why can't I use "C:\temp\foo" in DOS paths? Why doesn't `C:\temp\foo.exe` work? | February 8, 2005, 12:03 am |
| FAQ 5.34 Why can't I use "C:\temp\foo" in DOS paths? Why doesn't `C:\temp\foo.exe` work? | April 25, 2005, 5:03 pm |
| FAQ 5.34 Why can't I use "C:\temp\foo" in DOS paths? Why doesn't `C:\temp\foo.exe` work? | July 11, 2005, 4:03 pm |
| FAQ 5.34 Why can't I use "C:\temp\foo" in DOS paths? Why doesn't `C:\temp\foo.exe` work? | September 26, 2005, 10:03 am |
| FAQ 5.34 Why can't I use "C:\temp\foo" in DOS paths? Why doesn't `C:\temp\foo.exe` work? | December 3, 2005, 5:03 am |
| FAQ 5.34 Why can't I use "C:\temp\foo" in DOS paths? Why doesn't `C:\temp\foo.exe` work? | December 19, 2005, 5:03 am |
| FAQ 5.34 Why can't I use "C:\temp\foo" in DOS paths? Why doesn't `C:\temp\foo.exe` work? | May 4, 2006, 3:03 pm |
| FAQ 5.34 Why can't I use "C:\temp\foo" in DOS paths? Why doesn't `C:\temp\foo.exe` work? | August 22, 2006, 9:03 am |
| FAQ 5.34 Why can't I use "C:\temp\foo" in DOS paths? Why doesn't `C:\temp\foo.exe` work? | November 16, 2006, 3:03 pm |
|