Perl 当我想要打印到哈希键中的句柄时,为什么会出现语法错误?

Perl 当我想要打印到哈希键中的句柄时,为什么会出现语法错误?,perl,syntax-error,filehandle,Perl,Syntax Error,Filehandle,我有一段代码,它将当前目录中文件的所有文件句柄作为值存储在散列中。这些键是文件的名称 my %files_list; #this is a global variable. sub create_hash() { opendir my $dir, "." or die "Cannot open directory: $!"; my @files = readdir $dir; foreach (@files) { if (/.text/) {

我有一段代码,它将当前目录中文件的所有文件句柄作为值存储在散列中。这些键是文件的名称

my %files_list;    #this is a global variable.
sub create_hash() {
    opendir my $dir, "." or die "Cannot open directory: $!";
    my @files = readdir $dir;
    foreach (@files) {
        if (/.text/) {
            open(PLOT, ">>$_") || die("This file will not open!");
            $files_list{$_} = *PLOT;
        }
    }
}
接下来,我在代码中使用print语句,在这里我面临一些编译问题

my $domain = $_;
opendir my $dir, "." or die "Cannot open directory: $!";
my @files = readdir $dir;
foreach (@files) {
    if (/.text/ && /$subnetwork2/) {
        print $files_list{$_} "$domain";    #this is line 72 where there is error.
    }
}
closedir $dir;
编译错误如下:

String found where operator expected at process.pl line 72, near "} "$domain""
        (Missing operator before  "$domain"?)
syntax error at process.pl line 72, near "} "$domain""
有人能帮我理解故障吗?

可能是这样的:

print {$files_list{$_}} "$domain";
也许是这样:

print {$files_list{$_}} "$domain";
第一个问题: 运行
create\u hash
子例程后,所有键中的
%files\u list
都将填充
*PLOT

所有
打印{$files\u列表{$}“$domain”将打印到上次打开的文件中。
解决方案:

-open(PLOT,">>$_") || die("This file will not open!");
-$files_list{$_}=*PLOT;
+open($files_list{$_},">>$_") || die("This file will not open!");
-if(/.text/ && /$subnetwork2/)
-{
-    print $files_list{$_} "$domain";#this is line 72 where there is error.
+if(/.text/ && /$subnetwork2/ && exists $files_list{$_})
+{
+    print {$files_list{$_}} $domain;
第二个问题: 在打印到文件描述符之前,不检查文件描述符是否存在
解决方案:

-open(PLOT,">>$_") || die("This file will not open!");
-$files_list{$_}=*PLOT;
+open($files_list{$_},">>$_") || die("This file will not open!");
-if(/.text/ && /$subnetwork2/)
-{
-    print $files_list{$_} "$domain";#this is line 72 where there is error.
+if(/.text/ && /$subnetwork2/ && exists $files_list{$_})
+{
+    print {$files_list{$_}} $domain;
不要忘记关闭文件句柄…

第一个问题: 运行
create\u hash
子例程后,所有键中的
%files\u list
都将填充
*PLOT

所有
打印{$files\u列表{$}“$domain”将打印到上次打开的文件中。
解决方案:

-open(PLOT,">>$_") || die("This file will not open!");
-$files_list{$_}=*PLOT;
+open($files_list{$_},">>$_") || die("This file will not open!");
-if(/.text/ && /$subnetwork2/)
-{
-    print $files_list{$_} "$domain";#this is line 72 where there is error.
+if(/.text/ && /$subnetwork2/ && exists $files_list{$_})
+{
+    print {$files_list{$_}} $domain;
第二个问题: 在打印到文件描述符之前,不检查文件描述符是否存在
解决方案:

-open(PLOT,">>$_") || die("This file will not open!");
-$files_list{$_}=*PLOT;
+open($files_list{$_},">>$_") || die("This file will not open!");
-if(/.text/ && /$subnetwork2/)
-{
-    print $files_list{$_} "$domain";#this is line 72 where there is error.
+if(/.text/ && /$subnetwork2/ && exists $files_list{$_})
+{
+    print {$files_list{$_}} $domain;
别忘了关闭文件句柄…

也许你应该读一下。最后一段说:

如果您将句柄存储在数组或散列中,或者在任何时候 您使用的任何表达式都比裸字句柄或 普通的、未订阅的标量变量要检索它,您必须 改为使用返回filehandle值的块,在这种情况下 不得省略以下内容:

print { $files[$i] } "stuff\n";
print { $OK ? STDOUT : STDERR } "stuff\n";
也许你应该看看报纸。最后一段说:

如果您将句柄存储在数组或散列中,或者在任何时候 您使用的任何表达式都比裸字句柄或 普通的、未订阅的标量变量要检索它,您必须 改为使用返回filehandle值的块,在这种情况下 不得省略以下内容:

print { $files[$i] } "stuff\n";
print { $OK ? STDOUT : STDERR } "stuff\n";

另外,三参数形式的open是一个好习惯:
open($fd,'>>,$)
你真的应该使用词法文件句柄来处理这类事情。另外三参数形式的open也是一个好习惯:
open($fd,'>>,$)
你真的应该使用词法文件句柄来处理这类事情。