简体   繁体   English

为什么perl测试dir -d“”在Windows上返回true? 是不是有错误?

[英]why perl testing dir -d “ ” on windows returns true? bug or not?

Is there any explanation for perl returning true when testing a space-string dir on Windows? 在Windows上测试空间字符串目录时,是否有任何关于perl返回true的解释? Run on Windows7: 在Windows7上运行:

  perl -e "print qq{found\n} if -d qq{ }"

You will get output: found 你会得到输出: found

But same perl code returns false on Linux. 但是同样的perl代码在Linux上返回false。

Tested on perl 5.8 and strawberry perl 5.18 on Windows 在Windows上测试perl 5.8和草莓perl 5.18

Is it a bug or have an unconventional reasoning? 这是一个错误还是一个非传统的推理?

Under windows, any perl operation that internally tries to test a file or directory for existence uses the Win32 function CreateFile . 在Windows下,内部尝试测试存在的文件或目录的任何perl操作都使用Win32函数CreateFile Under windows a filename ending in spaces is not legal (although not clearly documented), and for some strange reason the CreateFile function internally strips all trailing spaces before trying to open the file/directory. 在windows下,以空格结尾的文件名不合法(虽然没有明确记录),并且由于某些奇怪的原因, CreateFile函数在尝试打开文件/目录之前在内部删除所有尾随空格。

Since a name starting with space looks like a relative path, the space is first appended to your current working directory but then internally ignored by the Win32 function. 由于以空格开头的名称看起来像一个相对路径,因此该空间首先会附加到当前工作目录,但在Win32函数内部会被忽略。 This results in the directory test seeing your current directory and reporting success. 这导致目录测试查看当前目录并报告成功。

The perl stat function then proceeds to acquiring additional information and seems to somewhere down the line handle the trailing space(s) differently and therefore fails to get any further information. 然后perl stat函数继续获取附加信息,并且似乎在线下某处处理尾随空格不同,因此无法获得任何进一步的信息。 However it seems to explicitly leave the mode attribute of the stat result set, because it earlier had deduced that there existed a directory. 但是它似乎明确地保留了stat结果集的mode属性,因为它之前已经推断出存在一个目录。

So yes, I would call this a bug in the windows port of perl, but fixing it is probably not as easy as it sounds, as there are lots of special cases for UNC paths, reparse points, NTFS/FAT filesystems etc. that makes correct handling of trailing spaces rather tricky. 所以,是的,我会把它称为perl的windows端口中的一个错误,但修复它可能并不像听起来那么容易,因为UNC路径,重新分析点,NTFS / FAT文件系统等有很多特殊情况。正确处理尾随空格相当棘手。

Your best bet would probably be to explicitly strip trailing spaces from any assumed directory name at a very early point (who needs that anyway) and croak if nothing is left after stripping. 你最好的选择可能是在很早的时候(无论如何都需要它)从任何假定的目录名中明确地删除尾随空格,如果在剥离后没有留下任何东西则会呱呱叫。

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

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