print()在关闭的文件句柄上

时间:2014-09-18 14:55:36

标签: perl printing filehandle

我用来将输出导出到下面代码末尾的文件$ fh3的打印命令从Perl返回以下错误:

"print() on closed filehandle $fh3 at Stopwords.pl line 46."

这里有两个奇怪的地方:首先,完全相同的代码在我拥有的单独程序中工作得很好。其次,第46行对应于紧接在该行之后的行“}”:

"print $fh3 "$_ \t $sequences{$_}\n ";"

有人能辨别出这里可能存在的问题吗?

#!/usr/bin/perl
use strict;
use warnings;
use Lingua::EN::StopWords qw(%StopWords);
use Lingua::Stem qw(stem);
use Mojo::DOM;

my $path = "U:/Perl/risk disclosures/2006-28";
chdir($path) or die "Cant chdir to $path $!";

# This program counts the total number of unique sentences in a 10-K and enumerates the frequency    of each one.

my @sequence;
my %sequences;
my $fh;

# Opening each file and reading its contents.

for my $file (<*.htm>) {
    my $data = do {
        open my $fh, '<', $file or die "open failed: $!";
        local $/;    # Slurp mode
        <$fh>;
    };
    my $dom = Mojo::DOM->new($data);
    my @words = split ' ', $dom->all_text();
    # Here eliminating stop words.
    foreach my $word (@words) {
        next if defined $StopWords{$word};
        # Here retaining only the word stem.
        my $stemmed_word = stem $word ;
        ++$sequences{$stemmed_word};
    }
}
my @keys = sort { "\L$sequences{$a}" <=> "\L$sequences{$b}" or "\L$a" cmp "\L$b" } keys %sequences;

open( my $fh3, '>', 'report5.txt' );
foreach (@keys) {
    print $fh3 "$_ \t $sequences{$_}\n ";
}
close $fh3;

0 个答案:

没有答案