TonyNY has asked for the wisdom of the Perl Monks concerning the following question:

Hi

I need to redirect the output from HTML::TreeBuilder to a file but I could not find any examples on the net.

use warnings; use strict; use HTML::TreeBuilder; # This is the file we are going to read. #my $file = 'test.html'; # Parse all of the contents of $file. my $parser = HTML::TreeBuilder->new (); $parser->parse_file ($file); # Now display the contents of $parser. recurse ($parser, 0); exit; # This displays the contents of $node and any children it may # have. The variable $depth is the indentation used. sub recurse { my ($node, $depth) = @_; # Print indentation according to the level of recursion. print " " x $depth; # If $node is a reference, then it is an HTML::Element. if (ref $node) { # Print the tag associated with $node, for example "html" or # "li". print $node->tag (), "\n"; # $node->content_list () returns a list of child nodes of # $node, which we store in @children. my @children = $node->content_list (); for my $child_node (@children) { recurse ($child_node, $depth + 1); } } else { # If $node is not a reference, then it is just a piece of text # from the HTML file. print $node, "\n"; } }

Thanks

Replies are listed 'Best First'.
Re: Redirect HTML::TreeBuilder to file
by choroba (Cardinal) on Sep 25, 2018 at 12:50 UTC
    To make print print to a file, just specify the special filehandle argument.
    open my $out, '>', 'output.html' or die $!; print {$out} "This will be printed to the file.\n"; close $out;
    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

      Hi choroba,

      Where would I put this in the code to build the tree?

      tried it here but it did not work:

      else { # If $node is not a reference, then it is just a piece of text # from the HTML file. # print $node, "\n"; open my $out, '>', 'output.html' or die $!; print {$out} "This will be printed to the file.\n"; close $out; } }

        Rather than just copy/paste stuff without understanding take a look at open and print. You've copied an example you were given which writes the string "This will be printed to the file", to the file... you know the line you commented out prints what you want to the screen.

Re: Redirect HTML::TreeBuilder to file
by bliako (Abbot) on Sep 25, 2018 at 13:57 UTC

    I suggest you modify recurse($node, $depth) to accept a 3rd parameter: a scalar to contain the filehandle to print to. For example: recurse($node, $depth, $fh_to_print_to). (Although I find most subs that print to file rather than returning a string of results, bad form. But that's just me.)

    Then modify all print statements in recurse() to utilise provided filehandle, rather that printing to the default: STDOUT.

    Then open the file to write results to, prior to first calling recurse(). And supply it to recurse(). Close the filehandle once the first call to recurse() returns.

    And you're done.