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

📄 ch16.htm

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

will let the named <TT><FONT FACE="Courier">program</FONT></TT>

use the bell character (7 octal) as the end-of-line character

when it reads the input files that have a <TT><FONT FACE="Courier">.bin</FONT></TT>

extension. 

<P>

For example, the following header comment line will set the end-of-line

character to a space, (octal 40): 

<BLOCKQUOTE>

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

</BLOCKQUOTE>

<P>

To read one paragraph at a time, specify <TT><FONT FACE="Courier">00</FONT></TT>

as the input to the <TT><FONT FACE="Courier">-0</FONT></TT> option.

This will let the Perl interpreter read input until it sees two

newlines together, and thus you will be able to read in one paragraph

at a time. If you do not specify a value with the <TT><FONT FACE="Courier">-0</FONT></TT>

option, the Perl interpreter assumes the null character (ASCII

0).

<H3><A NAME="UsingthelOption">Using the <TT><FONT SIZE=4 FACE="Courier">-l</FONT></TT><FONT SIZE=4>

Option</FONT></A><FONT SIZE=4> </FONT></H3>

<P>

The <TT><FONT FACE="Courier">-l</FONT></TT> option lets you use

a new output end-of-line character for printing statements. Like

the <TT><FONT FACE="Courier">-0</FONT></TT> option, the <TT><FONT FACE="Courier">-l</FONT></TT>

option takes an octal number instead of an ASCII character for

use in place of the newline. This is a one, not the letter &quot;el.&quot;

When the <TT><FONT FACE="Courier">-l</FONT></TT> option is specified,

the Perl interpreter always replaces the end-of-line character

in print statements with the newer version. Also, in the case

of <TT><FONT FACE="Courier">-n</FONT></TT> or <TT><FONT FACE="Courier">-p</FONT></TT>

options, the end-of-line character is removed after reading the

input. 

<P>

The Perl interpreter uses the character specified by the <TT><FONT FACE="Courier">-0</FONT></TT>

option, if it is defined, in case you do not specify the <TT><FONT FACE="Courier">-I</FONT></TT>

option. If <TT><FONT FACE="Courier">-0</FONT></TT> also has not

been specified, the end-of-line character is set to the newline

character.

<P>

When using both the <TT><FONT FACE="Courier">-l</FONT></TT> and

the <TT><FONT FACE="Courier">-0</FONT></TT> option, specify the

<TT><FONT FACE="Courier">-1</FONT></TT> option first, then <TT><FONT FACE="Courier">-0</FONT></TT>

option. Recall that options are processed from left to right.

If the <TT><FONT FACE="Courier">-l</FONT></TT> option appears

first, the output end-of-line character is set to the newline

character. If the <TT><FONT FACE="Courier">-0</FONT></TT> option

appears first, the output end-of-line character (set by <TT><FONT FACE="Courier">-l</FONT></TT>)

becomes the same as the input end-of-line character (set by <TT><FONT FACE="Courier">-0</FONT></TT>).

<P>

<CENTER>

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

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

<TR VALIGN=TOP><TD >

<BLOCKQUOTE>

It's probably easier to control the input and output end-of-line characters also by using the system variables <TT><FONT FACE="Courier">$/</FONT></TT> and <TT><FONT FACE="Courier">$\</FONT></TT>, respectively. 

</BLOCKQUOTE>



</TD></TR>

</TABLE></CENTER>

<P>

<H3><A NAME="UsingthexOptiontoGetaPerlProgra">Using the <TT><FONT SIZE=4 FACE="Courier">-x</FONT></TT><FONT SIZE=4>

Option to Get a Perl Program from Another File</FONT></A></H3>

<P>

The <TT><FONT FACE="Courier">-x</FONT></TT> option enables you

to process a Perl program that appears in the middle of a file.

When the <TT><FONT FACE="Courier">-x</FONT></TT> option is specified,

the Perl interpreter ignores every line in the program until it

sees a header comment. The Perl interpreter then processes the

program as usual until the bottom of the program file is reached

or the <TT><FONT FACE="Courier">__END__</FONT></TT> statement

is reached. Everything after the <TT><FONT FACE="Courier">__END__</FONT></TT>

statement is ignored by the Perl interpreter. 

<H3><A NAME="UsingtheSOption">Using the <TT><FONT SIZE=4 FACE="Courier">-S</FONT></TT><FONT SIZE=4>

Option</FONT></A></H3>

<P>

You need to use <TT><FONT FACE="Courier">-S</FONT></TT> only if

you run your Perl program using the <TT><FONT FACE="Courier">Perl

</FONT></TT>command. If you run a program directly using a script,

the <TT><FONT FACE="Courier">-S</FONT></TT> option is meaningless

because the shell will hunt for your program in the directories

specified in your <TT><FONT FACE="Courier">PATH</FONT></TT> environment

variable. The <TT><FONT FACE="Courier">-S</FONT></TT> option simply

tells the Perl interpreter that your program might be contained

in any of the directories specified by your <TT><FONT FACE="Courier">PATH</FONT></TT>

environment variable. 

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

Option: Printing the Perl Version Number</FONT></A></H3>

<P>

You might be curious as to which version of Perl you are running.

The <TT><FONT FACE="Courier">-v</FONT></TT> option prints a string

with the version information for the Perl interpreter you are

running. The Perl interpreter will not run any scripts, nor will

it honor any other options when this <TT><FONT FACE="Courier">-v</FONT></TT>

option is specified. Here is sample output from the <TT><FONT FACE="Courier">-v</FONT></TT>

command:

<BLOCKQUOTE>

<TT><FONT FACE="Courier">$ <B>perl -v<BR>

</B>This is perl, version 5.002<BR>

<BR>

Copyright 1987-1996, Larry Wall<BR>

<BR>

Perl may be copied only under the terms of either the Artistic

License or the<BR>

GNU General Public License, which may be found in the Perl 5.0

source kit.</FONT></TT>

</BLOCKQUOTE>

<P>

Now that you've learned the command-line options for the Perl

interpreter, you're ready to learn how to process input in your

Perl applications.

<H3><A NAME="UsingConditionalCodewiththeCPrepro">Using Conditional

Code with the C Preprocessor</A></H3>

<P>

The C preprocessor also provided five statements, <TT><FONT FACE="Courier">#ifdef</FONT></TT>,

<TT><FONT FACE="Courier">#ifndef</FONT></TT>,<TT><FONT FACE="Courier">

#if</FONT></TT>, <TT><FONT FACE="Courier">#else</FONT></TT>, and

<TT><FONT FACE="Courier">#endif</FONT></TT>, for conditional statements

to include or exclude parts of your Perl program. The syntax for

the <TT><FONT FACE="Courier">#ifdef</FONT></TT> and <TT><FONT FACE="Courier">#endif</FONT></TT>

statements is

<BLOCKQUOTE>

<TT><FONT FACE="Courier">#ifdef <I>cond<BR>

<I>...code if cond is defined...<BR>

</I></I>#else <BR>

<I>...code if cond is NOT defined...<BR>

</I>#endif</FONT></TT>

</BLOCKQUOTE>

<P>

The <TT><I><FONT FACE="Courier">cond</FONT></I></TT> is a character

string that can be used in a <TT><FONT FACE="Courier">#define</FONT></TT>

statement. If the character string has been defined to a value,

the first set of code (above the <TT><FONT FACE="Courier">#else</FONT></TT>

clause) is inserted in your program; otherwise, the second part

of code (after the <TT><FONT FACE="Courier">#else</FONT></TT>

and before the <TT><FONT FACE="Courier">#endif</FONT></TT> clause)

is inserted in your program. Because the <TT><FONT FACE="Courier">#else</FONT></TT>

clause is optional, you can also have statements of the form

<BLOCKQUOTE>

<TT><FONT FACE="Courier">#ifdef <I>cond<BR>

<I>...code if cond is defined...<BR>

</I></I>#endif</FONT></TT>

</BLOCKQUOTE>

<P>

The <TT><FONT FACE="Courier">#ifndef</FONT></TT> lets you define

code that is to be executed when a particular string is not defined.

Thus, <TT><FONT FACE="Courier">#ifndef</FONT></TT> takes the opposite

action of the <TT><FONT FACE="Courier">#ifdef</FONT></TT> statement.

For example:

<BLOCKQUOTE>

<TT><FONT FACE="Courier">#ifdef SOMBER<BR>

print (&quot;Hello, Cruel world!\n&quot;);<BR>

#else<BR>

print (&quot;Hello, Beautiful world!\n&quot;);<BR>

#endif</FONT></TT>

</BLOCKQUOTE>

<P>

This code prints a sad message (<TT><FONT FACE="Courier">Hello,

Cruel world!</FONT></TT>) if <TT><FONT FACE="Courier">SOMBER</FONT></TT>

was defined earlier, or a happy message (<TT><FONT FACE="Courier">Hello,

Beautiful world!</FONT></TT>) if <TT><FONT FACE="Courier">SOMBER</FONT></TT>

was not defined earlier. 

<P>

Code enclosed by <TT><FONT FACE="Courier">#ifdef</FONT></TT> and

<TT><FONT FACE="Courier">#endif</FONT></TT> does not have to be

a complete Perl statement. For example, the following code will

set the value of <TT><FONT FACE="Courier">$result</FONT></TT>

to different settings based on the whether or not <TT><FONT FACE="Courier">METRIC</FONT></TT>

was defined:

<BLOCKQUOTE>

<TT><FONT FACE="Courier">$area =&nbsp;&nbsp;$radius * PI * 2<BR>

#ifdef METRIC<BR>

* 2.54<BR>

#endif<BR>

;</FONT></TT>

</BLOCKQUOTE>

<P>

Here, <TT><FONT FACE="Courier">$result</FONT></TT> is assigned

a value in centimeters if <TT><FONT FACE="Courier">METRIC</FONT></TT>

is defined or in inches if it's not.<P>

<CENTER>

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

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

<TR VALIGN=TOP><TD >

<BLOCKQUOTE>

Don't overuse the C preprocessor because it might make your program hard to read, especially by people who are not familiar with the C programming language.</BLOCKQUOTE>



</TD></TR>

</TABLE></CENTER>

<P>

<P>

The <TT><FONT FACE="Courier">#if</FONT></TT> statement in the

C preprocessor is similar to the <TT><FONT FACE="Courier">#ifdef</FONT></TT>

statement. The <TT><FONT FACE="Courier">#if</FONT></TT> statement

uses the value of a variable, whereas the <TT><FONT FACE="Courier">#ifdef</FONT></TT>

statement simply checks to see whether a variable is defined.

The syntax for the <TT><FONT FACE="Courier">#if</FONT></TT> statement

is as follows:

<BLOCKQUOTE>

<TT><FONT FACE="Courier">#if <I>expr<BR>

<I>...code...<BR>

</I></I>#endif</FONT></TT>

</BLOCKQUOTE>

<P>

The <TT><I><FONT FACE="Courier">expr</FONT></I></TT> is the expression

that is evaluated by the C preprocessor, and <TT><I><FONT FACE="Courier">code</FONT></I></TT>

is the code to be executed if <TT><I><FONT FACE="Courier">expr</FONT></I></TT>

is nonzero. For example, the following statements will set the

value of <TT><FONT FACE="Courier">$result</FONT></TT> to <TT><FONT FACE="Courier">&quot;hello&quot;</FONT></TT>

if the sum of <TT><FONT FACE="Courier">S1</FONT></TT> and <TT><FONT FACE="Courier">S2</FONT></TT>

is nonzero:

<BLOCKQUOTE>

<TT><FONT FACE="Courier">#if S1 + S2<BR>

$result = &quot;hello&quot;;<BR>

#endif</FONT></TT>

</BLOCKQUOTE>

<P>

If you want to set the value of <TT><FONT FACE="Courier">$result</FONT></TT>

if either <TT><FONT FACE="Courier">S1</FONT></TT> or <TT><FONT FACE="Courier">S2</FONT></TT>

is set to a nonzero value, you can use the following statement:

<BLOCKQUOTE>

<TT><FONT FACE="Courier">#if S1 || S2<BR>

$result = &quot;hello&quot;;<BR>

#endif</FONT></TT>

</BLOCKQUOTE>

<P>

By specifying <TT><FONT FACE="Courier">0</FONT></TT> to the <TT><FONT FACE="Courier">#if</FONT></TT>

statement, you can easily prevent lines of code from being interpreted

without having to put a hash (<TT><FONT FACE="Courier">#</FONT></TT>)

in front of each line:

<BLOCKQUOTE>

<TT><FONT FACE="Courier">#if 0<BR>

$result = &quot;hello&quot;;<BR>

print (&quot;I will not be printed if the -P option is used.\n&quot;);

<BR>

#endif</FONT></TT>

</BLOCKQUOTE>

<P>

You can also use <TT><FONT FACE="Courier">#else</FONT></TT> with

the <TT><FONT FACE="Courier">#if</FONT></TT> operator:

<BLOCKQUOTE>

<TT><FONT FACE="Courier">#if S1 || S2<BR>

$result = &quot;hello&quot;;<BR>

#else<BR>

$result = &quot;goodbye&quot;; <BR>

#endif</FONT></TT>

</BLOCKQUOTE>

<P>

In this case, the value of <TT><FONT FACE="Courier">$result</FONT></TT>

will be <TT><FONT FACE="Courier">&quot;hello&quot;</FONT></TT>

if either <TT><FONT FACE="Courier">S1</FONT></TT> or <TT><FONT FACE="Courier">S2</FONT></TT>

has a nonzero value; otherwise, the value will be <TT><FONT FACE="Courier">&quot;goodbye&quot;</FONT></TT>.

<P>

<CENTER>

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

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

<TR VALIGN=TOP><TD >

<BLOCKQUOTE>

The C preprocessor does not support the exponent operator, so you cannot evaluate <BR>

<TT><FONT FACE="Courier">(x ** y)</FONT></TT> with the <TT><FONT FACE="Courier">#if</FONT></TT> statement.

</BLOCKQUOTE>



</TD></TR>

</TABLE></CENTER>

<P>

<P>

You can embed <TT><FONT FACE="Courier">#ifdef</FONT></TT>/<TT><FONT FACE="Courier">#else</FONT></TT>/<TT><FONT FACE="Courier">#endif</FONT></TT>

constructs inside one another. Just make sure that you match all

the <TT><FONT FACE="Courier">#ifdef</FONT></TT> and <TT><FONT FACE="Courier">#endif</FONT></TT>

statements so that there is one <TT><FONT FACE="Courier">#endif</FONT></TT>

for each <TT><FONT FACE="Courier">#ifdef</FONT></TT> and <TT><FONT FACE="Courier">#ifndef</FONT></TT>

statement. For example, here is a snippet of code that illustrates

⌨️ 快捷键说明

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