Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Perl 5:无法获取文件句柄描述符_Perl_File_File Descriptor - Fatal编程技术网

Perl 5:无法获取文件句柄描述符

Perl 5:无法获取文件句柄描述符,perl,file,file-descriptor,Perl,File,File Descriptor,test.pl: #!/usr/bin/perl use warnings; BEGIN { *CORE::GLOBAL::close = sub (;*) { my @Args = @_; my $FileNO = fileno($Args[0]); print $FileNO, "\n"; CORE::close(@Args); }; } use lib '.'; use Test; 下午六时 package Test; op

test.pl:

#!/usr/bin/perl

use warnings;

BEGIN {
  *CORE::GLOBAL::close = sub (;*) {
      my @Args = @_;
      my $FileNO = fileno($Args[0]);
      print $FileNO, "\n";
      CORE::close(@Args);
  };
}

use lib '.';
use Test;
下午六时

package Test;

open(XXX, 'test.pl');
close(XXX);

1;
当我运行这个程序(
perl test.pl
)时,我得到:


错误在哪里?

您正在将整个
@Args
数组传递到
CORE::close
中,该数组不是文件句柄。您需要传入数组的正确元素。下面是一个从命令行获取文件名并执行正确操作的示例:

use warnings;
use strict;

BEGIN {
  *CORE::GLOBAL::close = sub (;*) {
      my @Args = @_;
      my $FileNO = fileno($Args[0]);
      print $FileNO, "\n";

      # the next line is where the issue is

      CORE::close($Args[0]);
  };
}

die "need file param" if ! @ARGV;

my $file = $ARGV[0];

open my $fh, $file or die $!;

close $fh or die $!;
我会把你的东西更进一步。首先,我将重新调整内容,使
核心
函数的覆盖在
.pm
文件中,而不是在测试文件中,然后我将切换到使用词汇文件句柄,因为全局裸字句柄在这里不起作用:

Test.pm

package Test;

BEGIN {
   *CORE::GLOBAL::close = sub (;*) {
      my @Args = @_;
      my $FileNO = fileno($Args[0]);
      print $FileNO, "\n";
      CORE::close($Args[0]);
  };
}

1;
脚本文件:

use warnings;
use strict;

use lib '.';
use Test;

open my $fh, 'test.pl' or die $!;
close $fh or die $!;
在迁移代码和使用词法文件句柄之间,事情应该如您所期望的那样进行

运行脚本的输出:

3
use warnings;
use strict;

BEGIN {
  *CORE::GLOBAL::close = sub (;*) {
      my @Args = @_;
      my $FileNO = fileno($Args[0]);
      print $FileNO, "\n";
      CORE::close($Args[0]);
  };
}

use lib '.';
use Test;
…为了确保一切正常,我将返回您的默认配置,其中覆盖在测试脚本中,而不是模块中:

Test.pm

package Test;

open my $fh, 'test.pl' or die $!;
close $fh or die $!;

1;
…还有剧本:

3
use warnings;
use strict;

BEGIN {
  *CORE::GLOBAL::close = sub (;*) {
      my @Args = @_;
      my $FileNO = fileno($Args[0]);
      print $FileNO, "\n";
      CORE::close($Args[0]);
  };
}

use lib '.';
use Test;
输出:

4

Test.pm中缺少
使用严格的
(和
使用警告)。不要调用自己的模块
Test.pm
;已经有了一个标准的
测试
模块
close(@Args)
是错误的,因为
close
对其参数施加了标量上下文。另请查看并搜索
qualify\u to\u ref
@melpomene如果
close(@Args)
是错误的,那么什么是正确的?
close($Args[0])
您根本没有回答问题;您只是简单地更改了测试代码,这样就不会再发生了!!!即使进行了更改,OP的代码也无法获取fileno,也无法实际关闭文件句柄@ikegami OPs的问题是“错误在哪里”。基于此,我尽了最大努力。我修正了最明显的错误。ikegami补充道:“修复应该在哪里实施?”。这样更好吗?不,对OP的代码来说还是不起作用。梅尔波梅尼实际上在一次采访中回答了OP的问题comment@ikegami好的,请发布一个答案,或者让我知道我错在哪里。