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

📄 ch07_08.htm

📁 By Tom Christiansen and Nathan Torkington ISBN 1-56592-243-3 First Edition, published August 1998
💻 HTM
📖 第 1 页 / 共 2 页
字号:
>while</CODE> <CODECLASS="literal">(&lt;&gt;)</CODE> loop around your program text. It's normally used for filters like <EMCLASS="emphasis">grep</EM> or programs that summarize the data they read. The program is shown in <ACLASS="xref"HREF="ch07_08.htm#ch07-34632"TITLE="findlogin1">Example 7.1</A>.</P><DIVCLASS="example"><H4CLASS="example"><ACLASS="title"NAME="ch07-34632">Example 7.1: findlogin1</A></H4><PRECLASS="programlisting">#!/usr/bin/perl   # <ACLASS="indexterm"NAME="ch07-idx-1000009827-0"></A>findlogin1 - print all lines containing the string &quot;login&quot;   while (&lt;&gt;) {# loop over files on command line 	    print if /login/;     }</PRE></DIV><PCLASS="para">The program in <ACLASS="xref"HREF="ch07_08.htm#ch07-34632"TITLE="findlogin1">Example 7.1</A> could be written as shown in <ACLASS="xref"HREF="ch07_08.htm#ch07-36310"TITLE="findlogin2">Example 7.2</A>.</P><DIVCLASS="example"><H4CLASS="example"><ACLASS="title"NAME="ch07-36310">Example 7.2: findlogin2</A></H4><PRECLASS="programlisting">#!/usr/bin/perl -n     # findlogin2 - print all lines containing the string &quot;login&quot;     print if /login/;</PRE></DIV><PCLASS="para">You can combine the <BCLASS="emphasis.bold">-n</B> and <BCLASS="emphasis.bold">-e</B> options to run Perl code from the command line:</P><PRECLASS="programlisting">% perl -ne 'print if /login/'</PRE><PCLASS="para">The <BCLASS="emphasis.bold">-p</B> option is like <BCLASS="emphasis.bold">-n</B> but it adds a <CODECLASS="literal">print</CODE> at the end of the loop. It's normally used for programs that translate their input. This program is shown in <ACLASS="xref"HREF="ch07_08.htm#ch07-34498"TITLE="lowercase1">Example 7.3</A>.</P><DIVCLASS="example"><H4CLASS="example"><ACLASS="title"NAME="ch07-34498">Example 7.3: lowercase1</A></H4><PRECLASS="programlisting">#!/usr/bin/perl    # <ACLASS="indexterm"NAME="ch07-idx-1000009830-0"></A>lowercase - turn all lines into lowercaseuse locale;while (&lt;&gt;) {                 # loop over lines on command line    s/([^\W0-9_])/\l$1/g;    # change all letters to lowercaseprint;}</PRE></DIV><PCLASS="para">The program in <ACLASS="xref"HREF="ch07_08.htm#ch07-34498"TITLE="lowercase1">Example 7.3</A> could be written as shown in <ACLASS="xref"HREF="ch07_08.htm#ch07-32154"TITLE="lowercase2">Example 7.4</A>.</P><DIVCLASS="example"><H4CLASS="example"><ACLASS="title"NAME="ch07-32154">Example 7.4: lowercase2</A></H4><PRECLASS="programlisting">#!/usr/bin/perl -p     # lowercase - turn all lines into lowercase     use locale;     s/([^\W0-9_])/\l$1/g;# change all letters to lowercase</PRE></DIV><PCLASS="para">Or written from the command line as:</P><PRECLASS="programlisting">% perl -Mlocale -pe 's/([^\W0-9_])/\l$1/g'</PRE><PCLASS="para">While using<BCLASS="emphasis.bold"> -n</B> or <BCLASS="emphasis.bold">-p</B> for implicit input looping, the special label <ACLASS="indexterm"NAME="ch07-idx-1000012067-0"></A><CODECLASS="literal">LINE:</CODE> is silently created for the whole input loop. That means that from an inner loop, you can go on the following input record by using <CODECLASS="literal">next</CODE> <CODECLASS="literal">LINE</CODE> (this is like <EMCLASS="emphasis">awk  </EM>'s <CODECLASS="literal">next</CODE>). Go on to the file by closing ARGV (this is like <EMCLASS="emphasis">awk</EM>  's <CODECLASS="literal">nextfile</CODE>). This is shown in <ACLASS="xref"HREF="ch07_08.htm#ch07-34027"TITLE="countchunks">Example 7.5</A>.</P><DIVCLASS="example"><H4CLASS="example"><ACLASS="title"NAME="ch07-34027">Example 7.5: countchunks</A></H4><PRECLASS="programlisting">#!/usr/bin/perl -n    # <ACLASS="indexterm"NAME="ch07-idx-1000009833-0"></A>countchunks - count how many words are used.    # skip comments, and bail on file if __END__   # or __DATA__ seen.    for (split /\W+/) {     next LINE if /^#/;     close ARGV if /__(DATA|END)__/;     $chunks++;     }     END { print &quot;Found $chunks chunks\n&quot; }</PRE></DIV><PCLASS="para">The <EMCLASS="emphasis">tcsh</EM> keeps a <EMCLASS="emphasis">.history</EM> file in a format such that every other line contains a commented out timestamp in Epoch seconds:</P><PRECLASS="programlisting">#+0894382237     less /etc/motd     #+0894382239     vi ~/.exrc     #+0894382242     date     #+0894382242     who     #+0894382288     telnet home</PRE><PCLASS="para">A simple one-liner can render that legible:</P><PRECLASS="programlisting">% perl -pe 's/^#\+(\d+)\n/localtime($1) . &quot; &quot;/e' <CODECLASS="userinput"><B><CODECLASS="replaceable"><I>Tue May  5 09:30:37 1998     less /etc/motd </I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>Tue May  5 09:30:39 1998     vi ~/.exrc </I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>Tue May  5 09:30:42 1998     date</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>Tue May  5 09:30:42 1998     who </I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>Tue May  5 09:31:28 1998     telnet home</I></CODE></B></CODE></PRE><PCLASS="para">The <BCLASS="emphasis.bold">-i</B> option changes each file on the command line. It is described in <ACLASS="xref"HREF="ch07_10.htm"TITLE="Modifying a File in Place with -i Switch">Recipe 7.9</A>, and is normally used in conjunction with <BCLASS="emphasis.bold">-p</B>.</P><PCLASS="para">You have to say <CODECLASS="literal">use</CODE> <CODECLASS="literal">locale</CODE> to handle current character <ACLASS="indexterm"NAME="ch07-idx-1000009653-0"></A><ACLASS="indexterm"NAME="ch07-idx-1000009653-1"></A>set.<ACLASS="indexterm"NAME="ch07-idx-1000009629-0"></A><ACLASS="indexterm"NAME="ch07-idx-1000009629-1"></A><ACLASS="indexterm"NAME="ch07-idx-1000009629-2"></A></P></DIV></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch07-pgfId-1000008971">See Also</A></H3><PCLASS="para"><ICLASS="filename">perlrun </I>(1), and the <ACLASS="olink"HREF="../prog/ch06_01.htm#PERL2-CH-6-SECT-1.2">"Switches"</A> section of <ACLASS="olink"HREF="../prog/ch06_01.htm">Chapter 6</A> of <ACLASS="citetitle"HREF="../prog/index.htm"TITLE="Programming Perl"><CITECLASS="citetitle">Programming Perl</CITE></A>; <ACLASS="xref"HREF="ch07_10.htm"TITLE="Modifying a File in Place with -i Switch">Recipe 7.9</A>; Recipe 16.6</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="ch07_07.htm"TITLE="7.6. Storing Files Inside Your Program Text"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 7.6. Storing Files Inside Your Program Text"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="ch07_09.htm"TITLE="7.8. Modifying a File in Place with Temporary File"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 7.8. Modifying a File in Place with Temporary File"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228">7.6. Storing Files Inside Your Program Text</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">7.8. Modifying a File in Place with Temporary File</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 + -