<?xml version="1.0" encoding="windows-1252"?>
<node id="70715" title="hash n' params" created="2001-04-07 14:57:40" updated="2005-08-04 18:48:47">
<type id="115">
perlquestion</type>
<author id="14909">
ybiC</author>
<data>
<field name="doctext">
Monks whose opinions I respect have suggested a couple things to improve the quality of my scripts:
&lt;ol&gt;
  &lt;li&gt;use hash values instead of buckets o' scalar vars
  &lt;li&gt;simplify subroutines by passing parameters properly
&lt;/ol&gt;

&lt;p&gt;
This ditty is a result of re-reading through [merlyn]'s [isbn://1565922840|Llama] and [danger]'s [isbn://1884777805|EoPwP] toward that end. &amp;nbsp; Have I got the right idea, or am I fullabeans?

&lt;p&gt;
&lt;font size="-1"&gt;&lt;i&gt;P.S. Please ignore the fact that the following code is nothing more than a wrapper around 3 system calls. &amp;nbsp; Is mostly an exercise in learning a bit more Perl. &amp;nbsp; Using CPAN modules [cpan://HTML::FormatPS] and [cpan://HTML::FormatText] is on the Todos list {g}&lt;/i&gt;&lt;/font&gt;


&lt;readmore&gt;


&lt;code&gt;#!/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)) { &amp;FILECHECK($exec, 'die'); }
&amp;FILECHECK($file{html}, 'die');
&amp;FILECHECK($file{rc}, 'warn');

print "Ready to unlink prior ps, pdf, and txt files.\n";
print "&lt;enter&gt; to continue, ctrl+c to abort.";
&lt;STDIN&gt;;
foreach my $unlink(@unlink) {
    if (-e $unlink &amp;&amp; -w _) {
        unlink $unlink or die "Error unlinking $unlink: $!";
        }
    }


system ("$exec{html2ps}  -F $file{html} &gt; $file{ps}")
    and die "Error running $exec{html2ps}: $!";
system ("$exec{ps2pdf}      $file{ps}     $file{pdf}")
    and die "Error running $exec{ps2pdf}: $!";
system ("$exec{html2text} &lt; $file{html} &gt; $file{tmp}")
    and die "Error running $exec{html2text}: $!";

open (TMP,  "&lt; $file{tmp}")  or die "Error opening $file{tmp} RO: $!";
open (TEXT, "&gt; $file{txt}")  or die "Error opening $file{txt} WO: $!";
while (&lt;TMP&gt;) {
    s/&amp;nbsp;/ /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 &lt;!--NewPage--&gt; 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

&lt;/code&gt;</field>
</data>
</node>
