in reply to Redirect HTML::TreeBuilder to file

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.