After parsing an HTML file with HTML::TreeBuilder one gets back a large nested data structure:

my $tree = HTML::TreeBuilder->new_from_file($filename);

In the interest of efficiency, I would like to develop Perl modules which have the HTML parsed and already in-memory at mod_perl startup time:

package html::page::hello_world;


my $tree = HTML::TreeBuilder->new_from_file('/html/hello_world.html');

sub new {
    $tree
}

1;

This way, the module is used at server startup time and the constructor call incurs no delay due to parsing the HTML file. However, there is one problem: once the returned tree is modified, the new would return that same modified tree instead of a tree representing a fresh parse of the HTML file.

I therefore want to clone the tree and return a clone:

package html::page::hello_world;


my $tree  = HTML::TreeBuilder->new_from_file('/html/hello_world.html');
my $clone = $tree->clone;

sub new {
    my $retval = $clone;
    $clone = $tree->clone;
    $retval;
}

1;

But I don't want the overhead of making the new clone in the same process. I want to do something like a fork and return the pre-made clone to the caller immediately so it doesn't have to wait and manufacture a new clone in a separate thread/process.

Could anyone recommend a strategy/module for doing this?


In reply to fast return of HTML::Tree object clones (via threads/forks)? by metaperl

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.