use HTML::Parser;
use Symbol;
my $TXT = gensym;
my $parser = HTML::Parser->new(api_version => 3,
text_h => [ sub {
print $TXT @_;
}, "dtext" ]);
for my $html (@ARGV) {
(my $txt = $html) =~ s/\.html$/\.txt/;
open $TXT, ">$txt" or die "Can't open $txt: $!";
$parser->parse_file($html);
close $TXT or die "Can't close $txt: $!";
}
Yes, I know--I changed a bunch of stuff around. But
I find it easier to work w/ the new HTML::Parser API
rather than the old. In the new API you set up handlers
for specific events, rather than subclassing and
adding your own functionality.
So we create a new parser object and register an
event to handle text (just like your text method) by
using "text_h" (text handler). We give it a subref to
run on that event, then a list of arguments to pass
to our subref. "dtext" means text that's been passed
through decode_entities, so we don't have to do
that ourselves anymore.
That subref is a closure referring to the $TXT
handle, which we open and close each time
through the loop. There may be a more elegant
way to do this, but it seemed to work for me.
I wanted to use the -i CL option, but you
can't specify the name of the *new* file, only the
name of the backup file. So we're stuck with
looping through @ARGV and messing about
manually with each of the files; but that's not
so bad. Unless, of course, I've messed it up. :)
Each time through the loop we grab the name of
the file, then change the .html extension to .txt.
Save this script and run it like this:
% ./foo.pl file1.html file2.html ...
|