1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6use HTML::Parser ();
7
8my $doc = <<'EOT';
9
10<!-- This is not where <BODY> starts -->
11<title>foo</title>
12
13<script language="Perl" description="Print out <BODY>">
14   open(BODY, "body.txt");
15   while (<BODY>) {
16     print;
17   }
18</script>
19
20<!-- The next thing will be <BODY> the body -->
21
22<Body>
23
24Howdy!
25
26</body>
27
28EOT
29
30
31my $body_offset;
32HTML::Parser->new(
33    start_h => [
34        sub {
35            return unless shift eq "body";
36            $body_offset = shift;
37            shift->eof;    # tell the parser to stop
38        },
39        "tagname,offset,self"
40    ]
41)->parse($doc);
42
43die "No <body> found" unless defined $body_offset;
44
45my $head = substr($doc, 0, $body_offset, "");
46print $doc;
47