Udpate: original issue solved, now chatting about integrating modules into perl scripts to allow users to run the script without installing modules separately.
Dear fellow monks, this should be a quick one.
I'm trying to use an HTML stripper and character reference converter written by none other than Tom Christiansen, and the HTML stripper portion seems to work fine, but HTML character references are not being converted.
I'm afraid I know too little about hashes and chr to find the bug myself. This thing was written in 1996 so perhaps the problem is caused by some changes that have been made in perl itself since then.
(Note: I know modules are better for this purpose, but I need a solution that will work on other people's computers without installing a module.)
Here's the entire original script.
And here's the bit that doesn't work for me (it looks for an input file named file.html and creates file.txt - I removed all character references but one to keep the snippet short. Please test on files that contain numerical character references and Á.
#!/usr/bin/perl
use strict;
use warnings;
open (IN, "<:encoding(UTF-8)", "file.html") or die "Can't open file: $
+!";
open (OUT, ">:encoding(UTF-8)", "file.txt") or die "Can't open file: $
+!";
while (<IN>) {
my %entity;
my $chr;
#########################################################
# translate HTML 2.0 entities
#########################################################
s{ (
& # an entity starts with a semicolon
(
\x23\d+ # and is either a pound (#) and numbers
| # or else
\w+ # has alphanumunders up to a semi
)
;? # a semi terminates AS DOES ANYTHING ELSE (
+XXX)
)
} {
$entity{$2} # if it's a known entity use that
|| # but otherwise
$1 # leave what we'd found; NO WARNINGS (XXX)
}gex; # execute replacement -- that's code not a
+string
#########################################################
# but wait! load up the %entity mappings enwrapped in
# a BEGIN that the last might be first, and only execute
# once, since we're in a -p "loop"; awk is kinda nice after all.
#########################################################
BEGIN {
%entity = (
Aacute => chr 193, #capital A, acute accent
);
for $chr ( 0 .. 255 ) {
$entity{ '#' . $chr } = chr $chr;
}
}
print OUT $_;
}
close IN;
close OUT;
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.