?? ch17.htm
字號:
<HTML>
<HEAD>
<TITLE>Chapter 17 -- Using Command-Line Options</TITLE>
<META>
</HEAD>
<BODY TEXT="#000000" BGCOLOR="#FFFFFF" LINK="#0000EE" VLINK="#551A8B" ALINK="#CE2910">
<H1><FONT SIZE=6 COLOR=#FF0000>Chapter 17</FONT></H1>
<H1><FONT SIZE=6 COLOR=#FF0000>Using Command-Line Options</FONT>
</H1>
<HR>
<P>
<CENTER><B><FONT SIZE=5>CONTENTS</FONT></B></CENTER>
<UL>
<LI><A HREF="#HowAretheOptionsSpecified">
How Are the Options Specified?</A>
<LI><A HREF="#WhatAretheOptions">
What Are the Options?</A>
<UL>
<LI><A HREF="#ExampleUsingtheTTFONTSIZEFACECourierFONTTTFONTSIZEOptionFONT">
Example: Using the <TT>-0</TT>
Option</FONT></A>
<LI><A HREF="#ExampleUsingtheTTFONTSIZEFACECouriernFONTTTFONTSIZEandFONTTTFONTSIZEFACECourierpFONTTTFONTSIZEOptionsFONT">
Example: Using the <TT>-n</TT>
and </FONT><TT>-p</TT>
Options</FONT></A>
<LI><A HREF="#ExampleUsingtheTTFONTSIZEFACECourieriFONTTTFONTSIZEOptionFONT">
Example: Using the <TT>-i</TT>
Option</FONT></A>
<LI><A HREF="#ExampleUsingtheTTFONTSIZEFACECouriersFONTTTFONTSIZEOptionFONT">
Example: Using the <TT>-s</TT>
Option</FONT></A>
</UL>
<LI><A HREF="#Summary">
Summary</A>
<LI><A HREF="#ReviewQuestions">
Review Questions</A>
<LI><A HREF="#ReviewExercises">
Review Exercises</A>
</UL>
<HR>
<P>
Perl has a wide range of command-line options or switches that
you can use. The options are also called <I>switches</I> because
they can turn on or turn off different behaviors. A thorough knowledge
of the command line switches will enable you to create short one-time
programs to perform odd little tasks. For example, the <TT>-e</TT>
option lets you specify a line of code directly on the command
line instead of creating a script file. You use the <TT>-l</TT>
option to change the line endings in a text file.
<H2><A NAME="HowAretheOptionsSpecified"><FONT SIZE=5 COLOR=#FF0000>
How Are the Options Specified?</FONT></A></H2>
<P>
The most frequent way to specify command-line options is on the
command line. All of Perl's options are specified using a dash
and then a single character followed by arguments, if needed.
For example,
<BLOCKQUOTE>
<PRE>
perl -I/usr/~john/iNClude script.pl
</PRE>
</BLOCKQUOTE>
<P>
You can combine options with no arguments with the following switch.
The following two command lines are equivalent.
<BLOCKQUOTE>
<PRE>
perl -cI/usr/~john/iNClude script.pl
perl -c -I/usr/~john/iNClude script.pl
</PRE>
</BLOCKQUOTE>
<P>
You can also specify command-line options inside your script file
using the #! line. Just place them following the directory or
executable name. If you are working on a UNIX system, you are
probably familiar with using the #! notation to tell the system
where to find the Perl executable. The various UNIX systems and
Windows can interpret the #! line in different ways. Therefore,
Perl starts parsing the #! switches immediately after the first
instaNCe of <TT>perl</TT> on the line.
For example, if you started your script with this line:
<BLOCKQUOTE>
<PRE>
#!/bin/perl -w
</PRE>
</BLOCKQUOTE>
<P>
Then Perl will run with the <TT>-w</TT>
option in effect.<BR>
<p>
<CENTER>
<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>
<TR><TD><B>Caution</B></TD></TR>
<TR><TD>
<BLOCKQUOTE>
Some UNIX systems will only read the first 32 characters of the #! line. So try to have your options either end before the 32<FONT SIZE=1>nd</FONT> position or start after the 32<FONT SIZE=1>nd</FONT> position. Placing the options after the 32<FONT
SIZE=1>nd</FONT> position will help to make your scripts more portable because you will be bypassing one of the iNConsisteNCies of UNIX.
</BLOCKQUOTE>
</TD></TR>
</TABLE>
</CENTER>
<P>
<H2><A NAME="WhatAretheOptions"><FONT SIZE=5 COLOR=#FF0000>
What Are the Options?</FONT></A></H2>
<P>
Table 17.1 provides a short description of each command-line option
used with Perl. After the table, examples of several options will
be shown.<BR>
<P>
<CENTER><B>Table 17.1 Perl's Command-Line Options</B></CENTER>
<p>
<CENTER>
<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>
<TR><TD WIDTH=67><CENTER><I>Option</I></CENTER></TD><TD COLSPAN=3 WIDTH=523><I>Description</I>
</TD></TR>
<TR><TD WIDTH=67><CENTER><TT>-0</TT></CENTER>
</TD><TD COLSPAN=3 WIDTH=523>Lets you specify the record separator (<TT>$/</TT>) as an octal number. For example, -0055 will cause records to end on a dash. If no number is specified, records will end on null characters. The special value of 00 will place
Perl into paragraph mode. And 0777 will force Perl to read the whole file in one shot because 0777 is not a legal character value. See "Example: Using the <TT>-0</TT> Option" for more information.
</TD></TR>
<TR><TD WIDTH=67><CENTER><TT>-a</TT></CENTER>
</TD><TD COLSPAN=3 WIDTH=523>This option <I>must</I> be used in conjuNCtion with either the <TT>-n</TT> or <TT>-p</TT> option. Using the <TT>-a</TT> option will automatically feed input lines to the <TT>split</TT> fuNCtion. The results of the split are
placed into the <TT>@F</TT> variable. See "Example: Using the <TT>-n</TT> and <TT>-p</TT> Options" for more information.
</TD></TR>
<TR><TD WIDTH=67><CENTER><TT>-c</TT></CENTER>
</TD><TD COLSPAN=3 WIDTH=523>This option lets you check the syntax of your script without fully executing it. The <TT>BEGIN</TT> blocks and <TT>use</TT> statements are still executed because they are needed by the compilation process.
</TD></TR>
<TR><TD WIDTH=67><CENTER><TT>-d</TT></CENTER>
</TD><TD COLSPAN=3 WIDTH=523>This option lets you start the Perl debugger. See <A HREF="ch16.htm" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/ch16.htm" >Chapter 16</A>, "Debugging Perl," for more information.
</TD></TR>
<TR><TD WIDTH=67><CENTER><TT>-D</TT></CENTER>
</TD><TD COLSPAN=3 WIDTH=523>This option lets you turn on different behaviors related to the debugging process. The following table shows you the sub-options that can be used. Please note, however, that not all releases of Perl can use this feature. I
know that the hip port of Perl for Win32 can't. If your version of Perl does not have this option, you will see the message <TT>Recompile perl with -DDEBUGGING to use -D switch</TT> when you try it. If you want to watch your script as it executes, use
-D14. Following is a list of the other values that you can use. You can add the numbers together to specify more than one behavior (such as 8+4+2 = 14) or you can use the letters.
</TD></TR>
<TR><TD WIDTH=67><CENTER> </CENTER></TD><TD WIDTH=78><CENTER>1</CENTER>
</TD><TD WIDTH=75><CENTER>p</CENTER></TD><TD WIDTH=370>Tokenizing and Parsing
</TD></TR>
<TR><TD WIDTH=67><CENTER> </CENTER></TD><TD WIDTH=78><CENTER>2</CENTER>
</TD><TD WIDTH=75><CENTER>s</CENTER></TD><TD WIDTH=370>Stack Snapshots
</TD></TR>
<TR><TD WIDTH=67><CENTER> </CENTER></TD><TD WIDTH=78><CENTER>4</CENTER>
</TD><TD WIDTH=75><CENTER>l</CENTER></TD><TD WIDTH=370>Label Stack Processing
</TD></TR>
<TR><TD WIDTH=67><CENTER> </CENTER></TD><TD WIDTH=78><CENTER>8</CENTER>
</TD><TD WIDTH=75><CENTER>t</CENTER></TD><TD WIDTH=370>Trace Execution
</TD></TR>
<TR><TD WIDTH=67><CENTER> </CENTER></TD><TD WIDTH=78><CENTER>16</CENTER>
</TD><TD WIDTH=75><CENTER>o</CENTER></TD><TD WIDTH=370>Operator Node Construction
</TD></TR>
<TR><TD WIDTH=67><CENTER> </CENTER></TD><TD WIDTH=78><CENTER>32</CENTER>
</TD><TD WIDTH=75><CENTER>c</CENTER></TD><TD WIDTH=370>String/Numeric Conversions
</TD></TR>
<TR><TD WIDTH=67><CENTER> </CENTER></TD><TD WIDTH=78><CENTER>64</CENTER>
</TD><TD WIDTH=75><CENTER>P</CENTER></TD><TD WIDTH=370>Print Preprocessor Command for -P
</TD></TR>
<TR><TD WIDTH=67><CENTER> </CENTER></TD><TD WIDTH=78><CENTER>128</CENTER>
</TD><TD WIDTH=75><CENTER>m</CENTER></TD><TD WIDTH=370>Memory Allocation
</TD></TR>
<TR><TD WIDTH=67><CENTER> </CENTER></TD><TD WIDTH=78><CENTER>256</CENTER>
</TD><TD WIDTH=75><CENTER>f</CENTER></TD><TD WIDTH=370>Format Processing
</TD></TR>
<TR><TD WIDTH=67><CENTER> </CENTER></TD><TD WIDTH=78><CENTER>512</CENTER>
</TD><TD WIDTH=75><CENTER>r</CENTER></TD><TD WIDTH=370>Regular Expression Parsing
</TD></TR>
<TR><TD WIDTH=67><CENTER> </CENTER></TD><TD WIDTH=78><CENTER>1024</CENTER>
</TD><TD WIDTH=75><CENTER>x</CENTER></TD><TD WIDTH=370>Syntax Tree Dump
</TD></TR>
<TR><TD WIDTH=67><CENTER> </CENTER></TD><TD WIDTH=78><CENTER>2048</CENTER>
</TD><TD WIDTH=75><CENTER>u</CENTER></TD><TD WIDTH=370>Tainting Checks
</TD></TR>
<TR><TD WIDTH=67><CENTER> </CENTER></TD><TD WIDTH=78><CENTER>4096</CENTER>
</TD><TD WIDTH=75><CENTER>L</CENTER></TD><TD WIDTH=370>Memory Leaks (not supported any more)
</TD></TR>
<TR><TD WIDTH=67><CENTER> </CENTER></TD><TD WIDTH=78><CENTER>8192</CENTER>
</TD><TD WIDTH=75><CENTER>H</CENTER></TD><TD WIDTH=370>Hash Dump - usurps values( )
</TD></TR>
<TR><TD WIDTH=67><CENTER> </CENTER></TD><TD WIDTH=78><CENTER>16384</CENTER>
</TD><TD WIDTH=75><CENTER>X</CENTER></TD><TD WIDTH=370>Scratchpad Allocation
</TD></TR>
<TR><TD WIDTH=67><CENTER> </CENTER></TD><TD WIDTH=78><CENTER>32768</CENTER>
</TD><TD WIDTH=75><CENTER>D</CENTER></TD><TD WIDTH=370>Cleaning Up
</TD></TR>
<TR><TD WIDTH=67><CENTER><TT>-e</TT></CENTER>
</TD><TD COLSPAN=3 WIDTH=523>The option lets you specify a single line of code on the command line. This line of code will be executed in lieu of a script file. You can use multiple <TT>-e</TT> options to create a multiple line program-although given the
probability of a typing mistake, I'd create a script file instead. Semi-colons must be used to end Perl statements just like a normal script.
</TD></TR>
<TR><TD WIDTH=67><CENTER><TT>-F</TT></CENTER>
</TD><TD COLSPAN=3 WIDTH=523>This option modifies the behavior of the <TT>-a</TT> option. It lets you change the regular expression that is used to split the input lines. For example, <TT>-F /:+/</TT> will split the input line whenever one or more colons
are found. The slashes are optional; they simply delimit the pattern if they are there. I use them for their aesthetic value.
</TD></TR>
<TR><TD WIDTH=67><CENTER><TT>-I</TT></CENTER>
</TD><TD COLSPAN=3 WIDTH=523>This option lets you edit files in-place. It is used in conjuNCtion with the <TT>-n</TT> or <TT>-p</TT> option. See "Example: Using the <TT>-i</TT> option" for more information.
</TD></TR>
<TR><TD WIDTH=67><CENTER><TT>-I</TT></CENTER>
</TD><TD COLSPAN=3 WIDTH=523>This option is used in conjuNCtion with the <TT>-P</TT> option. It tells the C preprocessor where to look for iNClude files. The default search directories iNClude <TT>/usr/iNClude</TT> and <TT>/usr/lib/Perl</TT>.
</TD></TR>
<TR><TD WIDTH=67><CENTER><TT>-l</TT></CENTER>
</TD><TD COLSPAN=3 WIDTH=523>This option turns on line-ending processing. It can be used to set the output line terminator variable (<TT>$/</TT>) by specifying an octal value. See "Example: Using the <TT>-0 </TT>option" for an example of using
octal numbers. If no octal number is specified, the output line terminator is set equal to the input line terminator (such as <TT>$\ = $/;</TT>).
</TD></TR>
<TR><TD WIDTH=67><CENTER><TT>-n</TT></CENTER>
</TD><TD COLSPAN=3 WIDTH=523>This option places a loop around your script. It will automatically read a line from the diamond operator and then execute the script. It is most often used with the <TT>-e </TT>option. See "Examples: Using the
<TT>-n</TT> and <TT>-p</TT> Options" for more information.
</TD></TR>
<TR><TD WIDTH=67><CENTER><TT>-p</TT></CENTER>
</TD><TD COLSPAN=3 WIDTH=523>This option places a loop around your script. It will automatically read a line from the diamond operator, execute the script, and then print $_. It is most often used with the <TT>-e</TT> option. See "Examples: Using the
<TT>-n </TT>and <TT>-p</TT> Options" for more information.
</TD></TR>
<TR><TD WIDTH=67><CENTER><TT>-P</TT></CENTER>
</TD><TD COLSPAN=3 WIDTH=523>This option will invoke the C preprocessor before compiling your script. This might be useful if you have some C programming experieNCe and would like to use the #iNClude and #define facility. The C preprocessor can also be
used for conditional compilation. Use the <TT>-I</TT> option to tell Perl where to find iNClude files.
</TD></TR>
<TR><TD WIDTH=67><CENTER><TT>-s</TT></CENTER>
</TD><TD COLSPAN=3 WIDTH=523>This option lets you define custom switches for your script. See "Examples: Using the <TT>-s</TT> Option" for more information.
</TD></TR>
<TR><TD WIDTH=67><CENTER><TT>-S</TT></CENTER>
</TD><TD COLSPAN=3 WIDTH=523>This option makes Perl search for the script file using the PATH environment variable. It's mostly used with UNIX systems that don't support the #! Line. The docs/perlrun.htm documentation file that comes with your Perl
distribution has more information about this option.
</TD></TR>
<TR><TD WIDTH=67><CENTER><TT>-T</TT></CENTER>
</TD><TD COLSPAN=3 WIDTH=523>This UNIX-based option turns on taint checking. Normally, these checks are only done when running <TT>setuid</TT> or <TT>setgid</TT>. The docs/perlsec.htm documentation file that comes with your Perl distribution has more
information about this option.
</TD></TR>
<TR><TD WIDTH=67><CENTER><TT>-u</TT></CENTER>
</TD><TD COLSPAN=3 WIDTH=523>This UNIX-based option will cause Perl to dump core after compiling your script. See the Perl documentation that came with your Perl distribution for more information.
</TD></TR>
<TR><TD WIDTH=67><CENTER><TT>-U</TT></CENTER>
</TD><TD COLSPAN=3 WIDTH=523>This UNIX-based option will let Perl do unsafe operations. Its use is beyond the scope of this book.
</TD></TR>
<TR><TD WIDTH=67><CENTER><TT>-v</TT></CENTER>
</TD><TD COLSPAN=3 WIDTH=523>This option will display the version and patchlevel of your Perl executable.
</TD></TR>
<TR><TD WIDTH=67><CENTER><TT>-w</TT></CENTER>
</TD><TD COLSPAN=3 WIDTH=523>This option prints warnings about unsafe programming practices. See <A HREF="ch16.htm" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/ch16.htm" >Chapter 16</A>, "Debugging Perl," for more information.
</TD></TR>
<TR><TD WIDTH=67><CENTER><TT>-x</TT></CENTER>
</TD><TD COLSPAN=3 WIDTH=523>This option will let you extract a Perl script from the middle of a file. This feature comes in handy when someone has sent you a script via e-mail. Perl will scan the input file looking for a #! line that contains the word
<TT>perl</TT>. When it is found, it will execute the script until the __<TT>END</TT>__ token is found. If a directory name is specified after the <TT>-x</TT> option, Perl will switch to that directory before executing the script.
</TD></TR>
</TABLE>
</CENTER>
<P>
<P>
As you can see, Perl has quite a few command-line options. Most
of them are designed so that you can do useful things without
needing to create a text file to hold the script. If you are a
system administrator then these options will make you more productive.
You'll be able to manipulate files and data quickly and accurately.
If you're looking to create applications or more complicated programs,
you won't need these options-except for <TT>-w</TT>
and <TT>-d</TT>.
<P>
The rest of the chapter is devoted to demonstrating the <TT>-0</TT>,
<TT>-n</TT>, <TT>-p</TT>,
<TT>-i</TT>, and <TT>-s</TT>
options.
<H3><A NAME="ExampleUsingtheTTFONTSIZEFACECourierFONTTTFONTSIZEOptionFONT">
Example: Using the <TT>-0</TT>
Option</FONT></A></H3>
<P>
The <TT>-0</TT> option will let you
change the record separator. This is useful if your records are
separated by something other than a newline. Let's use the example
of input records separated by a dash instead of a newline. First,
you need to find out the octal value of the dash character. The
easy way to do this is to covert from the decimal value, which
will be displayed if you run the following command line.
<BLOCKQUOTE>
<PRE>
perl -e "print ord('-');"
</PRE>
</BLOCKQUOTE>
<P>
This program will display <TT>45</TT>.
Converting 45<FONT SIZE=1>10</FONT> into octal results in 55<FONT SIZE=1>8</FONT>.
<P>
Next, you'll need an input file to practice with. Listing 17.1
shows a sample input file.
<HR>
<BLOCKQUOTE>
<B>Listing 17.1 17LST01.DAT-Test Input File for the
</B><TT><B><FONT FACE="Courier">-0</FONT></B></TT><B> Option<BR>
</B>
</BLOCKQUOTE>
<BLOCKQUOTE>
<PRE>
Veterinarian-Orthopedist-Dentist-
</PRE>
</BLOCKQUOTE>
<HR>
<P>
Listing 17.2 holds a program that reads the above data file using
the diamond operators. The program will use the dash character
as an end-of-line indicator.
<P>
<IMG SRC="pseudo.gif" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/pseudo.gif" BORDER=1 ALIGN=RIGHT><p>
<BLOCKQUOTE>
<I>Set the record separator to be a dash using the #! switch setting
method.<BR>
Open a file for input.<BR>
Read all of the records into the </I><TT><I>@lines</I></TT><I>
array. One element in </I><TT><I>@lines</I></TT><I>
will be one record.<BR>
Close the file.<BR>
Iterate over the </I><TT><I>@lines</I></TT><I>
array and print each element.</I>
</BLOCKQUOTE>
<HR>
<BLOCKQUOTE>
<B>Listing 17.2 17LST02.PL-Using the </B><TT><B><FONT FACE="Courier">-0</FONT></B></TT><B>
Option to Change the Record Separator<BR>
</B>
</BLOCKQUOTE>
<BLOCKQUOTE>
<PRE>
#!perl -0055
open(FILE, "<test.dat");
@lines = <FILE>;
close(FILE);
foreach (@lines) {
print("$_\n");
}
</PRE>
</BLOCKQUOTE>
<HR>
<p>
<CENTER>
<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>
<TR><TD><B>Tip</B></TD></TR>
<TR><TD>
<BLOCKQUOTE>
Instead of using the command-line option, you could also say <TT>$/ = "-";</TT>. Using the command line is a better option if the line ending changes from input file to input file.
</BLOCKQUOTE>
</TD></TR>
</TABLE>
</CENTER>
<P>
<P>
This program will display:
<BLOCKQUOTE>
<PRE>
Veterinarian-
Orthopedist-
Dentist-
</PRE>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -