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

📄 ch16.htm

📁 《Perl 5 Unreleased》
💻 HTM
📖 第 1 页 / 共 5 页
字号:
<HTML>



<HEAD>

   <TITLE>Chapter 16 -- Command-line Interface with Perl</TITLE>

   <META NAME="GENERATOR" CONTENT="Mozilla/3.0b5aGold (WinNT; I) [Netscape]">

</HEAD>

<BODY TEXT="#000000" BGCOLOR="#FFFFFF" LINK="#0000EE" VLINK="#551A8B" ALINK="#CE2910">

<H1><FONT COLOR=#FF0000>Chapter 16</FONT></H1>

<H1><B><FONT SIZE=5 COLOR=#FF0000>Command-line Interface with Perl</FONT></B>

</H1>

<P>

<HR WIDTH="100%"></P>

<P>

<H3 ALIGN=CENTER><FONT COLOR="#000000"><FONT SIZE=+2>CONTENTS<A NAME="CONTENTS"></A>

</FONT></FONT></H3>



<UL>

<LI><A HREF="#TheCommandlineOptionstoPerl" >The Command-line Options to Perl</A>

<UL>

<LI><A HREF="#SendingOptionsviatheCommandLine" >Sending Options via the Command Line</A>

<LI><A HREF="#SpecifyinganOptionwithintheProgram" >Specifying an Option within the Program</A>

<LI><A HREF="#ThecandwSyntaxCheckingandWarnin" >The -c and -w Syntax Checking and Warning Options</A>

<LI><A HREF="#TheeOptionExecutingaSinglelineP" >The -e Option: Executing a Single-line Program</A>

<LI><A HREF="#ThesOptiontoSupplyCustomCommandl" >The -s Option to Supply Custom Command-line Options</A>

<LI><A HREF="#TheIOptiontoIncludeOtherFiles" >The -I Option to Include Other Files</A>

<LI><A HREF="#ThePOptionforUsingtheCPreprocess" >The -P Option for Using the C Preprocessor</A>

<LI><A HREF="#UsingthenandpOptionsforHandling" >Using the -n and -p Options for Handling Multiple Files</A>

<LI><A HREF="#TheiOptiontoEditFiles" >The -i Option to Edit Files</A>

<LI><A HREF="#UsingtheaOption" >Using the -a Option</A>

<LI><A HREF="#UsingtheFOption" >Using the -F Option</A>

<LI><A HREF="#Usingthe0Option" >Using the -0 Option</A>

<LI><A HREF="#UsingthelOption" >Using the -l Option</A>

<LI><A HREF="#UsingthexOptiontoGetaPerlProgra" >Using the -x Option to Get a Perl Program from Another File</A>

<LI><A HREF="#UsingtheSOption" >Using the -S Option</A>

<LI><A HREF="#ThevOptionPrintingthePerlVersion" >The -v Option: Printing the Perl Version Number</A>

<LI><A HREF="#UsingConditionalCodewiththeCPrepro" >Using Conditional Code with the C Preprocessor</A>

</UL>

<LI><A HREF="#ReadingInputfromSTDIN" >Reading Input from STDIN</A>

<UL>

<LI><A HREF="#TheTermQueryModule" >The Term::Query Module</A>

<LI><A HREF="#UsingtheTermQueryModule" >Using the Term::Query Module</A>

<LI><A HREF="#ExtendingtheQuerypmModule" >Extending the Query.pm Module</A>

<LI><A HREF="#Usingquerytable" >Using query_table()</A>

</UL>

<LI><A HREF="#TheGetoptsPackage" >The Getopts Package</A>

<UL>

<LI><A HREF="#UsingStdpm" >Using Std.pm</A>

<LI><A HREF="#TheLongpmmodule" >The Long.pm module</A>

<LI><A HREF="#ExamplesofOptionsSettings" >Examples of Options Settings</A>

</UL>

<LI><A HREF="#Summary" >Summary</A>

</UL>

<HR>

<P>

This chapter introduces you to handling the options available

from the command-line interface to the Perl interpreter, handling

user input, and writing interactive Perl scripts. 

<P>

By using the command-line options in Perl, you can determine how

to best use the Perl interpreter to take care of details such

as handling loops on the input, creating and destroying temporary

files, and handling multiple files. 

<P>

Of course, you would want to be able to process the incoming options

to your own programs as well. Writing scripts to handle user responses

takes an inordinate amount of time and effort given the infinite

number of responses you can receive. When passing installation

scripts for a software package, it would be nice if the scripts

were intelligent enough to filter out most of the incorrect responses.

In this chapter, you work with Perl modules that eliminate some

of the grunt work.

<H2><A NAME="TheCommandlineOptionstoPerl"><FONT SIZE=5 COLOR=#FF0000>The

Command-line Options to Perl</FONT></A></H2>

<P>

Perl's command-line options provide many features, such as checking

syntax, printing warnings, using the C preprocessor, and modifying

the way output is printed in a Perl document. There are two ways

to provide options to a Perl program: either by passing them in

the command line along with the command you enter to start the

Perl program or in the comment header of your Perl program script.

<H3><A NAME="SendingOptionsviatheCommandLine">Sending Options

via the Command Line</A></H3>

<P>

You can always enter options for a Perl program on the command

line. The syntax for specifying options on the command line is

<BLOCKQUOTE>

<TT><FONT FACE="Courier">perl <I>options programName</I></FONT></TT>

</BLOCKQUOTE>

<P>

where <TT><I><FONT FACE="Courier">programName</FONT></I></TT>

 is the name of the Perl program to run, and <TT><I><FONT FACE="Courier">options</FONT></I></TT>

is the list of options to provide to the program being run. For

example, the command

<BLOCKQUOTE>

<TT><FONT FACE="Courier">perl -d -w test1</FONT></TT>

</BLOCKQUOTE>

<P>

runs the Perl program named <TT><FONT FACE="Courier">test1</FONT></TT>

and passes it the options <TT><FONT FACE="Courier">-d</FONT></TT>

and <TT><FONT FACE="Courier">-w</FONT></TT>. You'll learn about

the actions of these options in the following sections. Some options

require an additional value. For example, the <TT><FONT FACE="Courier">-I</FONT></TT>

option requires a pathname for include files. 

<BLOCKQUOTE>

<TT><FONT FACE="Courier">perl -I /usr/local/include/special  something</FONT></TT>

</BLOCKQUOTE>

<P>

The <TT><FONT FACE="Courier">/usr/local/include/special</FONT></TT>

path is also searched for a file if it is not found via the <TT><FONT FACE="Courier">@Inc</FONT></TT>

path. It is not necessary to put a space between the option and

its argument.

<BLOCKQUOTE>

<TT><FONT FACE="Courier">perl -I/usr/local/include/special  something

</FONT></TT>

</BLOCKQUOTE>

<P>

In all cases, any value associated with an option must always

immediately follow the option.

<P>

Options that do not require an associated value can be grouped

without the use of an additional dash (-) character or space.

For example, the following two commands do the same thing:

<BLOCKQUOTE>

<TT><FONT FACE="Courier">perl -d -w  test1<BR>

perl -dw  test1</FONT></TT>

</BLOCKQUOTE>

<P>

The last option in a group can have additional values. For example,

the following two commands do the same thing:

<BLOCKQUOTE>

<TT><FONT FACE="Courier">perl -w -I/usr/local/include/special

 something <BR>

perl -wI/usr/local/include/special  something </FONT></TT>

</BLOCKQUOTE>

<H3><A NAME="SpecifyinganOptionwithintheProgram">Specifying an

Option within the Program</A></H3>

<P>

The command line at the start of a program that includes a header

comment (a comment beginning with the <TT><FONT FACE="Courier">#!</FONT></TT>

characters) can be used to pass options to Perl. For example,

the following line:

<BLOCKQUOTE>

<TT><FONT FACE="Courier">#!perl -w</FONT></TT>

</BLOCKQUOTE>

<P>

will pass the <TT><FONT FACE="Courier">-w</FONT></TT> option to

Perl. Historically, only one argument could be passed to Perl

this way, but now you can pass several options. A word of caution

is necessary here: Options specified on the command line will

override options specified in the header comment. For example,

if your header comment is

<BLOCKQUOTE>

<TT><FONT FACE="Courier">#!perl -d</FONT></TT>

</BLOCKQUOTE>

<P>

and you start your program with the following command, the program

will run with the <TT><FONT FACE="Courier">-w</FONT></TT> option

specified but not the <TT><FONT FACE="Courier">-d</FONT></TT>

option:

<BLOCKQUOTE>

<TT><FONT FACE="Courier">perl -w test1</FONT></TT>

</BLOCKQUOTE>

<P>

Table 16.1 lists some of the command-line options to Perl. <BR>

<P>

<CENTER><B>Table 16.1. Command-line options to Perl.</B> </CENTER>

<CENTER>

<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>

<TR VALIGN=TOP><TD WIDTH=66><CENTER><I>Option</I></CENTER></TD><TD WIDTH=524><I>Meaning</I>

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=66><CENTER><TT><FONT FACE="Courier">-c</FONT></TT></CENTER>

</TD><TD WIDTH=524>Do syntax checking only.</TD></TR>

<TR VALIGN=TOP><TD WIDTH=66><CENTER><TT><FONT FACE="Courier">-d</FONT></TT></CENTER>

</TD><TD WIDTH=524>Start the debugger.</TD></TR>

<TR VALIGN=TOP><TD WIDTH=66><CENTER><TT><FONT FACE="Courier">-e</FONT></TT></CENTER>

</TD><TD WIDTH=524>Execute a program from the command line.</TD>

</TR>

<TR VALIGN=TOP><TD WIDTH=66><CENTER><TT><FONT FACE="Courier">-i</FONT></TT></CENTER>

</TD><TD WIDTH=524>Insert line back into the input file.</TD>

</TR>

<TR VALIGN=TOP><TD WIDTH=66><CENTER><TT><FONT FACE="Courier">-I</FONT></TT></CENTER>

</TD><TD WIDTH=524>Specify the paths to search for included files.

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=66><CENTER><TT><FONT FACE="Courier">-p</FONT></TT></CENTER>

</TD><TD WIDTH=524>Echo each line of input.</TD></TR>

<TR VALIGN=TOP><TD WIDTH=66><CENTER><TT><FONT FACE="Courier">-P</FONT></TT></CENTER>

</TD><TD WIDTH=524>Use the C preprocessor.</TD></TR>

<TR VALIGN=TOP><TD WIDTH=66><CENTER><TT><FONT FACE="Courier">-s</FONT></TT></CENTER>

</TD><TD WIDTH=524>Parse very basic command-line switches to the program.

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=66><CENTER><TT><FONT FACE="Courier">-T</FONT></TT></CENTER>

</TD><TD WIDTH=524>Used for writing secure programs. Using this option forces data obtained from outside the program to not be used in any command that affects your file system. This feature lets you write secure programs for system administration tasks. 

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=66><CENTER><TT><FONT FACE="Courier">-u</FONT></TT></CENTER>

</TD><TD WIDTH=524>Generate a core dump.</TD></TR>

<TR VALIGN=TOP><TD WIDTH=66><CENTER><TT><FONT FACE="Courier">-U</FONT></TT></CENTER>

</TD><TD WIDTH=524>Run in unprotected mode (full access to file system).

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=66><CENTER><TT><FONT FACE="Courier">-v</FONT></TT></CENTER>

</TD><TD WIDTH=524>Print the version number.</TD></TR>

<TR VALIGN=TOP><TD WIDTH=66><CENTER><TT><FONT FACE="Courier">-w</FONT></TT></CENTER>

</TD><TD WIDTH=524>Print warning labels.</TD></TR>

<TR VALIGN=TOP><TD WIDTH=66><CENTER><TT><FONT FACE="Courier">-W</FONT></TT></CENTER>

</TD><TD WIDTH=524>Print warnings.</TD></TR>

</TABLE></CENTER>

<P>

<P>

The following sections cover each option in more detail. The options

are presented in the order in which they are most likely to be

found in Perl scripts rather than in alphabetical order. 

<H3><A NAME="ThecandwSyntaxCheckingandWarnin">The <TT><FONT SIZE=4 FACE="Courier">-c</FONT></TT><FONT SIZE=4>

and </FONT><TT><FONT SIZE=4 FACE="Courier">-w</FONT></TT><FONT SIZE=4>

Syntax Checking and Warning Options</FONT></A></H3>

<P>

The <TT><FONT FACE="Courier">-c</FONT></TT> option asks the Perl

interpreter to check the syntax of your Perl program without actually

running it. All other options except for <TT><FONT FACE="Courier">-v</FONT></TT>

and <TT><FONT FACE="Courier">-w</FONT></TT> are ignored by the

Perl interpreter when it sees the <TT><FONT FACE="Courier">-c</FONT></TT>

option. The <TT><FONT FACE="Courier">-w</FONT></TT> option prints

warnings instead of errors. An error will certainly crash your

program. A warning is issued when attempting to parse an ambiguous

operation. Both the <TT><FONT FACE="Courier">-c</FONT></TT> and

the <TT><FONT FACE="Courier">-w</FONT></TT> options can be used

together with the flag, like this: <TT><FONT FACE="Courier">-cw</FONT></TT>.

<P>

If the program you provide is syntactically correct, the Perl

interpreter will print the message

<BLOCKQUOTE>

<TT><I><FONT FACE="Courier">filename</FONT></I><FONT FACE="Courier">

syntax OK</FONT></TT>

</BLOCKQUOTE>

<P>

where <TT><I><FONT FACE="Courier">filename</FONT></I></TT> is

the name of your program. If any errors are detected, you'll see

the following message where <TT><I><FONT FACE="Courier">filename</FONT></I></TT>

is the name of your program: 

<BLOCKQUOTE>

<TT><I><FONT FACE="Courier">filename</FONT></I><FONT FACE="Courier">

had compilation errors</FONT></TT>

</BLOCKQUOTE>

<P>

The <TT><FONT FACE="Courier">-w</FONT></TT> option prints a warning

every time the Perl interpreter sees something that might cause

a problem. Here are some of the potential problems:  

<UL>

<LI><FONT COLOR=#000000>Having more than one subroutine with the

same name. Both functions will be called, and the program won't

crash. Use the </FONT><TT><FONT FACE="Courier">-w</FONT></TT>

option to warn about this problem.

<LI><FONT COLOR=#000000>Using the value of a variable that has

not been defined.</FONT>

<LI><FONT COLOR=#000000>Using the </FONT><TT><FONT FACE="Courier">==</FONT></TT>

operator to compare strings instead of <TT><FONT FACE="Courier">eq</FONT></TT>

operators.

</UL>

<P>

<CENTER>

<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>

<TR VALIGN=TOP><TD ><B>Note</B></TD></TR>

<TR VALIGN=TOP><TD >

<BLOCKQUOTE>

A number is converted to a string when compared with a string using the <TT><FONT FACE="Courier">eq</FONT></TT> operator. However, a string when used with the <TT><FONT FACE="Courier">==</FONT></TT> operator is always converted to the numeric value of 

<TT><FONT FACE="Courier">0</FONT></TT>.

</BLOCKQUOTE>



</TD></TR>

</TABLE></CENTER>

<P>

<H3><A NAME="TheeOptionExecutingaSinglelineP">The <TT><FONT SIZE=4 FACE="Courier">-e</FONT></TT><FONT SIZE=4>

Option: Executing a Single-line Program</FONT></A></H3>

<P>

⌨️ 快捷键说明

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