gpt4 book ai didi

perl - 我可以同时读取和写入多个文件句柄 (Perl) 吗?

转载 作者:行者123 更新时间:2023-12-01 12:55:03 33 4
gpt4 key购买 nike

我正在尝试读取两个文件,并在第三个文件中生成输出。我首先想在旅途中编辑第一个,但没有找到合适的数组保存方法。

我的问题是,每当我取消注释“_ref_param_handling”函数时,第三个文件(输出)为空。 但是以下是最让我困惑的:如果我在最后对输出文件执行一个 UNIX 非常基本的 `cat` 系统调用(见下面的代码),它工作得很好。如果我刚刚打开文件句柄并在编辑后立即关闭它,它也可以正常工作(围绕我的 print FILEHANDLE LIST)。

毫无疑问,我在这里遗漏了一些东西。除了我的键盘和椅子之间的问题之外,它是什么?文件句柄冲突?范围问题?

每个变量都已声明并具有我希望它具有的值。

编辑(不再适用)。在这三个文件上使用 IO::File 没有任何改变。


编辑 2:新的完整子程序代码

我的代码有效(除非我的引用已经存在,但我认为那是因为“追加”模式),但可能存在一些错误和unperlish编码方式(抱歉,Monks)。但是,我使用 Strict 和警告!

sub _ref_edit($) {
my $manda_def = "$dir/manda_def.list";
my $newrefhandle;
my $ref = $_[0];
(my $refout = $ref) =~ s/empty//;
my $refhandle;
my $parname = '';
my $parvalue = '';
my @val;

_printMan;

my $flush = readline STDIN; # Wait for <enter>

# If one or both of the ref. and the default values are missing
if ( !( -e $manda_def && -e $ref ) ) {
die "Cannot find $ref and/or $manda_def";
}

# Open needed files (ref & default)
open( $refhandle, "<", $ref ) or die "Cannot open ref $ref : $!";
open( $newrefhandle, ">>", $refout )
or die "Cannot open new ref $refout : $!";

# Read each line
while ( my $refline = <$refhandle> ) {
# If line read not an editable macro
if ( $refline =~ /^define\({{(.+)}},\s+{{.*__VALUE__.*}}\)/ ){
$parname = $1; # $1 = parameter name captured in regexp
# Prompt user
$parvalue = _ref_param_handling( $parname, $manda_def );
# Substitution in ref
$refline =~ s/__VALUE__/$parvalue/;
# Param not specified and no default value
$parvalue eq '' ? $refline=~s/__COM__/#/ : $refline=~s/__COM__//;
}

print $newrefhandle $refline;
}
close $newrefhandle;
close $refhandle;

return $refout;
} # End ref edit

_ref_param_handle 子例程仍然是:

open( $mde, '<', $_[1] )
or die "Cannot open mandatory/default list $_[1] : $!";

# Read default/mandatory file list
while (<$mde>) {
( $name, $manda, $default, $match, $descript ) = split( /\s+/, $_, 5 );
next if ( $name !~ $ref_param ); # If param read differs from parname

(SOME IF/ELSE)

} # End while <MDE>
close $mde;
return $input;
}

从 manda_def 文件中提取:

NAME  Mandatory? Default Match      Comm.
PORT y NULL ^\d+$ Database port
PROJECT y NULL \w{1,5} Project name
SERVER y NULL \w+ Server name
modemRouting n NULL .+
modlib y bin .+
modules y sms .+

从 ref_file 中提取:

define({{PORT}},         {{__VALUE__}})dnl
define({{PROJECT}}, {{__VALUE__}})dnl
define({{SERVER}}, {{__VALUE__}})dnl
define({{modemRouting}}, {{__COM__{{$0}} '__VALUE__'}})dnl
define({{modlib}}, {{__COM__{{$0}} '__VALUE__'}})dnl
define({{modules}}, {{__COM__{{$0}} '__VALUE__'}})dnl

感谢任何帮助。

最佳答案

不清楚什么是初始化 $refhandle$newrefhandle$mde。根据它们所具有的值将影响打开的行为 - 即它是否会在打开新文件句柄之前关闭任何文件句柄。

我建议您开始使用IO::File 接口(interface)来打开/写入文件,因为这使得文件句柄管理工作变得更加容易,并且会避免任何无意中的关闭。像...

use IO::File;

my $refhandle = IO::File->new("< $ref") or die "open() - $!";

$refhandle->print(...);

就就地编辑文件而言,这是我用来实现此目的的常用模式,确保 perl 的 -i 行为。

sub edit_file
{
my ($filename) = @_;

# you can re-create the one-liner above by localizing @ARGV as the list of
# files the <> will process, and localizing $^I as the name of the backup file.
local (@ARGV) = ($filename);
local($^I) = '.bak';

while (<>)
{
s/original string/new string/g;
}
continue
{
print;
}
}

关于perl - 我可以同时读取和写入多个文件句柄 (Perl) 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1198687/

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