为什么我的文件句柄 chdir 在 Perl 中不起作用?
当我尝试使用文件句柄作为参数的“chdir”时,“chdir”返回 0 并且 pwd
返回相同的目录。应该是这样吗?
我尝试了这个,因为在 chdir 的文档中我发现:
“在支持 fchdir 的系统上,您 可能传递文件句柄或目录 处理作为参数。在系统上 不支持 fchdir、传递句柄 在运行时产生致命错误。”
稍后给出:
#!/usr/bin/perl -w
use 5.010;
use strict;
use Cwd;
say cwd(); # /home/mm
open( my $fh, '>', '/home/mm/Documents/foto.jpg' ) or die $!;
say chdir $fh; # 0
say cwd(); # /home/mm
我认为这可能会 chdir 到文件的目录 - 但这里对我来说没有 DWIM。
When I try a "chdir" with a filehandle as argument, "chdir" returns 0 and a pwd
returns still the same directory. Should that be so?
I tried this, because in the documentation to chdir I found:
"On systems that support fchdir, you
might pass a file handle or directory
handle as argument. On systems that
don't support fchdir, passing handles
produces a fatal error at run time."
Given later:
#!/usr/bin/perl -w
use 5.010;
use strict;
use Cwd;
say cwd(); # /home/mm
open( my $fh, '>', '/home/mm/Documents/foto.jpg' ) or die $!;
say chdir $fh; # 0
say cwd(); # /home/mm
I thought that this would maybe chdir to the directory of the file - but no DWIM for me here.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
它还表示
您对
chdir
的调用失败。检查$!
变量以获取有关发生情况的线索。由于您没有遇到致命的运行时错误,因此您不必担心最后一段有关fchdir
的问题。运行几个测试,我发现当 FILEHANDLE 引用目录而不是常规文件时,chdir FILEHANDLE 可以工作。希望有帮助:
It also says
meaning that your call to
chdir
failed. Check the$!
variable for a clue about what happened. Since you didn't get a fatal runtime error, you don't have to worry about that last paragraph aboutfchdir
.Running a couple of tests, I see
chdir FILEHANDLE
works whenFILEHANDLE
refers to a directory, but not to a regular file. Hope that helps:哪个版本的
perl
?哪个操作系统?Windows 上的 5.10.1:
输出:
Linux 上的 5.10.1(
/home/sinan/test
是一个目录):Which version of
perl
? Which operating system?5.10.1 on Windows:
Output:
5.10.1 on Linux (
/home/sinan/test
is a directory):对我有用。 Windows 不支持 fchdir,它实际上是一个致命错误:
产生一个致命错误。因此,看起来在根本不支持 fchdir 的系统上,它可以按照规范运行。看起来措辞可以澄清,尤其是“可能”一词。
Works for me. Windows doesn't support fchdir and it is in fact a fatal error there:
produces a fatal error. So it looks like on systems that have no support for fchdir at all it works to spec. Looks like the wording could be cleared up especially the word "might."