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

📄 ch16.htm

📁 《Perl 5 Unreleased》
💻 HTM
📖 第 1 页 / 共 5 页
字号:
You can execute Perl statements at the command line with the <TT><FONT FACE="Courier">-e</FONT></TT>

option. Here is an example of a command that prints a string:

<BLOCKQUOTE>

<TT><FONT FACE="Courier">$ <B>perl -e 'print (&quot;Kamran Wuz

Here\n&quot;);'<BR>

</B>Kamran Wuz Here</FONT></TT>

</BLOCKQUOTE>

<P>

Don't forget to type the semicolon (<TT><FONT FACE="Courier">;</FONT></TT>)

at the end of each statement. You can specify more than one statement

by using either semicolons to separate them or using multiple

<TT><FONT FACE="Courier">-e</FONT></TT> options. For example,

the following two statements both print the string <TT><FONT FACE="Courier">Howdy

folks</FONT></TT>:

<BLOCKQUOTE>

<TT><FONT FACE="Courier">$ <B>perl -e 'print (&quot;Howdy\n&quot;);'

-e 'print (&quot; folks\n&quot;);'<BR>

</B>Howdy folks<BR>

$ <B>perl -e 'print (&quot;Howdy\n&quot;); print (&quot; folks\n&quot;);'

<BR>

</B>Howdy folks</FONT></TT>

</BLOCKQUOTE>

<P>

In the case of multiple <TT><FONT FACE="Courier">-e</FONT></TT>

options, the Perl interpreter executes them from left to right.

Here's an example:

<BLOCKQUOTE>

<TT><FONT FACE="Courier">$ perl -e 'print (&quot;Donald&quot;);'

-e 'print (&quot; Duck&quot;);'<BR>

Donald Duck</FONT></TT>

</BLOCKQUOTE>

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

Option to Supply Custom Command-line Options</FONT></A></H3>

<P>

Generally, you'll specify the command line in a Perl script with

execute permissions in the first line of a script file, as follows:

<BLOCKQUOTE>

<TT><FONT FACE="Courier">#!/usr/bin/perl </FONT></TT>

</BLOCKQUOTE>

<P>

The first line is the complete pathname to the Perl interpreter.

<P>

You can run the same script using the following command at the

command line, as follows: 

<BLOCKQUOTE>

<TT><FONT FACE="Courier">perl <I>scriptFile</I></FONT></TT>

</BLOCKQUOTE>

<P>

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

the name of the script file. Any command-line options specified

before the script file's name will be passed to the Perl interpreter

and not to your script file. 

<P>

To pass options to the script that you run, you have to use the

<TT><FONT FACE="Courier">-s</FONT></TT> option. 

<BLOCKQUOTE>

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

</BLOCKQUOTE>

<P>

This command starts the Perl program <TT><FONT FACE="Courier">scriptFile</FONT></TT>

and passes it the <TT><FONT FACE="Courier">-w </FONT></TT>option.

If you do not specify the <TT><FONT FACE="Courier">-s</FONT></TT>

option, your <TT><FONT FACE="Courier">-w</FONT></TT> will be sent

as part of the <TT><FONT FACE="Courier">@ARGV</FONT></TT> array

to the program being run. For programs that are run from the command

line with the Perl command, it's best to include <TT><FONT FACE="Courier">-s</FONT></TT>

as part of your header comment:

<BLOCKQUOTE>

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

</BLOCKQUOTE>

<P>

This way you are guaranteed that the program always will check

for options provided that no other Perl options were specified

on the command line when you invoked the program.

<P>

A scalar variable with the same name as the name of any specified

option is created and automatically set to <TT><FONT FACE="Courier">1</FONT></TT>

before the Perl interpreter executes a program. For example, if

a Perl program named <TT><FONT FACE="Courier">scriptFile</FONT></TT><I>

</I>is called with the <TT><FONT FACE="Courier">-x</FONT></TT>

option, as in

<BLOCKQUOTE>

<TT><FONT FACE="Courier">perl -s scriptFile -x</FONT></TT>

</BLOCKQUOTE>

<P>

the scalar variable <TT><FONT FACE="Courier">$x </FONT></TT>is

automatically set to <TT><FONT FACE="Courier">1</FONT></TT>. This

lets you test the <TT><FONT FACE="Courier">$x</FONT></TT> variable

in a conditional expression to see whether the option has been

set. The named variable will not appear in the <TT><FONT FACE="Courier">@ARGV</FONT></TT>

array. Options do not have to be a single character. For example,

the following command sets the value of the scalar variable <TT><FONT FACE="Courier">$surge</FONT></TT>

to <TT><FONT FACE="Courier">1</FONT></TT>:

<BLOCKQUOTE>

<TT><FONT FACE="Courier">perl -s scriptFile -surge</FONT></TT>

</BLOCKQUOTE>

<P>

Options can be set to a value other than <TT><FONT FACE="Courier">1</FONT></TT>

by simply assigning a value at the command line. For example:

<BLOCKQUOTE>

<TT><FONT FACE="Courier">perl -s scriptFile -surge=&quot;power&quot;</FONT></TT>

</BLOCKQUOTE>

<P>

This command sets the value of <TT><FONT FACE="Courier">$surge</FONT></TT>

to <TT><FONT FACE="Courier">power</FONT></TT> in the program specified

in <TT><FONT FACE="Courier">scriptFile</FONT></TT>.

<P>

The <TT><FONT FACE="Courier">-s</FONT></TT> option lets you supply

both options and command-line arguments based on these rules:

<UL>

<LI><FONT COLOR=#000000>All arguments that start with a dash (</FONT><TT><FONT FACE="Courier">-</FONT></TT>)

and immediately follow the program name are assumed to be options.

<LI><FONT COLOR=#000000>Any argument not starting with a dash

(</FONT><TT><FONT FACE="Courier">-</FONT></TT>) is assumed to

be an ordinary argument. All subsequent arguments, even if they

start with a <TT><FONT FACE="Courier">-</FONT></TT> , are then

assumed to be ordinary arguments and not options.

<LI><FONT COLOR=#000000>A double dash (</FONT><TT><FONT FACE="Courier">--</FONT></TT>)

will end Perl's parsing of command-line switches.

</UL>

<P>

This means, for example, that the command

<BLOCKQUOTE>

<TT><FONT FACE="Courier">perl -s scriptFile -arg1 -arg2 -arg3</FONT></TT>

</BLOCKQUOTE>

<P>

treats <TT><FONT FACE="Courier">-arg1</FONT></TT> as an option

to <TT><FONT FACE="Courier">scriptFile</FONT></TT>, and <TT><FONT FACE="Courier">-arg2</FONT></TT>

and <TT><FONT FACE="Courier">-arg3</FONT></TT> are ordinary arguments

that are placed in <TT><FONT FACE="Courier">@ARGV</FONT></TT>.

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

Option to Include Other Files</FONT></A></H3>

<P>

The <TT><FONT FACE="Courier">-I</FONT></TT> option is used with

the <TT><FONT FACE="Courier">-P</FONT></TT> option (which is described

in the next section). The <TT><FONT FACE="Courier">-I</FONT></TT>

option lets you specify the pathnames to search for include files

to be processed by the C preprocessor. For example:

<BLOCKQUOTE>

<TT><FONT FACE="Courier">perl -P -I /usr/local/other <I>scriptFile</I></FONT></TT>

</BLOCKQUOTE>

<P>

This command tells the Perl interpreter to search the directory

<TT><FONT FACE="Courier">/usr/local/other</FONT></TT> for include

files if the file is not found in its default paths. The default

path is the current directory and, if the file is not found, the

<TT><FONT FACE="Courier">/usr/local/lib/perl</FONT></TT> directory.

The <TT><FONT FACE="Courier">-I</FONT></TT> option can be repeated

on the same command line to specify more than one include-file

path. 

<P>

Using the <TT><FONT FACE="Courier">-I</FONT></TT> option also

adds the path or paths to the <TT><FONT FACE="Courier">@Inc</FONT></TT>

variable. The paths are then made available to the Perl interpreter

when it uses the <TT><FONT FACE="Courier">require</FONT></TT>

function to find its modules. 

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

Option for Using the C Preprocessor</FONT></A></H3>

<P>

The <TT><FONT FACE="Courier">-P</FONT></TT> option is helpful

only if you have a C compiler on your system. Although all UNIX

systems come with a C compiler, DOS and Windows NT systems don't;

you have to purchase your own. The <TT><FONT FACE="Courier">cpp</FONT></TT>

preprocessor is the default C preprocessor in UNIX. The C preprocessor

is a program that takes code written in C which does basic string

substitution based on the values of variables. To enable the use

of <TT><FONT FACE="Courier">cpp</FONT></TT> with the <TT><FONT FACE="Courier">-P</FONT></TT>

option, use the following statement to start your Perl program:

<BLOCKQUOTE>

<TT><FONT FACE="Courier">perl -P <I>scriptFile</I></FONT></TT>

</BLOCKQUOTE>

<P>

The Perl program <TT><I><FONT FACE="Courier">scriptFile</FONT></I></TT>

is first run through the C preprocessor, and then the resulting

output is executed by the Perl interpreter. You can also specify

the use of the C preprocessor in the header comment like this:

<BLOCKQUOTE>

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

</BLOCKQUOTE>

<P>

All C preprocessor statements have the following syntax:

<BLOCKQUOTE>

<TT><FONT FACE="Courier">#<I>command</I> <I>value</I></FONT></TT>

</BLOCKQUOTE>

<P>

The hash (<TT><FONT FACE="Courier">#</FONT></TT>) is interpreted

by Perl as a comment, and so any statements intended for the C

preprocessor are ignored by Perl even if the <TT><FONT FACE="Courier">-P</FONT></TT>

option is not used. The <TT><I><FONT FACE="Courier">command</FONT></I></TT>

is the preprocessor operation to perform, and <TT><I><FONT FACE="Courier">value</FONT></I></TT>,

which is optional, is associated with this operation.

<P>

The <TT><FONT FACE="Courier">#define</FONT></TT> operator is the

most common preprocessor statement. It tells the preprocessor

to replace every occurrence of a particular character string with

a specified value. The syntax for <TT><FONT FACE="Courier">#define</FONT></TT>

is

<BLOCKQUOTE>

<TT><FONT FACE="Courier">#define<I> item value</I></FONT></TT>

</BLOCKQUOTE>

<P>

This statement replaces all occurrences of the character string

<TT><I><FONT FACE="Courier">item</FONT></I></TT> with <TT><I><FONT FACE="Courier">value</FONT></I></TT>.

This substitution operation is known as <I>macro substitution</I>.

The item being substituted can contain a combination of letters,

digits, and underscores. The value specified in a <TT><FONT FACE="Courier">#define</FONT></TT>

statement can be any character string or number. For example,

the following statement will replace all occurrences of <TT><FONT FACE="Courier">DOCTOR</FONT></TT>

with <TT><FONT FACE="Courier">quack</FONT></TT>, and <TT><FONT FACE="Courier">Donald</FONT></TT>

with <TT><FONT FACE="Courier">&quot;Duck&quot;</FONT></TT> including

the quotation marks:

<BLOCKQUOTE>

<TT><FONT FACE="Courier">#define DOCTOR  QUACK<BR>

#define Donald &quot;Duck&quot;</FONT></TT>

</BLOCKQUOTE>

<P>

Any expressions are treated as strings. For example, the following

statement: 

<BLOCKQUOTE>

<TT><FONT FACE="Courier">#define AREA  (3.141 * 2)</FONT></TT>

</BLOCKQUOTE>

<P>

replaces <TT><FONT FACE="Courier">AREA</FONT></TT> with the string

<TT><FONT FACE="Courier">(3.141 * 2)</FONT></TT>, including the

parentheses and not the value of 6.282. 

<P>

When using <TT><FONT FACE="Courier">#define</FONT></TT> with expressions,

don't forget to enclose the value in parentheses. For example,

consider the following Perl statement:

<BLOCKQUOTE>

<TT><FONT FACE="Courier">$result = ITEM * 10;</FONT></TT>

</BLOCKQUOTE>

<P>

If the statement you use in your preprocessor command is this:

<BLOCKQUOTE>

<TT><FONT FACE="Courier">#define ITEM  1 + 2</FONT></TT>

</BLOCKQUOTE>

<P>

the resulting Perl statement is this:

<BLOCKQUOTE>

<TT><FONT FACE="Courier">$result = 1 + 2 * 10;</FONT></TT>

</BLOCKQUOTE>

<P>

This statement assigns 21 to <TT><FONT FACE="Courier">$result</FONT></TT>

instead of 30, which would be the result if you used this expression

instead:

<BLOCKQUOTE>

<TT><FONT FACE="Courier">#define ITEM  (1 + 2)</FONT></TT>

</BLOCKQUOTE>

<P>

to get this statement as a result:

<BLOCKQUOTE>

<TT><FONT FACE="Courier">$result = (1 + 2) * 10;</FONT></TT>

</BLOCKQUOTE>

<P>

You can even specify multiple parameters with a <TT><FONT FACE="Courier">#define</FONT></TT>

statement thus enabling you to use a preprocessor command like

a simple function that also accepts arguments. For example, this

preprocessor statement:

<BLOCKQUOTE>

<TT><FONT FACE="Courier">#define SQUARE(val)  ((val) * (val))</FONT></TT>

</BLOCKQUOTE>

<P>

will return the square of a passed value. This statement:

<BLOCKQUOTE>

<TT><FONT FACE="Courier">$result = SQUARE(4) </FONT></TT>

</BLOCKQUOTE>

<P>

will generate the following statement: 

<BLOCKQUOTE>

<TT><FONT FACE="Courier">$result = ((4) * (4));</FONT></TT>

</BLOCKQUOTE>

⌨️ 快捷键说明

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