z.B.:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#! /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 $parser = HTML::Parser->new(
start_h => [ \&_starttag, 'self, tagname, attr' ],
end_h => [ \&_endtag, 'self, tagname' ],
text_h => [ \&_text, 'self, dtext' ]
);
$parser->parse($pa);
sub _starttag {
my ($self, $tag, $attr) = @_;
$self->{'_body'} = 1 if ($tag eq 'body');
}
sub _endtag {
my ($self, $tag) = @_;
$self->{'_body'} = undef if ($tag eq 'body');
}
sub _text {
my ($self, $dtext) = @_;
$dtext =~ s/\A\s+//;
$dtext =~ s/\s+\z//;
return() unless ( length($dtext) > 0 and $dtext =~ /[^\s]/ );
if ( defined($self->{'_body'}) ) {
print $dtext," ";
}
}