Here's how I would write your script (in my admittedly idiosyncratic, but hopefully sufficiently Perl-ish, style). You probably won't like all of it, or even most of it; take what you like.

(Thoroughly untested.)

#!/usr/bin/perl use strict; use warnings; # use diagnostics; use File::Find; use Date::Parse; find \&process, 'tmp'; exit 0; { my $output_dir; BEGIN { $output_dir = '/home/hynek/old-blog/new'; } sub process { if ( -f $_ && /\.html$/ ) { my ( $time, $text ) = parse( $File::Find::name ); ( my $output_file = "$output_dir/$_" ) =~ s/html$/txt/; print_out( $text, $output_file ); utime $time, $time, $output_file; } } } sub parse { my $input_file = shift; open my $in, "<:utf8", $input_file or die "Can't read $input_file: $ +!"; my ( $time, $subject, $text ); while ( <$in> ) { $time = Date::Parse::str2time( $1 ) and next if !$time && /\w+, (\w+ \d\d, \d\d\d\d)/; chomp; # is this *really* necessary for all lines? my $match_no; if ( $match_no = ( m%<h3 class="post-title">% ... m%</h3>% ) ) { next if $match_no == 1; $subject = $_ and next if /\w/; $text = $subject and next if $match_no =~ y/E//; } if ( $match_no = ( m%<div class="post-body">% ... m%</div>% ) ) { next if $match_no == 1; last if $match_no =~ y/E//; $text .= "$_\n"; # why chomp earlier? are you getting # rid of DOS eol sequences? } } close $in or die "Failed to close $input_file: $!"; $text =~ s,(\r|</?p>|^\s*),,gm; return ( $time, $text ); } sub print_out { my ( $text, $filename ) = @_; open my $out, ">:encoding(iso-8859-15)", $filename or die "Cannot write to $filename: $!"; print $out $text; close $out; } __END__

TIMTOWTDI-ly,

the lowliest monk


In reply to Re: Hints for getting this perly... by tlm
in thread Hints for getting this perly... by hynek

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.