gpt4 book ai didi

perl - 如何取消引用 STDIN 文件句柄的副本?

转载 作者:行者123 更新时间:2023-12-04 12:11:12 24 4
gpt4 key购买 nike

我试图弄清楚如何让 Perl 模块受到尊重并打开对文件句柄的引用。当你看到主程序时,你就会明白我的意思:

#!/usr/bin/perl
use strict;
use warnings;

use lib '/usr/local/share/custom_pm';
use Read_FQ;

# open the STDIN filehandle and make a copy of it for (safe handling)
open(FILECOPY, "<&STDIN") or die "Couldn't duplicate STDIN: $!";

# filehandle ref
my $FH_ref = \*FILECOPY;

# pass a reference of the filehandle copy to the module's subroutine
# the value the perl module returns gets stored in $value
my $value = {Read_FQ::read_fq($FH_ref)};

# do something with $value

基本上,我希望主程序通过 STDIN 接收输入,制作 STDIN 文件句柄的副本(用于安全处理),然后将该副本的引用传递给 Read_FQ.pm 文件(perl 模块)中的 read_fq() 子例程。然后子程序将从该文件句柄中读取输入,对其进行处理并返回一个值。这里是 Read_FQ.pm 文件:
package Read_FQ;

sub read_fq{
my ($filehandle) = @_;
my contents = '';

open my $fh, '<', $filehandle or die "Too bad! Couldn't open $filehandle for read\n";
while (<$fh>) {
# do something
}
close $fh;
return $contents;

这就是我遇到麻烦的地方。在终端中,当我将文件名传递给主程序以打开时:
cat file.txt | ./script01.pl
它给出以下错误消息: Too bad! Couldn't open GLOB(0xfa97f0) for read
这告诉我问题是我如何取消引用并打开对 perl 模块中文件句柄的引用。主程序没问题。我读到 $refGlob = \*FILE;是对文件句柄的引用,在大多数情况下,应该由 Perl 自动取消引用。但是,这里不是这种情况。有谁知道如何取消引用文件句柄 ref 以便我可以处理它?

谢谢。非常感谢任何建议。

最佳答案

您的 $filehandle应该已经打开 - 你打开了 FILECOPY ,取了个引用,放到$FH_ref ,即 $filehandle .如果您想再次打开它,请使用 <& open 中的参数或者立即开始阅读。

关于perl - 如何取消引用 STDIN 文件句柄的副本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11512318/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com