title: Get results to write to CSV using Perl
body: <p>The following Perl script cureently reads in an html file and strips off what I don't need. It also opens up a csv document which is blank. </p>
  
  <p>My problem being is I want to import the stripped down results into the CSV's 3 fields using Name as field 1, Lives in as field 2 and commented as field 3. </p>
  
  <p>The results are getting displayed in the cmd prompt but not in the CSV.</p>
  
  <pre><code>use warnings; 
  use strict;  
  use DBI;
  use HTML::TreeBuilder;  
  use Text::CSV;
  
  open (FILE, 'file.htm'); 
  open (F1, "&gt;file.csv") || die "couldn't open the file!";
  
  
  my $csv = Text::CSV-&gt;new ({ binary =&gt; 1, empty_is_undef =&gt; 1 }) 
      or die "Cannot use CSV: ".Text::CSV-&gt;error_diag (); 
  
  open my $fh, "&lt;", 'file.csv' or die "ERROR: $!"; 
  $csv-&gt;column_names('field1', 'field2', 'field3'); 
  while ( my $l = $csv-&gt;getline_hr($fh)) { 
      next if ($l-&gt;{'field1'} =~ /xxx/); 
      printf "Field1: %s Field2: %s Field3: %s\n", 
             $l-&gt;{'field1'}, $l-&gt;{'field2'}, $1-&gt;{'field3'} 
  } 
  close $fh; 
  
  my $tree = HTML::TreeBuilder-&gt;new_from_content( do { local $/; &lt;FILE&gt; } ); 
  
  for ( $tree-&gt;look_down( 'class' =&gt; 'postbody' ) ) {
      my $location = $_-&gt;look_down
      ( 'class' =&gt; 'posthilit' )-&gt;as_trimmed_text;     
  
      my $comment  = $_-&gt;look_down( 'class' =&gt; 'content' )-&gt;as_trimmed_text;
      my $name     = $_-&gt;look_down( '_tag'  =&gt; 'h3' )-&gt;as_text;     
  
      $name =~ s/^Re:\s*//;
      $name =~ s/\s*$location\s*$//;      
  
      print "Name: $name\nLives in: $location\nCommented: $comment\n";
  } 
  </code></pre>
  
  <p>An example of the html is:</p>
  
  <pre class="lang-html prettyprint-override"><code>&lt;div class="postbody"&gt;
      &lt;h3&gt;&lt;a href "foo"&gt;Re: John Smith &lt;span class="posthilit"&gt;England&lt;/span&gt;&lt;/a&gt;&lt;/h3&gt;
      &lt;div class="content"&gt;Is C# better than Visula Basic?&lt;/div&gt;
  &lt;/div&gt;
  </code></pre>