Skip to Content
Perl Cookbook
book

Perl Cookbook

by Tom Christiansen, Nathan Torkington
August 1998
Intermediate to advanced
800 pages
39h 20m
English
O'Reilly Media, Inc.
Content preview from Perl Cookbook

Program: htmlsub

This program makes substitutions in HTML files so that the changes only happen in normal text. If you had the file scooby.html that contained:

<HTML><HEAD><TITLE>Hi!</TITLE></HEAD><BODY>
<H1>Welcome to Scooby World!</H1>
I have <A HREF="pictures.html">pictures</A> of the crazy dog
himself.  Here's one!<P>
<IMG SRC="scooby.jpg" ALT="Good doggy!"><P>
<BLINK>He's my hero!</BLINK>  I would like to meet him some day,
and get my picture taken with him.<P>
P.S. I am deathly ill.  <A HREF="shergold.html">Please send
cards</A>.
</BODY></HTML>

You can use htmlsub change every occurrence of the word ``picture'' in the document text to read ``photo”. It prints the new document on STDOUT:

% htmlsub picture photo scooby.html

               <HTML><HEAD><TITLE>Hi!</TITLE></HEAD><BODY>
            
               <H1>Welcome to Scooby World!</H1>
            
               I have <A HREF="pictures.html" >photos</A> of the crazy dog
            
               himself.  Here's one!<P>
            
               <IMG SRC="scooby.jpg" ALT="Good doggy!"><P>
            
               <BLINK>He's my hero!</BLINK>  I would like to meet him some day,
            
               and get my photo taken with him.<P>
            
               P.S. I am deathly ill.  <A HREF="shergold.html" >Please send
            
               cards</A>.
            
               </BODY></HTML>

The program is shown in Example 20.11.

Example 20-11. htmlsub

#!/usr/bin/perl -w # htmlsub - make substitutions in normal text of HTML files # from Gisle Aas <gisle@aas.no> sub usage { die "Usage: $0 <from> <to> <file>...\n" } my $from = shift or usage; my $to = shift or usage; usage unless @ARGV; # Build the HTML::Filter subclass to do the substituting. package MyFilter; ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Perl Cookbook, 2nd Edition

Perl Cookbook, 2nd Edition

Tom Christiansen, Nathan Torkington
Perl One-Liners

Perl One-Liners

Peteris Krumins
Perl Best Practices

Perl Best Practices

Damian Conway
Perl in a Nutshell, 2nd Edition

Perl in a Nutshell, 2nd Edition

Nathan Patwardhan, Ellen Siever, Stephen Spainhour

Publisher Resources

ISBN: 1565922433Supplemental ContentCatalog PageErrata