Monks whose opinions I respect have suggested a couple things to improve the quality of my scripts:
- use hash values instead of buckets o' scalar vars
- simplify subroutines by passing parameters properly
This ditty is a result of re-reading through merlyn's Llama and danger's EoPwP toward that end. Have I got the right idea, or am I fullabeans?
P.S. Please ignore the fact that the following code is nothing more than a wrapper around 3 system calls. Is mostly an exercise in learning a bit more Perl. Using CPAN modules HTML::FormatPS and HTML::FormatText is on the Todos list {g}
#!/usr/bin/perl -w
# hashnparam.pl
# pod at tail
use strict;
use Cwd 'chdir';
my $dir = '~/';
my %exec = ();
$exec{html2ps} = '/usr/bin/html2ps';
$exec{ps2pdf} = '/usr/bin/ps2pdf';
$exec{html2text} = '~/perl/html2text';
my %file = ();
$file{rc} = '~/.html2psrc';
$file{html} = "$dir/original.html";
$file{tmp} = 'generated.tmp';
$file{ps} = 'generated.ps';
$file{pdf} = 'generated.pdf';
$file{txt} = 'generated.txt';
my @exists = @file{'rc', 'html'};
my @unlink = @file{'tmp', 'ps', 'pdf', 'txt'};
my @outfiles = @file{'ps', 'pdf', 'txt'};
print ("\nStarting $0 run.\n\n");
chdir "$dir" or die "Error chdir to $dir: $!";
foreach my $exec (values(%exec)) { &FILECHECK($exec, 'die'); }
&FILECHECK($file{html}, 'die');
&FILECHECK($file{rc}, 'warn');
print "Ready to unlink prior ps, pdf, and txt files.\n";
print "<enter> to continue, ctrl+c to abort.";
<STDIN>;
foreach my $unlink(@unlink) {
if (-e $unlink && -w _) {
unlink $unlink or die "Error unlinking $unlink: $!";
}
}
system ("$exec{html2ps} -F $file{html} > $file{ps}")
and die "Error running $exec{html2ps}: $!";
system ("$exec{ps2pdf} $file{ps} $file{pdf}")
and die "Error running $exec{ps2pdf}: $!";
system ("$exec{html2text} < $file{html} > $file{tmp}")
and die "Error running $exec{html2text}: $!";
open (TMP, "< $file{tmp}") or die "Error opening $file{tmp} RO: $!";
open (TEXT, "> $file{txt}") or die "Error opening $file{txt} WO: $!";
while (<TMP>) {
s/ / /g;
s/ / /g;
print TEXT;
}
close TMP or die "Error closing $file{tmp}: $!";
close TEXT or die "Error closing $file{txt}: $!";
unlink $file{tmp} or die "Error unlinking $file{tmp}: $!";
print "\nCompleted $0 run.\n";
print " Original HTML file at:\n";
print " $file{html}\n";
print " Generated files at:\n";
foreach my $outfile (@outfiles) {
print " $dir/$outfile\n";
}
print ("\n");
###############################################
sub FILECHECK {
my $file = $_[0];
my $ooot = $_[1];
unless (-e $file) {
if ($ooot eq 'die') {
die "\nError: $file not found where specified.\n\n";
}
elsif ($ooot eq 'warn') {
warn "\nCaution: $file not found where specified.\n";
}
}
}
###############################################
=head1 Name
hashnparam.pl
=head1 Description
Generates PostScript+PDF+text files from existing HTML.
Insert <!--NewPage--> as needed in original.html then run this script
+.
html2ps does not handle frames, tables, or images in original HTML.
=head1 Changelog
2001-04-07 07:20 Hashified variables
Sub'd+parameterized filecheck
2001-04-05 Initial working Perl code
2001-04-03 Simple Bash script
=head1 Todos
Replace system call to html2ps with "use HTML::FormatPS".
Replace system call to html2text with "use HTML::FormatText".
(HTML::Tree and Font::AFM needed for above)
Find perlish replacement for system call to ps2pdf.
=head1 Author
ybiC
=head1 Required
html2ps 1.0b1-8
gs-aladdin 5.50-8
ps2pdf
html2text
=head1 Tested
On Debian 2.2, WinNT4, and Win2kPro
Mozilla M18, 0.8 Netscape 4.75
Opera 5.02 IE 5.5
w3m 0.1.9-5 Lynx 2.8.3-1
gv 3.5.8-17 gsview32 2.6
Acrobat Reader 4.056
=head1 html2ps runcontrol
/* ~/.html2psrc */
@html2ps {
package {
ImageMagick: 1; PerlMagick: 1;
TeX: 1; Ghostscript: 1;
check: weblint; libwww-perl: 1;
path: "/usr/X11R6/bin:/usr/bin";
}
paper { type: letter; }
hyphenation {
en {
file: "/usr/share/texmf/tex/generic/hyphen/ushyph1.tex";
}
}
margin {
top: 1; right: 2.5;
bottom: 1.5; left: 2.5;
}
footer {
alternate: 0;
left: \$D;
right: "page $N";
}
}
/* Standard style sheet definitions */
BODY { font-family: Helvetica; font-size: 11pt; }
P { text-align: justify; }
H1 { font-size: 19pt; text-align: center; font-weight: bold; }
H2 { font-size: 14pt }
H3 { font-size: 13pt }
H4 { font-size: 12pt }
H5 { font-size: 11pt; text-align: center; }
H6 { font-size: 11pt; text-align: center; }
=cut