Thanks Alexander for the cool examples, I'm a big LOTR fan BTW.

However I'm still getting the following error:

print() on closed filehandle FH at ./get-filldb.pl.iem2 line 42. print() on closed filehandle FH at ./get-filldb.pl.iem2 line 44. print() on closed filehandle FH at ./get-filldb.pl.iem2 line 46. print() on closed filehandle FH at ./get-filldb.pl.iem2 line 48. print() on closed filehandle FH at ./get-filldb.pl.iem2 line 50. print() on closed filehandle FH at ./get-filldb.pl.iem2 line 52. print() on closed filehandle FH at ./get-filldb.pl.iem2 line 54. print() on closed filehandle FH at ./get-filldb.pl.iem2 line 56.

Basically what I'm trying to do is write to a text file close the file handler then open the file for reading but even If I try to open another text file for reading I get the same error. If I'm closing the file handle after writing shouldn't I be able to open a text file and print the contents? I can open the text file for reading but when I try to print is when I get the error Please see code below

use strict; use warnings; use HTML::TreeBuilder 3; open(FH, '>', $filename) or die "cannot open file"; select 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"; } close FH; my $fn='lotr.txt'; open my $fh,'>',$fn or die "Could not open $fn: $!"; print "Three Rings for the Elven-kings under the sky,\n"; print $fh "The Road goes ever on and on\n"; print "Seven for the Dwarf-lords in their halls of stone,\n"; print $fh "Down from the door where it began.\n"; print "Nine for Mortal Men doomed to die,\n"; print $fh "Now far ahead the Road has gone,\n"; print "One for the Dark Lord on his dark throne\n"; print $fh "And I must follow, if I can,\n"; print "In the Land of Mordor where the Shadows lie.\n"; print $fh "Pursuing it with eager feet,\n"; print "One Ring to rule them all, One Ring to find them,\n"; print $fh "Until it joins some larger way\n"; print "One Ring to bring them all and in the darkness bind them\n"; print $fh "Where many paths and errands meet.\n"; print "In the Land of Mordor where the Shadows lie.\n"; print $fh "And whither then? I cannot say.\n"; close $fh;

Thanks


In reply to Re^4: print() on closed filehandle WRITE_FH by TonyNY
in thread print() on closed filehandle WRITE_FH by TonyNY

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.