为什么我的文件句柄 chdir 在 Perl 中不起作用?

发布于 2024-08-11 02:34:37 字数 499 浏览 6 评论 0原文

当我尝试使用文件句柄作为参数的“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 技术交流群。

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

发布评论

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

评论(3

关于从前 2024-08-18 02:34:37

它还表示

It returns true upon success, false otherwise.

您对 chdir 的调用失败。检查 $! 变量以获取有关发生情况的线索。由于您没有遇到致命的运行时错误,因此您不必担心最后一段有关 fchdir 的问题。


运行几个测试,我发现当 FILEHANDLE 引用目录而不是常规文件时,chdir FILEHANDLE 可以工作。希望有帮助:

  open(FH, "<", "/tmp/file");  # assume this file exists
  chdir FH and print "Success 1\n" or warn "Fail 1: $!\n";
  open(FH, "<", "/tmp");
  chdir FH and print "Success 2\n" or warn "Fail 2: $!\n";
  opendir(FH, "/tmp");
  chdir FH and print "Success 3\n" or warn "Fail 3: $!\n";

 

  Fail 1: Not a directory
  Success 2
  Success 3

It also says

It returns true upon success, false otherwise.

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


Running a couple of tests, I see chdir FILEHANDLE works when FILEHANDLE refers to a directory, but not to a regular file. Hope that helps:

  open(FH, "<", "/tmp/file");  # assume this file exists
  chdir FH and print "Success 1\n" or warn "Fail 1: $!\n";
  open(FH, "<", "/tmp");
  chdir FH and print "Success 2\n" or warn "Fail 2: $!\n";
  opendir(FH, "/tmp");
  chdir FH and print "Success 3\n" or warn "Fail 3: $!\n";

 

  Fail 1: Not a directory
  Success 2
  Success 3
罪歌 2024-08-18 02:34:37

哪个版本的 perl?哪个操作系统?

Windows 上的 5.10.1:

#!/usr/bin/perl

use strict; use warnings;

# have to use a file because Windows does not let 
# open directories as files
# only done so I can illustrate the fatal error on
# a platform where fchdir is not implemented

open my $fh, '<', 'e:/home/test.txt'
    or die "Cannot open file: $!";

chdir $fh
    or die "Cannot chdir using filehandle: $!";

输出:

C:\Temp> k
The fchdir function is unimplemented at C:\Temp\k.pl line 9.

Linux 上的 5.10.1(/home/sinan/test 是一个目录):

$ cat k.pl
#!/usr/bin/perl

use strict; use warnings;

use Cwd;

open my $fh, '<', '/home/sinan/test'
    or die "Cannot open file: $!";

chdir $fh
    or die "Cannot chdir using filehandle: $!";

print getcwd, "\n";

$ ./k.pl
/home/sinan/test

Which version of perl? Which operating system?

5.10.1 on Windows:

#!/usr/bin/perl

use strict; use warnings;

# have to use a file because Windows does not let 
# open directories as files
# only done so I can illustrate the fatal error on
# a platform where fchdir is not implemented

open my $fh, '<', 'e:/home/test.txt'
    or die "Cannot open file: $!";

chdir $fh
    or die "Cannot chdir using filehandle: $!";

Output:

C:\Temp> k
The fchdir function is unimplemented at C:\Temp\k.pl line 9.

5.10.1 on Linux (/home/sinan/test is a directory):

$ cat k.pl
#!/usr/bin/perl

use strict; use warnings;

use Cwd;

open my $fh, '<', '/home/sinan/test'
    or die "Cannot open file: $!";

chdir $fh
    or die "Cannot chdir using filehandle: $!";

print getcwd, "\n";

$ ./k.pl
/home/sinan/test

对我有用。 Windows 不支持 fchdir,它实际上是一个致命错误:

perl -we"opendir my $fh, 'temp'; chdir $fh or print 'foo'"

产生一个致命错误。因此,看起来在根本不支持 fchdir 的系统上,它可以按照规范运行。看起来措辞可以澄清,尤其是“可能”一词。

Works for me. Windows doesn't support fchdir and it is in fact a fatal error there:

perl -we"opendir my $fh, 'temp'; chdir $fh or print 'foo'"

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

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