#!/usr/bin/perl -w
use strict;
$|++;
use HTML::Parser;
my @feet; # final footnote list
my @feet_index; # indexes into @feet
my @elements; # ensure nested tags match
my $parser = HTML::Parser->new
(
text_h => [\&text_h, "text"],
start_h => [\&start_h, "text, tagname"],
end_h => [\&end_h, "text, tagname"],
);
$parser->xml_mode(1); # keep tags case-sensitive
$parser->parse_file(\*DATA); # prints main part to STDOUT
if (@feet) { # we had footnotes?
print "
\n";
print "$_ | ",
"$feet[$_-1] |
\n" for 1..@feet;
print "
";
}
exit 0; # end of code
sub text_h {
my ($text) = @_;
if (@feet_index) { # are we inside a footnote?
$feet[$feet_index[-1]] .= $text; # append to that
} else {
print $text; # just show it
}
}
sub start_h {
my ($text, $tagname) = @_;
## ensure proper nesting
push @elements, $tagname;
if ($tagname eq "foot") {
push @feet, ""; # the note itself
text_h("".@feet."");
push @feet_index, $#feet; # pointer to note
return;
}
text_h($text); # uninteresting start tag
}
sub end_h {
my ($text, $tagname) = @_;
## ensure proper nesting
die "saw $text outside of element"
unless @elements;
die "saw $text nested inside <$elements[-1]>"
unless $elements[-1] eq $tagname;
pop @elements;
if ($tagname eq "foot") {
pop @feet_index; # no longer accumulating here
return;
}
text_h($text); # uninteresting end tag
}
__END__
Writing a Perl column
Writing a magazine column about Perl is a simplefor some! task.
Just perform the following steps:
- Think of a problem to
solveYou can ask around for help here.
I keep an archive of "todo" ideas, and it really helps..
- Write the code to solve it.The code should be between 50 and 200
lines for optimum column
lengthAbout 10,000 characters..
- Fret over the code for a few hoursOr a few minutes..
- Write the columnI use PODSee perldoc perlpod.
format..
- Show the column to a group of friends
on IRCUsually the #perl channel.
for a quick peer review.
- Turn it inBy email. to the editor.
- Wait a few days for the galleysUsually a
PDFPortable Document Format from
AdobeSee www.adobe.com for downloads...
to come back.
- Grimace over the hacks to your lovely
prosejust kidding, guys! and provide corrections to
the corrections.
- Wait a few monthsOr so it seems, since the deadline
for a April cover is usually the first week of January. for it
to "hit the stands".
- Wave the magazine in front of your friendsOr the cute girl
at the bookstore checkout counter.!