wrkrbeee has asked for the wisdom of the Perl Monks concerning the following question:
Hi Perl Monks, the code below generates the infamous "readline on closed filehandle" error at line 42, which is while (my $doc = <$FH_IN>). A little digging seems to indicate that a faulty call is the culprit. I added a line to the code (immediately after the OPEN) to detect the current working directory. The input file is not in this directory ... yet I specified the input directory at the beginning of the program. Hence, I fail to understand why Perl is looking in a directory that differs from that specified in the code. Appreciate any insight! Thank you! UPDATE: Think I see the error, I'm closing the file before the while loop has a chance to cycle through the document. Need to close after the cycle is complete, which is at the end of the file. Am I right?
#!/usr/bin/perl -w use strict; use warnings; use File::stat; use lib "c:/strawberry/perl/site/lib"; #This program will extract Exhibit 21 info from 10-K filings; #Specify the directory containing the files that you want to read; my $files_dir = 'E:\research\audit fee models\filings\test'; #Specify the directory containing the results/output; my $write_dir = 'E:\research\audit fee models\filings\filenames\Exhib +it21.txt'; #Open the directory containing the files you plan to read; opendir(my $dir_handle, $files_dir) or die "Can't open directory $!"; #Initialize file counter variable; my $file_count = 0; #Loop for reading each file in the input directory; while (my $filename = readdir($dir_handle)) { next unless -f $files_dir.'/'.$filename; print "Processing $filename\n"; #Initialize the variable names. my $line_count=0; my $access_num=""; my $cik=-99; my $name=""; my $stuff=""; my $line=""; my $htm =""; #Open the input file; open my $FH_IN, '<',$files_dir.'/'.$filename or die "Can't open $fi +lename <:$!>"; use Cwd; print getcwd(), "\n"; #Within the file loop, read each line of the current file; $/="</DOCUMENT>"; while (my $doc = <$FH_IN>) { #Begin new document section; if ($doc =~ m/^\s*<FILENAME>(.*?)(ex21)/igm ) #if ($doc =~ m/^\s*<FILENAME>(.*?)(ex21)(.*?)(.htm$)/igm || # $doc =~ m/^\s*<FILENAME>(.*?)(EX-21)(.*?)(.htm$)/igm || # $doc =~ m/^\s*<FILENAME>(.*?)(ex21)(.*?)(.htm$)/igm || # $doc =~ m/^\s*<FILENAME>(.*?)(EX-21)(.*?)(.htm$)/igm) + { $htm=join('',$1,$2); } print $htm; #close $FH_IN or die "Cannot close $filename: $!"; close $FH_IN; #Open the ouput file; open my $FH_OUT, '>>',$write_dir or die "Can't open file $write_di +r >:$!>"; #Save the output to disk; print $FH_OUT "$htm\n"; #closedir($dir_handle); close($FH_OUT); } #end of document section; } #end of current file;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: readline on closed filehandle
by stevieb (Canon) on Apr 14, 2016 at 19:22 UTC | |
by wrkrbeee (Scribe) on Apr 14, 2016 at 19:32 UTC | |
by wrkrbeee (Scribe) on Apr 14, 2016 at 22:32 UTC | |
by stevieb (Canon) on Apr 16, 2016 at 11:35 UTC | |
|
Re: readline on closed filehandle
by choroba (Cardinal) on Apr 14, 2016 at 19:13 UTC |