I found some code that does just that in a script I use. It uses the XML::Twig interface, for I prefer that to HTML::Tree. Here's the subroutine, plus some code to show how it should be invoked. Of course, you may need to also teach this about some elements other than br.

use warnings; use strict; use 5.010; use XML::Twig; sub html_text { my($el) = @_; my $r = ""; for my $n ($el->descendants_or_self) { if ($n->is_text) { $r .= $n->trimmed_text; } elsif ("br" eq $n->gi) { $r .= "\n"; } } $r; } my $tw = XML::Twig->new; $tw->parse_html(q(<p>The Cow Jumped<br>Over the Moon</p>)); say html_text($tw->root);

In reply to Re: HTML::Element and "<br>" tags by ambrus
in thread HTML::Element and "<br>" tags by trashtalker

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.