Hi!

I want to "split" a HTML-text via HTML::Parser.

I have the HTML-text
<p>This is a bad try to display text then code <pre>#! usr/bin/perl use strict; use warnings; print "Hello World!";</pre> and then plain text again</p>

And I want to get an array which has three elements:
array_0_: This is a bad try to display text then code
array_1_: #! usr/bin/perl
   use strict;
   use warnings;

   print "Hello World!";
array_2_: and then plain text again

I've tried to solve this problem this way:
#! /usr/bin/perl use strict; use warnings; use Data::Dumper; use HTML::Parser; my $pa = qq~ <p>This is a bad try to display text then code <pre>#! usr/bin/perl use strict; use warnings; print "Hello World!";</pre> and then plain text again</p>~; my $p = HTML::Parser->new(); $p->handler(start => \&start_handler,"tagname,self"); $p->parse($pa); sub start_handler{ my ($tag,$self) = @_; my $text = ''; if($tag eq 'pre'){ print "Pre:\n"; } $self->handler(text => sub {$text .= shift},"dtext"); $self->handler(end => sub {my $tag = shift; print $text,"\n\n" if($ +tag eq 'pre' || $tag eq 'p');},"tagname"); }

But that fails. I just get the code, then the code again with "and then plain text again"...

In reply to split html via HTML::Parser by reneeb

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.