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

📄 ch17.htm

📁 this is a book on pearl , simple example with explanation is given here. it could be beneficial for
💻 HTM
📖 第 1 页 / 共 2 页
字号:
</BLOCKQUOTE><P>Notice that the end-of-line indicator is left as part of the record.This behavior also happens when the newline is used as the end-of-lineindicator. You can use <TT>chop()</TT>or <TT>chomp()</TT> to remove thedash, if needed.<H3><A NAME="ExampleUsingtheTTFONTSIZEFACECouriernFONTTTFONTSIZEandFONTTTFONTSIZEFACECourierpFONTTTFONTSIZEOptionsFONT">Example: Using the <TT>-n</TT>and </FONT><TT>-p</TT>Options</FONT></A></H3><P>The <TT>-n</TT> and <TT>-p</TT>options wrap your script inside loops. Before looking at specificexamples, let's see what the loops look like and how they arechanged by the <TT>-a</TT> and <TT>-F</TT>options.<P>The <TT>-n</TT> option causes Perlto execute your script inside the following loop:<BLOCKQUOTE><PRE>while (&lt;&gt;) {    # your script}</PRE></BLOCKQUOTE><P>The <TT>-p</TT> option uses the sameloop, but adds a <TT>continue</TT>block so that <TT>$_</TT> will beprinted every time through the loop. If both <TT>-n</TT>and <TT>-p</TT> are specified on thecommand line, the <TT>-p</TT> optionwill take precedeNCe. The loop looks like this:<BLOCKQUOTE><PRE>while (&lt;&gt;) {    # your script} continue {    print;}</PRE></BLOCKQUOTE><P>The <TT>-a</TT> option adds a <TT>split()</TT>fuNCtion call to the beginning of each iteration of the loop sothat the loop looks like this:<BLOCKQUOTE><PRE>while (&lt;&gt;) {    @F = split(/ /);    # your script}</PRE></BLOCKQUOTE><P>The <TT>-F</TT> option lets you spliton something besides the space character. If you used <TT>-F/</TT>i<TT>+/</TT>on the command line, the loop would look like this:<BLOCKQUOTE><PRE>while (&lt;&gt;) {    @F = split(/i+/);    # your script}</PRE></BLOCKQUOTE><P>You can use <TT>BEGIN</TT> and <TT>END</TT>blocks if you need to specify some initialization or cleanup code.The initialization section might be used to create objects orto open log files. The cleanup section can be used to displaystatistics or close files. For example,<BLOCKQUOTE><PRE>BEGIN {    # initialization section    $count = 0;}while (&lt;&gt;) {    # your script}END {    # cleanup section    print(&quot;The count was $count.\n&quot;);}</PRE></BLOCKQUOTE><P>Next, you'll see some examples of these options in action. Let'sstart with a command line that simply displays each line of theinput file-like the <TT>type</TT>command in DOS and UNIX.<BLOCKQUOTE><PRE>perl -p -e &quot;1;&quot; test.dat</PRE></BLOCKQUOTE><P>This command line is equivalent to:<BLOCKQUOTE><PRE>while (&lt;&gt;) {    1;} continue {    print;}<BR></PRE></BLOCKQUOTE><p><CENTER><TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%><TR><TD><B>Note</B></TD></TR><TR><TD><BLOCKQUOTE>The <TT>1;</TT> statement was used to give Perl something to process. Otherwise, Perl would not have had any statements to execute.</BLOCKQUOTE></TD></TR></TABLE></CENTER><P><P>And will display:<BLOCKQUOTE><PRE>David VeterinarianJohn OrthopedistJeff Dentist</PRE></BLOCKQUOTE><P>How about just printing the first word of each line? You coulduse this com-<BR>mand line:<BLOCKQUOTE><PRE>perl -p -e &quot;s/\s*(\w+).*/$1/;&quot; test.dat</PRE></BLOCKQUOTE><P>which is equivalent to:<BLOCKQUOTE><PRE>while (&lt;&gt;) {    s/\s*(\w+).*/$1/;} continue {    print;}</PRE></BLOCKQUOTE><P>And will display:<BLOCKQUOTE><PRE>DavidJohnJeff</PRE></BLOCKQUOTE><P>If you have data files that store information in columns, youcan pull out the second column of information like this:<BLOCKQUOTE><PRE>perl -p -e &quot;s/\s*.+\s(.+)\s*/$1\n/;&quot; test.dat</PRE></BLOCKQUOTE><P>which will display:<BLOCKQUOTE><PRE>VeterinarianOrthopedistDentist</PRE></BLOCKQUOTE><P>You can use the <TT>-a</TT> optionto get access to information stored in columns. For example, youcould also display the second column like this:<BLOCKQUOTE><PRE>perl -p -a -e &quot;$_ = \&quot;$F[1]\n\&quot;;&quot; test.dat</PRE></BLOCKQUOTE><P>which is equivalent to<BLOCKQUOTE><PRE> while (&lt;&gt;) {    @F = split(/ /);    $_ = \&quot;$F[1]\n\&quot;;} continue {    print;}</PRE></BLOCKQUOTE><P>Notice that you need to escape the double-quotes in the abovecommand line. If you don't do this you will get an error message.<H3><A NAME="ExampleUsingtheTTFONTSIZEFACECourieriFONTTTFONTSIZEOptionFONT">Example: Using the <TT>-i</TT>Option</FONT></A></H3><P>The <TT>-i</TT> option lets you modifyfiles in-place. This means that Perl will automatically renamethe input file and open the output file using the original name.You can force Perl to create a backup file by specifying a fileextension for the backup file immediately after the <TT>-i</TT>.For example, <TT>-i.bak</TT>. If noextension is specified, no backup file will be kept.<P>One of the more popular uses for the <TT>-i</TT>option is to change sequeNCes of characters. This kind of changenormally requires 10 or more lines of code. However, using command-lineoptions you can do it like this:<BLOCKQUOTE><PRE>perl -p -i.bak -e &quot;s/harry/tom/g;&quot; test.dat</PRE></BLOCKQUOTE><P>This command-line will change all occurreNCes of &quot;harry&quot;to &quot;tom&quot; in the <TT>test.dat</TT>file.<H3><A NAME="ExampleUsingtheTTFONTSIZEFACECouriersFONTTTFONTSIZEOptionFONT">Example: Using the <TT>-s</TT>Option</FONT></A></H3><P>The <TT>-s</TT> option lets you createyour own custom switches. Custom switches are placed after thescript name but before any filename arguments. Any custom switchesare removed from the <TT>@ARGV</TT>array. Then a scalar variable is named after the switch is createdand initialized to 1. For example, let's say that you want touse a switch called <TT>-useTR</TT>in a script like the one in Listing 17.3.<HR><BLOCKQUOTE><B>Listing 17.3&nbsp;&nbsp;17LST03.PL-Checking for the </B><TT><B><FONT FACE="Courier">useTR</FONT></B></TT><B>Switch<BR></B></BLOCKQUOTE><BLOCKQUOTE><PRE>if ($useTR) {    # do TR processing.    print &quot;useTR=$useTR\n&quot;;}</PRE></BLOCKQUOTE><HR><P>You might execute this program using this following command line:<BLOCKQUOTE><PRE>perl -s -w 17lst03.pl -useTR</PRE></BLOCKQUOTE><P>and it would display:<BLOCKQUOTE><PRE>useTR=1</PRE></BLOCKQUOTE><H2><A NAME="Summary"><FONT SIZE=5 COLOR=#FF0000>Summary</FONT></A></H2><P>This chapter covered the different command-line options that youcan use with Perl. The options can also be referred to as switchesbecause they turn different behaviors on and off.<P>The switches can be specified on the command line or using the#! line inside your script. If you use the #! line, try to placethe options after the 32<FONT SIZE=1>nd</FONT> position to avoidiNConsistent handling by different versions of UNIX.<P>The <TT>-n</TT> option is used toplace your script inside of an input loop. The <TT>-p</TT>option uses the same loop, but also prints the <TT>$_</TT>variable after each pass through the loop. The <TT>-a</TT>and <TT>-F</TT> options are used whenyou want the input lines to be split into the <TT>@F</TT>array.<P>Another very useful option is <TT>-i</TT>,which lets you edit files in-place. This option is good when youare doing a lot of text file manipulation.<P>The next chapter, &quot;Using Internet Protocols,&quot; introducesyou to some of the different standards used on the Internet. Thesestandards let you do activities like read mail, send mail, andtransfer files.<H2><A NAME="ReviewQuestions"><FONT SIZE=5 COLOR=#FF0000>Review Questions</FONT></A></H2><P>Answers to Review Questions are in Appendix A.<OL><LI>What is a command-line option?<LI>What are the two places that the switches can be specified?<LI>What switch should always be used?<LI>Which switch lets you read records that end with the ~ characterinstead of the newline?<LI>What two options can be used with the <TT>-n</TT>option?<LI>How can you execute a script that someone sent you via e-mail?<LI>What happens if you specify both the <TT>-v</TT>and the <TT>-c</TT> options?</OL><H2><A NAME="ReviewExercises"><FONT SIZE=5 COLOR=#FF0000>Review Exercises</FONT></A></H2><OL><LI>Use the <TT>-v</TT> option tosee the patchlevel of your version of Perl.<LI>Use the chomp or chop fuNCtion to remove the dash from theend of the records printed by the program in Listing 17.2.<LI>Write a program that uses the <TT>-p</TT>option to display the third column.<LI>Modify the program written in Exercise 3 to use a <TT>BEGIN</TT>block to ask the user which column to display.<LI>Create a sample e-mail message that contains a Perl script.Use the <TT>-x</TT> option to executeit.<LI>Modify the e-mail message written for Exercise 5 to displayany text that appears after the <TT>__END__</TT>token. Hint: Use the DATA file handle.</OL><HR><CENTER><P><A HREF="ch16.htm"><IMG SRC="pc.gif" BORDER=0 HEIGHT=88 WIDTH=140></A><A HREF="#CONTENTS"><IMG SRC="cc.gif" BORDER=0 HEIGHT=88 WIDTH=140></A><A HREF="index.htm"><IMG SRC="hb.gif" BORDER=0 HEIGHT=88 WIDTH=140></A><A HREF="ch18.htm"><IMG SRC="nc.gif" BORDER=0 HEIGHT=88 WIDTH=140></A><HR WIDTH="100%"></P></CENTER></BODY></HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -