⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch20_16.htm

📁 By Tom Christiansen and Nathan Torkington ISBN 1-56592-243-3 First Edition, published August 1998
💻 HTM
字号:
<HTML><HEAD><TITLE>Recipe 20.15. Program: hrefsub (Perl Cookbook)</TITLE><METANAME="DC.title"CONTENT="Perl Cookbook"><METANAME="DC.creator"CONTENT="Tom Christiansen &amp; Nathan Torkington"><METANAME="DC.publisher"CONTENT="O'Reilly &amp; Associates, Inc."><METANAME="DC.date"CONTENT="1999-07-02T01:46:04Z"><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_15.htm"TITLE="20.14. Program: htmlsub"></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="ALT="Search this book"><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_15.htm"TITLE="20.14. Program: htmlsub"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 20.14. Program: htmlsub"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"><a href="index/index.htm"><img border="0" src="../gifs/txtnexta.gif"></a></TD></TR></TABLE></DIV><DIVCLASS="sect1"><H2CLASS="sect1"><ACLASS="title"NAME="ch20-chap20_program_1">20.15. Program: hrefsub</A></H2><PCLASS="para"><ACLASS="indexterm"NAME="ch20-idx-1000002703-0"></A><ACLASS="indexterm"NAME="ch20-idx-1000002703-1"></A>hrefsub makes substitutions in HTML files, so that the changes only apply to the text in HREF fields of &lt;<CODECLASS="literal">A</CODE> <CODECLASS="literal">HREF=&quot;...&quot;</CODE>&gt; tags. For instance, if you had the scooby.html file from the previous example, and you've moved shergold.html to be cards.html, you need simply say:</P><PRECLASS="programlisting">% hrefsub shergold.html cards.html scooby.html<CODECLASS="userinput"><B><CODECLASS="replaceable"><I>&lt;HTML&gt;&lt;HEAD&gt;&lt;TITLE&gt;Hi!&lt;/TITLE&gt;&lt;/HEAD&gt;&lt;BODY&gt;</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>&lt;H1&gt;Welcome to Scooby World!&lt;/H1&gt;</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>I have &lt;A HREF=&quot;pictures.html&quot;&gt;pictures&lt;/A&gt; of the crazy dog</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>himself.  Here's one!&lt;P&gt;</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>&lt;IMG SRC=&quot;scooby.jpg&quot; ALT=&quot;Good doggy!&quot;&gt;&lt;P&gt;</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>&lt;BLINK&gt;He's my hero!&lt;/BLINK&gt;  I would like to meet him some day,</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>and get my picture taken with him.&lt;P&gt;</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>P.S. I am deathly ill.  &lt;a href=&quot;cards.html&quot;&gt;Please send</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>cards&lt;/A&gt;.</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>&lt;/BODY&gt;&lt;/HTML&gt;</I></CODE></B></CODE></PRE><PCLASS="para">The HTML::Filter manual page has a BUGS section that says:</P><BLOCKQUOTECLASS="blockquote"><PCLASS="para">Comments in declarations are removed from the declarations and then inserted as separate comments after the declaration. If you turn on <CODECLASS="literal">strict_comment()</CODE>, then comments with embedded <CODECLASS="literal">&quot;-\|-&quot;</CODE> are split into multiple comments.</P></BLOCKQUOTE><PCLASS="para">This version of hrefsub will always lowercase the <CODECLASS="literal">&lt;a&gt;</CODE> and the attribute names within this tag when substitution occurs. If <CODECLASS="literal">$foo</CODE> is a multiword string, then the text given to <CODECLASS="literal">MyFilter-&gt;text</CODE> may be broken such that these words do not come together; i.e., the substitution does not work. There should probably be a new option to HTML::Parser to make it not return text until the whole segment has been seen. Also, some people may not be happy with having their 8-bit Latin-1 characters replaced by ugly entities, so htmlsub does that,<CODECLASS="literal"></CODE><ACLASS="indexterm"NAME="ch20-idx-1000004280-0"></A><ACLASS="indexterm"NAME="ch20-idx-1000004280-1"></A><ACLASS="indexterm"NAME="ch20-idx-1000004280-2"></A><ACLASS="indexterm"NAME="ch20-idx-1000004280-3"></A><ACLASS="indexterm"NAME="ch20-idx-1000004280-4"></A> too.</P><DIVCLASS="example"><H4CLASS="example"><ACLASS="title"NAME="ch20-pgfId-1000004374">Example 20.12: hrefsub</A></H4><PRECLASS="programlisting">#!/usr/bin/perl -w# hrefsub - make substitutions in &lt;A HREF=&quot;...&quot;&gt; fields of HTML files# from Gisle Aas &lt;gisle@aas.no&gt;sub usage { die &quot;Usage: $0 &lt;from&gt; &lt;to&gt; &lt;file&gt;...\n&quot; }my $from = shift or usage;my $to   = shift or usage;usage unless @ARGV;# The HTML::Filter subclass to do the substitution.package MyFilter;require HTML::Filter;@ISA=qw(HTML::Filter);use HTML::Entities qw(encode_entities);sub start {   my($self, $tag, $attr, $attrseq, $orig) = @_;   if ($tag eq 'a' &amp;&amp; exists $attr-&gt;{href}) {           if ($attr-&gt;{href} =~ s/\Q$from/$to/g) {               # must reconstruct the start tag based on $tag and $attr.               # wish we instead were told the extent of the 'href' value               # in $orig.               my $tmp = &quot;&lt;$tag&quot;;               for (@$attrseq) {                   my $encoded = encode_entities($attr-&gt;{$_});                   $tmp .= qq( $_=&quot;$encoded &quot;);               }               $tmp .= &quot;&gt;&quot;;               $self-&gt;output($tmp);               return;           }   }   $self-&gt;output($orig);}# Now use the class.package main;foreach (@ARGV) {        MyFilter-&gt;new-&gt;parse_file($_);}</PRE></DIV><PCLASS="para"></P></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_15.htm"TITLE="20.14. Program: htmlsub"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 20.14. Program: htmlsub"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"><a href="index/index.htm"><img border="0" src="../gifs/txtnexta.gif"></a></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228">20.14. Program: htmlsub</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">Index</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 &copy; 2002</a> O'Reilly &amp; 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 + -