This should work:
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 ...

In reply to Re: processing multiple files by btrott
in thread processing multiple files by suzka

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.