http://www.perlmonks.org?node_id=1160453


in reply to readline on closed filehandle

Proper indentation and alignment is a very handy thing... you close the while loop on top of your close statement for the read file handle, which means it will read the first line (ie. entry), closes the file, then fails to read the next chunk because there's no more valid handle to read from.

Is this more along the lines of what you're attempting to do (untested)?

use strict; use warnings; use File::stat; use lib "c:/strawberry/perl/site/lib"; my $files_dir = 'E:\research\audit fee models\filings\test'; my $write_dir = 'E:\research\audit fee models\filings\filenames\Exhib +it21.txt'; open my $FH_OUT, '>>',$write_dir or die "Can't open file $write_dir >: +$!>"; opendir(my $dir_handle, $files_dir) or die "Can't open directory $!"; my $file_count = 0; while (my $filename = readdir($dir_handle)){ next unless -f $files_dir.'/'.$filename; print "Processing $filename\n"; my $line_count=0; my $access_num=""; my $cik=-99; my $name=""; my $stuff=""; my $line=""; my $htm =""; open my $FH_IN, '<',$files_dir.'/'.$filename or die "Can't open $f +ilename <:$!>"; use Cwd; print getcwd(), "\n"; $/="</DOCUMENT>"; while (my $doc = <$FH_IN>){ if ($doc =~ m/^\s*<FILENAME>(.*?)(ex21)/igm ){ $htm=join('',$1,$2); print $htm; print $FH_OUT "$htm\n"; } } close $FH_IN; } close($FH_OUT);