Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Monks whose opinions I respect have suggested a couple things to improve the quality of my scripts:
  1. use hash values instead of buckets o' scalar vars
  2. 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/&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 <!--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

In reply to hash n' params by ybiC

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (7)
As of 2024-04-19 10:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found