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;

In reply to readline on closed filehandle by wrkrbeee

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.