简体   繁体   English

为什么这条路径不能在PERL中打开Windows文件?

[英]Why doesn't this path work to open a Windows file in PERL?

I tried to play with Strawberry Perl, and one of the things that stumped me was reading the files. 我尝试使用Strawberry Perl,其中一件难以理解的事情就是阅读文件。

I tried to do: 我试着这样做:

open(FH, "D:\test\numbers.txt");

But it can not find the file (despite the file being there, and no permissions issues). 但它无法找到该文件(尽管该文件存在,并且没有权限问题)。

An equivalent code (100% of the script other than the filename was identical) worked fine on Linux. 一个等效的代码(100%的脚本除了文件名相同)在Linux上运行良好。

As per Perl FAQ 5 , you should be using forward slashes in your DOS/Windows filenames (or, as an alternative, escaping the backslashes). 根据Perl FAQ 5 ,您应该在DOS / Windows文件名中使用斜杠(或者,作为替代方法,转义反斜杠)。

Why can't I use "C:\\temp\\foo" in DOS paths? 为什么我不能在DOS路径中使用“C:\\ temp \\ foo”? Why doesn't `C:\\temp\\foo.exe` work? 为什么`C:\\ temp \\ foo.exe`不起作用?

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. 请记住,在双引号字符串(“like \\ this”)中,反斜杠是一个转义字符。 The full list of these is in Quote and Quote-like Operators in perlop. 这些完整列表在perlop的Quote和Quote-like Operators中。 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. 不出所料,您的旧DOS文件系统上没有名为“c :( tab)emp(formfeed)oo”或“c:(tab)\\ temp(formfeed)oo.exe”的文件。

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. 因为所有DOS和Windows版本都像MS-DOS 2.0那样在路径中处理/和\\相同,所以你也可以使用不与Perl冲突的那个 - 或者POSIX shell,ANSI C和C ++,awk,Tcl,Java或Python,仅举几例。 POSIX paths are more portable, too. POSIX路径也更便携。

So your code should be open(FH, "D:/test/numbers.txt"); 所以你的代码应该是open(FH, "D:/test/numbers.txt"); instead, to avoid trying to open a file named "D:<TAB>est\\numbers.txt" 相反,避免尝试打开名为“D:<TAB> \\ test \\ numbers.txt”的文件


As an aside, you could further improve your code by using lexical (instead of global named) filehandle, a 3-argument form of open, and, most importantly, error-checking ALL your IO operations, especially open() calls: 顺便说一下,你可以通过使用lexical(而不是全局命名的)filehandle,一个3参数形式的open,以及最重要的是,错误检查你的所有IO操作,特别是open()调用来进一步改进你的代码:

open(my $fh, "<", "D:/test/numbers.txt") or die "Could not open file: $!";

Or, better yet, don't hard-code filenames in IO calls (the following practice MAY have let you figure out a problem sooner): 或者,更好的是,不要在IO调用中对文件名进行硬编码(以下练习可能会让您更快地发现问题):

my $filename = "D:/test/numbers.txt";
open(my $fh, "<", $filename) or die "Could not open file $filename: $!";

Never use interpolated strings when you don't need interpolation! 不需要插值时,切勿使用插值字符串! You are trying to open a file name with a tab character and a newline character in it from the \\t and the \\n! 您正尝试从\\ t和\\ n中打开带有制表符和换行符的文件名。

Use single quotes when you want don't need (or want) interpolation. 如果不需要(或想要)插值,请使用单引号。

One of the biggest problems novice Perl programmers seem to run into is that they automatically use "" for everything without thinking. 新手Perl程序员似乎遇到的最大问题之一就是他们会毫不犹豫地自动使用“”。 You need to understand the difference between "" and '' and you need to ALWAYS think before you type so that you choose the right one. 您需要了解“”和“”之间的区别,并且在键入之前需要始终考虑,以便选择正确的。 It's a hard habit to get into, but it's vital if you're going to write good Perl. 这是一个很难进入的习惯,但如果你要编写好的Perl,这一点至关重要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM