为什么此路径无法在 PERL 中打开 Windows 文件?

发布于 2025-01-05 07:16:13 字数 203 浏览 7 评论 0 原文

我尝试使用 Strawberry Perl,但困扰我的事情之一就是读取文件。

我尝试这样做:

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

但它找不到该文件(尽管该文件在那里,并且没有权限问题)。

等效代码(除文件名之外的脚本 100% 相同)在 Linux 上运行良好。

I tried to play with Strawberry Perl, and one of the things that stumped me was reading the files.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

诗酒趁年少 2025-01-12 07:16:13

根据 Perl FAQ 5,您应该在 DOS/Windows 文件名中使用正斜杠(或者,作为替代方案,转义反斜杠)。

为什么我不能在 DOS 路径中使用“C:\temp\foo”?为什么 `C:\temp\foo.exe` 不起作用?

哎呀!您只需将制表符和换页符放入该文件名中即可!请记住,在双引号字符串(“like\this”)中,反斜杠是转义字符。这些的完整列表位于 perlop 中的 Quote 和 Quote-like Operators 中。毫不奇怪,您的旧 DOS 文件系统上没有名为“c:(tab)emp(formfeed)oo”或“c:(tab)emp(formfeed)oo.exe”的文件。

用单引号引用字符串,或者(最好)使用正斜杠。由于自 MS-DOS 2.0 等版本以来,所有 DOS 和 Windows 版本都在路径中将 / 和 \ 视为相同,因此您不妨使用不与 Perl 或 POSIX shell、ANSI C 和C++、awk、Tcl、Java 或 Python,仅举几例。 POSIX 路径也更可移植。

因此,您的代码应该是 open(FH, "D:/test/numbers.txt"); ,以避免尝试打开名为“D:est\numbers.txt”的文件 顺便说一句


,您可以通过使用词法(而不是全局命名)文件句柄(一种 3 参数形式的 open)来进一步改进您的代码,最重要的是,对所有 IO 操作进行错误检查,尤其是open() 调用:

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

或者,更好的是,不要在 IO 调用中硬编码文件名(以下做法可能会让您更快地找出问题):

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

As per Perl FAQ 5, you should be using forward slashes in your DOS/Windows filenames (or, as an alternative, escaping the backslashes).

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.

So your code should be open(FH, "D:/test/numbers.txt"); instead, to avoid trying to open a file named "D:<TAB>est\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:

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):

my $filename = "D:/test/numbers.txt";
open(my $fh, "<", $filename) or die "Could not open file $filename: $!";
余罪 2025-01-12 07:16:13

当不需要插值时,切勿使用插值字符串!您正在尝试从 \t 和 \n! 打开一个包含制表符和换行符的文件名!

当您不需要(或想要)插值时,请使用单引号。

新手 Perl 程序员似乎遇到的最大问题之一是他们不假思索地自动使用“”来表示所有内容。您需要了解“”和“”之间的区别,并且在键入之前始终需要思考,以便选择正确的选项。这是一个很难养成的习惯,但如果你想写出好的 Perl 语言,这一点就至关重要。

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!

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. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文