Hi, I'm currently trying to sharpen my perl-skills and do nearly everything with a perl-script. Currently, I decided to move my blog from blogger.com to a own blosxom-installation. To be not forced to do massive copy'n'paste I decided to write a perl-script. I also succeeded, the job is done but the script looks somehow unperlish to me. :( So I'd like some suggestion how to do it better.

As it's a one-use script, some assumptions are made: The old .html-files from blogger.com are in /home/hynek/old-blog/tmp and the new files are put into /home/hynek/old-blog/new. The date has always the same form and appears only once and the body and the subject are also easy to distinguish so it doesn't pay off to use a full-blown html-parser, however it leads to a rather ugly loop. An example of such a page is at here. Blosxom-files are simple .txt files with html in it and with the first line being the subject.

So here's the code:

#!/usr/bin/perl use strict; use warnings; use diagnostics; use File::Find; use Date::Format; use Date::Parse; sub process { if (-f $_ && /\.html$/) { my $fn = $_; open(F, "<:utf8", $fn) or die("Cannot open ${File::Find::name}: $! +"); my @file = <F>; chomp(@file); close F; (my $date) = grep m%\w+, \w+ \d\d, \d\d\d\d%, @file; $date =~ m%\w+, (\w+ \d\d, \d\d\d\d)%; my $time = str2time($1); my ($in_text, $in_subject) = undef; my ($text, $subject) = ""; READ: foreach (@file) { if (m%<h3 class="post-title">%) { # Subject $in_subject = 1; } elsif ($in_subject && $_ !~ m%</h3>%) { $subject = $_ if /\w/; } elsif ($in_subject && $_ =~ m%</h3>%) { $in_subject = undef; $text = "$subject\n"; } elsif (m%<div class="post-body">%) { # Text $in_text = 1; } elsif ($in_text && $_ !~ m%</div>%) { $text .= $_ . "\n"; } elsif ($in_text && $_ =~ m%</div>%) { last READ; } } $text =~ s/(\r|<p>|<\/p>|^\s*)//gm; $fn =~ s/html$/txt/; open(OUT, ">:encoding(iso-8859-15)", "/home/hynek/old-blog/new/$fn +") or die "Cannot open for write: $!."; print OUT $text; close(OUT); utime $time, $time, ("/home/hynek/old-blog/new/$fn"); } } find(\&process, ("tmp"));
What I especially dislike is the loop and the reading of the date consisting of two regexps.

Thanks in advance.

UPDATE: Removed the copy'n'paste error from utime.


In reply to 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.