http://qs1969.pair.com?node_id=493480

Well this is pretty nifty.

I am building a workflow application at work, and the heart of the matter is dealt with by a Perl script on the web. The documentation sits in a webified Lotus database on another machine. I have to include the documentation from the Lotus front page in my script.

It was obvious that HTML::Parser was the answer, but it took a bit of fiddling about to get it right. The thing to remember is that a decent module should always include an eg/ directory to help you out. By mulling over the contents of the examples included with H::P I was able to come up with the following code.

The idea is to look at the tags as they go by, and keep an eye out when we enter <script> or <body> tags. While I was writing this, I found it helpful to use the DEBUG setting, to change the opening less-than character of a tag to an left square bracket, which made it easier to see what was happening.

I'm not a heavy user of H::P; this is perhaps the most complex thing I've ever done with it, so I'm quite pleased the way it turned out. See if you can spot the pun in the code.

use strict; use LWP::Simple; use HTML::Parser; use constant DEBUG => 0; my $URI_HOST = 'http://www.example.com'; my $URI_PATH = '/sample/database.nsf'; my $content = get( "$URI_HOST$URI_PATH" ); if( not $content ) { $content = q{<p><small>The documentation is unavailable</small></p +>}; } else { my $new = ''; my %in_tag; my $tag_watch = sub { my $tag = shift; my $in = shift; my $attr = shift; $in_tag{$tag} += $in; return unless $in_tag{script} or $in_tag{body} or grep { $tag eq $_ } qw( script body ) ; my $out = (DEBUG ? '[' : '<') . ($in < 1 ? '/' : '') . $tag; if( $attr ) { my @attr_pair; while( my( $key, $value ) = each %$attr ) { # renormalise things that look like URIs $value =~ s{^($URI_PATH/)}{$URI_HOST$1}; push @attr_pair, qq{$key="$value"}; } $out .= join( ' ', ('', @attr_pair)); } $out .= '>'; $new .= $out; }; my $p = HTML::Parser->new( api_version => 3, start_h => [$tag_watch, "tagname, '+1', attr"], end_h => [$tag_watch, "tagname, '-1'"], text_h => [sub { $new .= $_[0] if $in_tag{script} or $in_ta +g{body} }, "dtext"], comment_h => [sub { $new .= $_[0] if $in_tag{script} }, + "dtext"], ); $p->parse($content); $p->eof(); $content = $new; } # now include $content in a templating/include output mechanism of you +r choice

• another intruder with the mooring in the heart of the Perl