Technically, I'm not sure that the '~literal' pseudo-element actually has any effect on how things are printed out (though that's how it's explained in the docs). It seems more sensical to me that it controls whether the text inserted is escaped or not. *shrug* But that's just implementation details. :)

Honestly, I don't really think there's a way to get HTML::TreeBuilder to print out the original HTML exactly the same. You can get pretty close by calling $tree->no_space_compacting(1) before parsing the HTML, but it doesn't seem to be 100% the same.

If all you want to do is make some minor changes (eg, changing '--' to '—') to the text parts, keeping the rest of the HTML exactly the same, you're probably best off switching to HTML::Parser (which HTML::TreeBuilder is based on). Code follows...

#!/usr/bin/perl use warnings; use strict; our $html = <<'EOHTML'; <html> <body> <p>The relationship can be expressed by the following equation:<br> <blockquote> y &lt;= (7x<sup>3</sup> + 3x<sup>2</sup>)/((x - 3)(x - 5) </blockquote> <p>Of course x != 3 &amp; x != 5 -- That goes without saying. <p>:) </body> </html> EOHTML use HTML::Parser; my $parser = HTML::Parser->new( handlers => { default => [sub { print @_ }, 'text'], # print out HTML tags unmod +ified text => [\&process_text, 'text'], }, ); $parser->parse($html); sub process_text { $_ = shift; # Make any changes to the text here... s/--/&mdash;/g; s/:\)/<img src="smiley.gif">/g; # and so forth. # Now print out the modified text. print; }

Output:

<html> <body> <p>The relationship can be expressed by the following equation:<br> <blockquote> y &lt;= (7x<sup>3</sup> + 3x<sup>2</sup>)/((x - 3)(x - 5) </blockquote> <p>Of course x != 3 &amp; x != 5 &mdash; That goes without saying. <p><img src="smiley.gif"> </body> </html>

bbfu
Black flowers blossum
Fearless on my breath


In reply to Re3: processing text nodes using HTML::Element by bbfu
in thread processing text nodes using HTML::Element by Anonymous Monk

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.