in reply to print() on closed filehandle WRITE_FH

select changes the default output for print statements, and you've got some print statements after you close WRITE_FH that are then still trying to write to the file. You need to save the original output handle by first saying my $orig_fh = select(WRITE_FH); and then restore it later by saying select($orig_fh); right before or after you close the file.

Replies are listed 'Best First'.
Re^2: print() on closed filehandle WRITE_FH
by TonyNY (Beadle) on Sep 25, 2018 at 19:02 UTC

    Hi haukex,

    I'm till getting the same error. Probably because I've added your suggestions in the wrong place.

    use strict; use HTML::TreeBuilder 3; open(WRITE_FH, '>', $filename) or die "cannot open $filename"; select WRITE_FH; # ... # ... everything you print should be redirected to your file # ... my $root = HTML::TreeBuilder->new; $root->parse_file('file2.html'); my @div_class = $root->find_by_attribute("class","forminput"); foreach my $node (@div_class) { # print $node->content_list(); # print "\n"; } my $orig_fh = select(WRITE_FH); select($orig_fh); close WRITE_FH; #prnt elements in array: open( my $fh, '<', $filename ) or die "Can't open $filename: $!"; my @line = <$fh>; print "\n"; print "FillDB File Size Limit: $line[0]"; print "FillDB File Count Limit: $line[1]"; print "Timeout for queries in queue: $line[2]"; print "Size of queries in queue: $line[3]"; print "Size of results queue: $line[4]"; print "\n"; close $fh;
      Probably because I've added your suggestions in the wrong place.

      Yes, and pryrt already explained the issue. But then you posted here, so it seems like you didn't get it working?

      If you take a look at the documentation I linked to, and the replies you've gotten, you'll see that:

      • print "..."; prints to the currently selected filehandle, which is STDOUT by default
      • print $filehandle "..."; and print {$filehandle} "..."; print to that specific filehandle
      • select both changes the currently selected filehandle, and returns the previously selected filehandle

      If you go through your program line-by-line, the above information should be enough for you to see what is going wrong and how to fix it. If you have questions about the above, please always feel free to ask.

      I understand the appeal of being able to copy and paste code and just have it work. However, I find it so much more satisfying to figure it out myself, to understand the flow of the code and what is going on. And of course, having it figured out means I'll know more about what I'm doing next time. In fact, I intentionally didn't post a block of code in an attempt to encourage you to look at the docs and figure it out :-)

        Hi haukex, I was ale to get it to work by using OUT:

        #Parse html file using TreeBuilder Module use strict; use warnings; use HTML::TreeBuilder 3; #Open text file to redirect treebuilder output to file open OUT, '>', $treefile or die "cannot open $treefile for writing ; $!"; my $root = HTML::TreeBuilder->new; $root->parse_file($htmlfile) or die "could not open $htmlfile for pars +ing\n"; my @div_class = $root->find_by_attribute("class","forminput"); foreach my $node (@div_class) { print OUT $node->content_list(); print OUT "\n"; } close OUT;

        As far as just copying and pasting sometimes I an up against time constraints so I don't always have the time to fully understand the code and end up going back later to look at the code more closely which I will do with what you have provided for me here

      You have to do the my $orig_fh = select(WRITE_FH); the first time you select WRITE_FH -- otherwise, you've already lost the original file handle. Then, just before or just after the close WRITE_FH, you need to select the original again using select($orig_fh), so that it's active after you've closed WRITE_FH.