Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: readline on closed filehandle

by stevieb (Canon)
on Apr 14, 2016 at 19:22 UTC ( [id://1160453]=note: print w/replies, xml ) Need Help??


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);

Replies are listed 'Best First'.
Re^2: readline on closed filehandle
by wrkrbeee (Scribe) on Apr 14, 2016 at 19:32 UTC
    Yes, that's much better. Thank you for your time!!
Re^2: readline on closed filehandle
by wrkrbeee (Scribe) on Apr 14, 2016 at 22:32 UTC

    Need to show my stupidity here, gotta ask ... the code below is the lower half of the cleanly aligned code you offered earlier (thanks again there!). Pgm runs, but no output. So, I began tinkering with my REGEX to no avail. Now I'm looking at the code and here's what my feeble mind sees .... first, we set $/ (the delimiter) to </DOCUMENT>. Thus, we should process data until we discover this delimiter, at which point we write the data to disk, then begin processing anything after </DOCUMENT> up until the next occurrence of </DOCUMENT>. Is that right? If so, then as a diagnostic tool, should I be able to print (to the screen) the contents of the data between the delimiters? If so, which variable will contain this data? Sorry for the neophyte-like questions. Thank you. UPDATE: Withdrawn, problem solved. Sorry!

    open my $FH_IN, '<',$files_dir.'/'.$filename or die "Can't open $filen +ame <:$!>"; use Cwd; print getcwd(), "\n"; $/="</DOCUMENT>"; while (my $doc = <$FH_IN>) { if ($doc =~ m/^\s*<FILENAME>\s*(.*)(ex21)\s*(.*)(<\/TEXT)\s*(.* +)/igm || $doc =~ m/^\s*<FILENAME>\s*(.*)(EX\-21)\s*(.*)(<\/TEXT)\s*( +.*)/igm ) { $htm=join('',$1); print $htm; print $FH_OUT "$htm\n"; } } close $FH_IN; } close($FH_OUT);

      It would be appreciated if you would share what you did to correct your mistake/misunderstanding, so that future readers will learn as well.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1160453]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (1)
As of 2024-04-24 16:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found