为什么文件句柄的chdir在Perl中不起作用?
当我尝试使用filehandle作为参数的“chdir”时,“chdir”返回0,而为什么文件句柄的chdir在Perl中不起作用?,perl,chdir,Perl,Chdir,当我尝试使用filehandle作为参数的“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, '>
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
我想这可能会指向文件的目录,但这里没有DWIM
It returns true upon success, false otherwise.
它还说
It returns true upon success, false otherwise.
perl的哪个版本?哪个操作系统 5.10.1在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: $!";
perl的哪个版本?哪个操作系统 5.10.1在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: $!";
对我有用。Windows不支持fchdir,事实上这是一个致命错误:
perl -we"opendir my $fh, 'temp'; chdir $fh or print 'foo'"
产生致命错误。因此,在完全不支持fchdir的系统上,它的工作符合规范。看起来措辞可以被澄清,尤其是“可能”这个词。对我来说,这很有用。Windows不支持fchdir,事实上这是一个致命错误:
perl -we"opendir my $fh, 'temp'; chdir $fh or print 'foo'"
产生致命错误。因此,在完全不支持fchdir的系统上,它的工作符合规范。看起来措辞可能会被澄清,尤其是“可能”一词这是哪种语言/环境?显示问题的小演示脚本在哪里?这是哪种语言/环境?显示问题的小演示脚本在哪里?Tsk Tsk,您使用的是全局typeglob而不是本地词汇:)我不知道,那个文件句柄可以引用一个目录。@sid_com这不是一个有用的功能,但在某种抽象层次上,Un*x目录只是另一个文件句柄,这是一个旧的坏习惯,我仍然沉迷于简短的一次性程序。至少我开始使用3 arg版本的
open
Tsk-Tsk,您使用的是全局类型glob而不是本地词法:)我不知道,filehandle可以引用目录。@sid_com这不是一个有用的功能,但在某种抽象级别上,Un*x目录只是另一个FileOrry目录,这是一个老坏习惯,我仍然沉迷于短期一次性节目。至少我开始使用3 arg版本的open
perl -we"opendir my $fh, 'temp'; chdir $fh or print 'foo'"