📄 ch01_13.htm
字号:
<HTML><HEAD><TITLE>Recipe 1.12. Reformatting Paragraphs (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:29:15Z"><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="ch01_01.htm"TITLE="1. Strings"><LINKREL="prev"HREF="ch01_12.htm"TITLE="1.11. Indenting Here Documents"><LINKREL="next"HREF="ch01_14.htm"TITLE="1.13. Escaping Characters"></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="ch01_12.htm"TITLE="1.11. Indenting Here Documents"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 1.11. Indenting Here Documents"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1"><ACLASS="chapter"REL="up"HREF="ch01_01.htm"TITLE="1. Strings"></A></FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch01_14.htm"TITLE="1.13. Escaping Characters"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 1.13. Escaping Characters"BORDER="0"></A></TD></TR></TABLE></DIV><DIVCLASS="sect1"><H2CLASS="sect1"><ACLASS="title"NAME="ch01-chap01_reformatting_0">1.12. Reformatting Paragraphs</A></H2><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch01-pgfId-1417">Problem <ACLASS="indexterm"NAME="ch01-idx-1000010298-0"></A><ACLASS="indexterm"NAME="ch01-idx-1000010298-1"></A><ACLASS="indexterm"NAME="ch01-idx-1000010298-2"></A><ACLASS="indexterm"NAME="ch01-idx-1000010298-3"></A><ACLASS="indexterm"NAME="ch01-idx-1000010298-4"></A></A></H3><PCLASS="para">Your string is too big to fit the screen, and you want to break it up into lines of words, without splitting a word between lines. For instance, a style correction script might read a text file a paragraph at a time, replacing bad phrases with good ones. Replacing a phrase like <EMCLASS="emphasis">utilizes the inherent functionality of</EM> with <EMCLASS="emphasis">uses</EM> will change the length of lines, so it must somehow reformat the paragraphs when they're output.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch01-pgfId-1423">Solution</A></H3><PCLASS="para">Use the standard Text::Wrap module to put line breaks at the right place.</P><PRECLASS="programlisting">use Text::Wrap;@OUTPUT = wrap($LEADTAB, $NEXTTAB, @PARA);</PRE></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch01-pgfId-1433">Discussion</A></H3><PCLASS="para">The Text::Wrap module provides the <CODECLASS="literal">wrap</CODE> function, shown in <ACLASS="xref"HREF="ch01_13.htm#ch01-10808"TITLE="wrapdemo">Example 1.3</A>, which takes a list of lines and reformats them into a paragraph having no line more than<ACLASS="indexterm"NAME="ch01-idx-1000010299-0"></A> <CODECLASS="literal">$Text::Wrap::columns</CODE> characters long. We set <CODECLASS="literal">$columns</CODE> to 20, ensuring that no line will be longer than 20 characters. We pass <CODECLASS="literal">wrap</CODE> two arguments before the list of lines: the first is the indent for the first line of output, the second the indent for every subsequent line.</P><DIVCLASS="example"><H4CLASS="example"><ACLASS="title"NAME="ch01-10808">Example 1.3: wrapdemo</A></H4><PRECLASS="programlisting">#!/usr/bin/perl -w# <ACLASS="indexterm"NAME="ch01-idx-1000010671-0"></A>wrapdemo - show how Text::Wrap works@input = ("Folding and splicing is the work of an editor,", "not a mere collection of silicon", "and", "mobile electrons!");use Text::Wrap qw($columns &wrap);$columns = 20;print "0123456789" x 2, "\n";print wrap(" ", " ", @input), "\n";</PRE></DIV><PCLASS="para">The result of this program is:</P><PRECLASS="programlisting"><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>01234567890123456789</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I> Folding and</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I> splicing is the</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I> work of an</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I> editor, not a</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I> mere collection</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I> of silicon and</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I> mobile electrons!</I></CODE></B></CODE></PRE><PCLASS="para">We get back a single string, with newlines ending each line but the last:</P><PRECLASS="programlisting"># merge multiple lines into one, then wrap one long lineuse Text::Wrap;undef $/;print wrap('', '', split(/\s*\n\s*/, <>));</PRE><PCLASS="para">If you have the <ACLASS="indexterm"NAME="ch01-idx-1000010305-0"></A>Term::ReadKey module (available from CPAN) on your system, you can use it to determine your window size so you can wrap lines to fit the current screen size. If you don't have the module, sometimes the screen size can be found in <CODECLASS="literal">$ENV{COLUMNS}</CODE> or by parsing the output of the <EMCLASS="emphasis">stty</EM> command.</P><PCLASS="para">The following program tries to reformat both short and long lines within a paragraph, similar to the <EMCLASS="emphasis">fmt</EM><ACLASS="indexterm"NAME="ch01-idx-1000011513-0"></A> program, by setting the input record separator <CODECLASS="literal">$/</CODE> to the empty string (causing < > to read paragraphs) and the output record separator <CODECLASS="literal">$\</CODE> to two newlines. Then the paragraph is converted into one long line by changing all newlines (and any surrounding whitespace) to single spaces. Finally, we call the <CODECLASS="literal">wrap</CODE> function with both leading and subsequent tab strings set to the empty string so we can have block paragraphs.</P><PRECLASS="programlisting">use Text::Wrap qw(&wrap $columns);use Term::ReadKey qw(GetTerminalSize);($columns) = GetTerminalSize();($/, $\) = ('', "\n\n"); # read by paragraph, output 2 newlineswhile (<>) { # grab a full paragraph s/\s*\n\s*/ /g; # convert intervening newlines to spaces print wrap('', '', $_); # and format}</PRE></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch01-pgfId-1513">See Also</A></H3><PCLASS="para">The <CODECLASS="literal">split</CODE> and <CODECLASS="literal">join</CODE> functions in <EMCLASS="emphasis">perlfunc </EM>(1) and <ACLASS="olink"HREF="../prog/ch03_01.htm">Chapter 3</A> of <ACLASS="citetitle"HREF="../prog/index.htm"TITLE="Programming Perl"><CITECLASS="citetitle">Programming Perl</CITE></A>; the manpage for the standard Text::Wrap module, also in <ACLASS="olink"HREF="../prog/ch07_01.htm">Chapter 7</A> of <ACLASS="citetitle"HREF="../prog/index.htm"TITLE="Programming Perl"><CITECLASS="citetitle">Programming Perl</CITE></A>; the CPAN module Term::ReadKey, and its use in <ACLASS="xref"HREF="ch15_07.htm"TITLE="Reading from the Keyboard">Recipe 15.6</A> <ACLASS="indexterm"NAME="ch01-idx-1000010311-0"></A><ACLASS="indexterm"NAME="ch01-idx-1000010311-1"></A><ACLASS="indexterm"NAME="ch01-idx-1000010311-2"></A><ACLASS="indexterm"NAME="ch01-idx-1000010311-3"></A><ACLASS="indexterm"NAME="ch01-idx-1000010311-4"></A></P></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="ch01_12.htm"TITLE="1.11. Indenting Here Documents"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 1.11. Indenting Here Documents"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="ch01_14.htm"TITLE="1.13. Escaping Characters"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 1.13. Escaping Characters"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228">1.11. Indenting Here Documents</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">1.13. Escaping Characters</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 + -