📄 ch20_15.htm
字号:
<HTML><HEAD><TITLE>Recipe 20.14. Program: htmlsub (Perl Cookbook)</TITLE><METANAME="DC.title"CONTENT="Perl Cookbook"><METANAME="DC.creator"CONTENT="Tom Christiansen & Nathan Torkington"><METANAME="DC.publisher"CONTENT="O'Reilly & Associates, Inc."><METANAME="DC.date"CONTENT="1999-07-02T01:46:03Z"><METANAME="DC.type"CONTENT="Text.Monograph"><METANAME="DC.format"CONTENT="text/html"SCHEME="MIME"><METANAME="DC.source"CONTENT="1-56592-243-3"SCHEME="ISBN"><METANAME="DC.language"CONTENT="en-US"><METANAME="generator"CONTENT="Jade 1.1/O'Reilly DocBook 3.0 to HTML 4.0"><LINKREV="made"HREF="mailto:online-books@oreilly.com"TITLE="Online Books Comments"><LINKREL="up"HREF="ch20_01.htm"TITLE="20. Web Automation"><LINKREL="prev"HREF="ch20_14.htm"TITLE="20.13. Processing Server Logs"><LINKREL="next"HREF="ch20_16.htm"TITLE="20.15. Program: hrefsub"></HEAD><BODYBGCOLOR="#FFFFFF"><img alt="Book Home" border="0" src="gifs/smbanner.gif" usemap="#banner-map" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="Perl Cookbook"><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><p><TABLEWIDTH="684"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch20_14.htm"TITLE="20.13. Processing Server Logs"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 20.13. Processing Server Logs"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1"><ACLASS="chapter"REL="up"HREF="ch20_01.htm"TITLE="20. Web Automation"></A></FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch20_16.htm"TITLE="20.15. Program: hrefsub"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 20.15. Program: hrefsub"BORDER="0"></A></TD></TR></TABLE></DIV><DIVCLASS="sect1"><H2CLASS="sect1"><ACLASS="title"NAME="ch20-chap20_program_0">20.14. Program: htmlsub</A></H2><PCLASS="para"><CODECLASS="literal"></CODE><ACLASS="indexterm"NAME="ch20-idx-1000002695-0"></A><ACLASS="indexterm"NAME="ch20-idx-1000002695-1"></A><ACLASS="indexterm"NAME="ch20-idx-1000002695-2"></A><ACLASS="indexterm"NAME="ch20-idx-1000002695-3"></A>This program makes substitutions in HTML files so that the changes only happen in normal text. If you had the file index.html that contained:</P><PRECLASS="programlisting"><HTML><HEAD><TITLE>Hi!</TITLE></HEAD><BODY><H1>Welcome to Scooby World!</H1>I have <A HREF="pictures.html">pictures</A> of the crazy doghimself. 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 sendcards</A>.</BODY></HTML></PRE><PCLASS="para">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:</P><PRECLASS="programlisting">% htmlsub picture photo scooby.html<CODECLASS="userinput"><B><CODECLASS="replaceable"><I><HTML><HEAD><TITLE>Hi!</TITLE></HEAD><BODY></I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I><H1>Welcome to Scooby World!</H1></I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>I have <A HREF="pictures.html">photos</A> of the crazy dog</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>himself. Here's one!<P></I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I><IMG SRC="scooby.jpg" ALT="Good doggy!"><P></I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I><BLINK>He's my hero!</BLINK> I would like to meet him some day,</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>and get my photo taken with him.<P></I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>P.S. I am deathly ill. <A HREF="shergold.html">Please send</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>cards</A>.</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I></BODY></HTML></I></CODE></B></CODE></PRE><PCLASS="para">The program is shown in <ACLASS="xref"HREF="ch20_15.htm#ch20-14593"TITLE="htmlsub">Example 20.11</A>.</P><DIVCLASS="example"><H4CLASS="example"><ACLASS="title"NAME="ch20-14593">Example 20.11: htmlsub</A></H4><PRECLASS="programlisting">#!/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;require HTML::Filter;@ISA=qw(HTML::Filter);use HTML::Entities qw(decode_entities encode_entities);sub text{ my $self = shift; my $text = decode_entities($_[0]); $text =~ s/\Q$from/$to/go; # most important line $self->SUPER::text(encode_entities($text));}# Now use the class.package main;foreach (@ARGV) { MyFilter->new->parse_file($_);}<CODECLASS="literal"></CODE><ACLASS="indexterm"NAME="ch20-idx-1000003462-0"></A></PRE></DIV></DIV><DIVCLASS="htmlnav"><P></P><HRALIGN="LEFT"WIDTH="684"TITLE="footer"><TABLEWIDTH="684"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch20_14.htm"TITLE="20.13. Processing Server Logs"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 20.13. Processing Server Logs"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><ACLASS="book"HREF="index.htm"TITLE="Perl Cookbook"><IMGSRC="../gifs/txthome.gif"ALT="Perl Cookbook"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch20_16.htm"TITLE="20.15. Program: hrefsub"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 20.15. Program: hrefsub"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228">20.13. Processing Server Logs</TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><ACLASS="index"HREF="index/index.htm"TITLE="Book Index"><IMGSRC="../gifs/index.gif"ALT="Book Index"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228">20.15. Program: hrefsub</TD></TR></TABLE><HRALIGN="LEFT"WIDTH="684"TITLE="footer"><FONTSIZE="-1"></DIV<!-- LIBRARY NAV BAR --> <img src="../gifs/smnavbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links"><p> <a href="copyrght.htm">Copyright © 2002</a> O'Reilly & Associates. All rights reserved.</font> </p> <map name="library-map"> <area shape="rect" coords="1,0,85,94" href="../index.htm"><area shape="rect" coords="86,1,178,103" href="../lwp/index.htm"><area shape="rect" coords="180,0,265,103" href="../lperl/index.htm"><area shape="rect" coords="267,0,353,105" href="../perlnut/index.htm"><area shape="rect" coords="354,1,446,115" href="../prog/index.htm"><area shape="rect" coords="448,0,526,132" href="../tk/index.htm"><area shape="rect" coords="528,1,615,119" href="../cookbook/index.htm"><area shape="rect" coords="617,0,690,135" href="../pxml/index.htm"></map> </BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -