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

📄 ch17.htm

📁 prrl 5 programs codes in the book
💻 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-line

indicator. You can use <TT>chop()</TT>

or <TT>chomp()</TT> to remove the

dash, 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 specific

examples, let's see what the loops look like and how they are

changed by the <TT>-a</TT> and <TT>-F</TT>

options.

<P>

The <TT>-n</TT> option causes Perl

to execute your script inside the following loop:

<BLOCKQUOTE>

<PRE>

while (&lt;&gt;) {

    # your script

}

</PRE>

</BLOCKQUOTE>

<P>

The <TT>-p</TT> option uses the same

loop, but adds a <TT>continue</TT>

block so that <TT>$_</TT> will be

printed every time through the loop. If both <TT>-n</TT>

and <TT>-p</TT> are specified on the

command line, the <TT>-p</TT> option

will 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 so

that the loop looks like this:

<BLOCKQUOTE>

<PRE>

while (&lt;&gt;) {

    @F = split(/ /);

    # your script

}

</PRE>

</BLOCKQUOTE>

<P>

The <TT>-F</TT> option lets you split

on 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 or

to open log files. The cleanup section can be used to display

statistics 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's

start with a command line that simply displays each line of the

input 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 Veterinarian

John Orthopedist

Jeff Dentist

</PRE>

</BLOCKQUOTE>

<P>

How about just printing the first word of each line? You could

use 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>

David

John

Jeff

</PRE>

</BLOCKQUOTE>

<P>

If you have data files that store information in columns, you

can 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>

Veterinarian

Orthopedist

Dentist

</PRE>

</BLOCKQUOTE>

<P>

You can use the <TT>-a</TT> option

to get access to information stored in columns. For example, you

could 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 above

command 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 modify

files in-place. This means that Perl will automatically rename

the input file and open the output file using the original name.

You can force Perl to create a backup file by specifying a file

extension for the backup file immediately after the <TT>-i</TT>.

For example, <TT>-i.bak</TT>. If no

extension 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 change

normally requires 10 or more lines of code. However, using command-line

options 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 create

your own custom switches. Custom switches are placed after the

script name but before any filename arguments. Any custom switches

are removed from the <TT>@ARGV</TT>

array. Then a scalar variable is named after the switch is created

and initialized to 1. For example, let's say that you want to

use 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 you

can use with Perl. The options can also be referred to as switches

because 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 place

the options after the 32<FONT SIZE=1>nd</FONT> position to avoid

iNConsistent handling by different versions of UNIX.

<P>

The <TT>-n</TT> option is used to

place 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 when

you 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 you

are doing a lot of text file manipulation.

<P>

The next chapter, &quot;Using Internet Protocols,&quot; introduces

you to some of the different standards used on the Internet. These

standards let you do activities like read mail, send mail, and

transfer 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 ~ character

instead 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 to

see the patchlevel of your version of Perl.

<LI>Use the chomp or chop fuNCtion to remove the dash from the

end 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 execute

it.

<LI>Modify the e-mail message written for Exercise 5 to display

any text that appears after the <TT>__END__</TT>

token. Hint: Use the DATA file handle.

</OL>

<HR>



<CENTER><P><A HREF="ch16.htm" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/ch16.htm"><IMG SRC="pc.gif" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/pc.gif" BORDER=0 HEIGHT=88 WIDTH=140></A>

<A HREF="#CONTENTS"><IMG SRC="cc.gif" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/cc.gif" BORDER=0 HEIGHT=88 WIDTH=140></A>

<A HREF="index-1.htm" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/index-1.htm"><IMG SRC="hb.gif" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/hb.gif" BORDER=0 HEIGHT=88 WIDTH=140></A>

<A HREF="ch18.htm" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/ch18.htm"><IMG SRC="nc.gif" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/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 + -