windows 为什么 Perl 会抱怨“包含换行符的文件名统计失败”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2652261/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 14:17:33  来源:igfitidea点击:

Why does Perl complain about "Unsuccessful stat on filename containing newline"?

windowsperl

提问by Daniel

I am getting an error I do not understand. I am using File:Find to recurse a fylesystem on Windows using Activestate Perl 5.8.8 and trying to stat $File::Find::name; so I am not stat-ing a filename got from a text file scanning requiring chomp-ing or newline removing. I was unable to get file modification time, the mtimein:

我收到一个我不明白的错误。我正在使用 File:Find 在 Windows 上使用 Activestate Perl 5.8.8 递归 fylesystem 并尝试 stat $File::Find::name;所以我不是stat-ing 从需要chomp-ing 或换行符删除的文本文件扫描中获得的文件名。我无法获得文件修改时间,mtime在:

my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks) = stat($File::Find::name);

so trying a

所以尝试一个

-s $File::Find::name 

gives me the error:

给我错误:

Unsuccessful stat on filename containing newline

包含换行符的文件名统计失败

A typical file name found is F01-01-10 Num 0-00000.pdfbut I get the same error even renaming in E02.pdf

找到的典型文件名是F01-01-10 Num 0-00000.pdf但即使在E02.pdf 中重命名我也遇到相同的错误

回答by rjh

According to perldiagif anyfile operation fails and the filename happens to contain a newline character, the warning "Unsuccessful on filename containing newline" will be emitted.

根据perldiag,如果任何文件操作失败并且文件名恰好包含换行符,则会发出警告“文件名包含换行符不成功”。

The assumption is that, as you say, the filename has come from standard input or similar, and the user has forgotten to chompthe newline away. You might want to pass the string through chompanyway, just to see if it works.

假设是,如您所说,文件名来自标准输入或类似输入,并且用户忘记chomp了换行符。chomp无论如何,您可能想要传递字符串,只是为了看看它是否有效。

There is some evidencethat &CORE::statmtime might be broken with some combinations of OS patchlevel and ActiveState Perl versions - a suggested workaround is to use the File::statmodule like so:

一些证据表明&CORE::statmtime 可能会因操作系统补丁级别和 ActiveState Perl 版本的某些组合而被破坏 - 建议的解决方法是使用File::stat模块,如下所示:

my $sb = stat($File::Find::name);
my $mtime = scalar localtime $sb->mtime;

...you might find File::stat's object representation to be more convenient than the list returned by CORE::stat.

...您可能会发现 File::stat 的对象表示比由 返回的列表更方便CORE::stat

回答by Gayathri

Even i got the same error while trying to delete a folder. i used chomp before using rmtree command. That solved my problem.

即使我在尝试删除文件夹时也遇到了同样的错误。我在使用 rmtree 命令之前使用了 chomp。那解决了我的问题。

回答by Mesa

Ran into this same problem in my script and found my mistake to be when i created the file i tagged a date stamp to the end and forgot to chomp my $DATE variable before adding it to the file name.

在我的脚本中遇到了同样的问题,发现我的错误是当我创建文件时,我在最后标记了一个日期戳,并且在将它添加到文件名之前忘记了 chomp 我的 $DATE 变量。

回答by Ken

Yep, just ran into this myself with a hand built scalar. Scratched my head a bit until I realized I had included a time/date stamp in the filename. Chomp the date command and voila, problem resolved.

是的,我自己用手工构建的标量遇到了这个问题。挠了挠我的头,直到我意识到我在文件名中包含了时间/日期戳。修改日期命令,瞧,问题解决了。