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

📄 ch2.htm

📁 美国Macmillan出版社编写的Perl教程《Perl CGI Web Pages for WINNT》
💻 HTM
📖 第 1 页 / 共 4 页
字号:
<BLOCKQUOTE>

<PRE>

&quot;The Dirty&quot; . (2*6)

</PRE>

</BLOCKQUOTE>

<P>

which will give you this string result:

<BLOCKQUOTE>

<PRE>

&quot;The Dirty12&quot;

</PRE>

</BLOCKQUOTE>

<P>

Remember, before Perl processes the operand outside the parentheses,

it processes the operand inside the parentheses.

<H4>Numeric Operators</H4>

<P>

Operators for numbers are pretty much what you might expect. One

odd one is from the C programming language and it's called a modulus.

It is the &quot;%&quot; symbol and it divides two numbers by their

integer value, not their actual value, and then takes the remainder

as the new value. For example,

<BLOCKQUOTE>

<PRE>

25.3 % 4.4378742 

</PRE>

</BLOCKQUOTE>

<P>

is first converted to

<BLOCKQUOTE>

<PRE>

25 % 4 

</PRE>

</BLOCKQUOTE>

<P>

and then the new value is 1, which is what remains after 4 divides

into 25. A&nbsp;full listing of numeric operators appears in Table

2.2.<BR>

<P>

<CENTER><B>Table 2.2 Numeric Operators</B></CENTER>

<P>

<CENTER>

<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=60% CELLPADDING=3>

<TR VALIGN=TOP><TD><CENTER><B>Operator</B></CENTER></TD><TD><B>Action</B>

</TD><TD><B>Example</B></TD></TR>

<TR VALIGN=TOP><TD><CENTER>+</CENTER></TD><TD>Addition

</TD><TD>1+2, or 3</TD></TR>

<TR VALIGN=TOP><TD><CENTER>-</CENTER></TD><TD>Subtraction

</TD><TD>1-2, or -1</TD></TR>

<TR VALIGN=TOP><TD><CENTER>*</CENTER></TD><TD>Multiplication

</TD><TD>2*2, or 4</TD></TR>

<TR VALIGN=TOP><TD><CENTER>/</CENTER></TD><TD>Division

</TD><TD>2/2, or 1</TD></TR>

<TR VALIGN=TOP><TD><CENTER>**</CENTER></TD><TD>Exponentiation

</TD><TD>2**3, or 8</TD></TR>

<TR VALIGN=TOP><TD><CENTER>%</CENTER></TD><TD>Modulus

</TD><TD>2.3%3.2, or 0</TD></TR>

<TR VALIGN=TOP><TD><CENTER>&lt;</CENTER></TD><TD>Less than

</TD><TD>2&lt;3</TD></TR>

<TR VALIGN=TOP><TD><CENTER>&lt;=</CENTER></TD><TD>Less than or equal to

</TD><TD>2&lt;=3</TD></TR>

<TR VALIGN=TOP><TD><CENTER>==</CENTER></TD><TD>Equal to

</TD><TD>2==4/2</TD></TR>

<TR VALIGN=TOP><TD><CENTER>&gt;=</CENTER></TD><TD>Greater than or equal to

</TD><TD>3&gt;=2</TD></TR>

<TR VALIGN=TOP><TD><CENTER>&gt;</CENTER></TD><TD>Greater than

</TD><TD>5&gt;1</TD></TR>

<TR VALIGN=TOP><TD><CENTER>!=</CENTER></TD><TD>Not equal to

</TD><TD>5!=1</TD></TR>

</TABLE></CENTER>

<P>

<P>

When these operators are used, Perl makes a comparison and then

returns a true or false value. Operators for strings are a little

different.

<H4>String Operators</H4>

<P>

These operators work the same way as the numeric ones, and are

represented by characters. The concatenate operator used previously

can have these different results:

<UL>

<LI>&quot;Hey,&quot; . &quot;now!&quot; produces &quot;Hey, now!&quot;

<LI>&quot;Hey, now!&quot; . &quot;\n&quot; produces &quot;Hey,

now!\n&quot;

<LI>&quot;Hey,&quot; . &quot; &quot; . &quot;now!&quot; produces

&quot;Hey, now!&quot;

</UL>

<P>

Other string operators are listed in Table 2.3.<BR>

<P>

<CENTER><B>Table 2.3 String Operators</B></CENTER>

<P>

<CENTER>

<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=60% CELLPADDING=3>

<TR VALIGN=TOP><TD><CENTER><B>Operator</B></CENTER></TD><TD><B>Action</B>

</TD><TD><B>Example</B></TD></TR>

<TR VALIGN=TOP><TD><CENTER>&nbsp;</CENTER></TD><TD>Concatenate

</TD><TD>&quot;bi&quot; . &quot;g&quot;, or &quot;big&quot;

</TD></TR>

<TR VALIGN=TOP><TD><CENTER>eq</CENTER></TD><TD>Equal

</TD><TD>&quot;small&quot; eq &quot;small&quot;</TD>

</TR>

<TR VALIGN=TOP><TD><CENTER>ne</CENTER></TD><TD>Not equal

</TD><TD>&quot;small&quot; ne &quot;tiny&quot;</TD>

</TR>

<TR VALIGN=TOP><TD><CENTER>lt</CENTER></TD><TD>Less than

</TD><TD>&quot;30&quot; lt &quot;7&quot;</TD></TR>

<TR VALIGN=TOP><TD><CENTER>gt</CENTER></TD><TD>Greater than

</TD><TD>&quot;50&quot; gt &quot;300&quot;</TD></TR>

<TR VALIGN=TOP><TD><CENTER>le</CENTER></TD><TD>Less than or equal to

</TD><TD>&quot;ten&quot; le &quot;ten&quot;</TD></TR>

<TR VALIGN=TOP><TD><CENTER>ge</CENTER></TD><TD>Greater than or equal to

</TD><TD>&quot;eleven&quot; ge &quot;eleven&quot;</TD>

</TR>

<TR VALIGN=TOP><TD><CENTER>x</CENTER></TD><TD>String repetition

</TD><TD>&quot;more&quot; x 2, or &quot;moremore&quot;

</TD></TR>

</TABLE></CENTER>

<P>

<P>

You might have noticed something odd in Table 2.3 with the less

than and greater than examples. They seem confusing, but remember,

we're dealing with strings now, not numbers, so Perl takes the

ASCII values of the number characters because it sees them as

strings with these operators. This makes 30 less than 7 because

3 is less than 7 in ASCII.

<P>

It should be noted that, just as Perl will process what's inside

parentheses first, it also gives special precedence and associativity

(paying attention to which side of the operator a value is on)

to each of its operators. For a full list of these relationships

check Appendix A.

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

Scalar Variables</FONT></A></H2>

<P>

While a scalar value may change during the run of a program, the

scalar variable remains the same. The variable is merely the jug

into which the sweet wine of value is poured. A scalar variable

can contain only one value at a time, but that value can change

as the program demands. A scalar variable has this format:

<BLOCKQUOTE>

<PRE>

$the_variable's_name $another_variable

</PRE>

</BLOCKQUOTE>

<P>

where you see that white space ends the variable name. Variables

are case-sensitive, so $JAZZ is different from $jazz, which is

different again from $Jazz. When choosing a variable name it is

highly recommended to pick something that reflects the value or

task that the variable is fufilling in your script to make it

easier for anyone (including yourself) to read the script. 

<P>

Variables work with operators to give them their values. The most

common use you will encounter is something like this:

<BLOCKQUOTE>

<PRE>

$IQ = 13&Oslash;

</PRE>

</BLOCKQUOTE>

<P>

or

<BLOCKQUOTE>

<PRE>

$Weight = $IQ + 2&Oslash;

</PRE>

</BLOCKQUOTE>

<P>

or

<BLOCKQUOTE>

<PRE>

$page = $weight - 17

</PRE>

</BLOCKQUOTE>

<P>

where the different scalar variables are assigned values based

on integers, or other scalar variables. You can also use scalar

variables on both sides of an operator, like so:

<BLOCKQUOTE>

<PRE>

$income = $income - $food

</PRE>

</BLOCKQUOTE>

<P>

where the value of $income is assigned a new value of $income

minus $food. This type of relationship is so common in Perl, as

in other languages, that Perl has developed a shortcut to do this

called a<I> binary assignment operator. </I>Almost all of Perl's

binary assignment operators are made of some modified &quot;=&quot;

symbol. For example, a binary assignment that could be used in

place of an ordinary operator might take $income = $income-$rent

and change it to a binary assignment like so:

<BLOCKQUOTE>

<PRE>

$income -= $rent

</PRE>

</BLOCKQUOTE>

<P>

where the operator &quot;-&quot; is made into a binary assignment

when added to the front of the &quot;=&quot; operator. Binary

is a clever name for these new assignments, considering that two

operators are used to make them. Almost any operator can be used

as a binary assignment in this way.

<P>

Beyond the binary assignment are the autoincrement and autodecrement

operators. These can be used to automatically add or subtract

one to a variable. Autoincrement is signified with the &quot;++&quot;

symbols. It could be used like this:

<BLOCKQUOTE>

<PRE>

$debt += 1, is the same as

++$debt

</PRE>

</BLOCKQUOTE>

<P>

with the value of $debt going up one with every pass. Autodecrement

is very similar, with the &quot;--&quot; symbols being placed

in front of a variable to lower its value by one with each pass.

<P>

You can only use the autoincrement and autodecrement operators

to move your variable up or down one step.

<H3><A NAME="TheChopOperator">

The Chop Operator</A></H3>

<P>

This operator, as has been described before, takes a scalar variable

and chops the last character from it. And what use does this have?

<P>

It all has to do with Perl's unending search for truth. The real

power behind Perl can be found in the way it compares variables,

finds them &quot;true&quot; or &quot;false,&quot; records the

result, and then moves on. One of the catches is that Perl should

treat a blank line like a null sting, which would give it a false

value. Always one step ahead, Perl was made to see a blank line

as a new line, which is given the value &quot;\n,&quot; and this

has a true value. 

<P>

However, you might not want that new line there, so you can use

the chop operator to get rid of it. The default value of chop()

is to chop off the last character. You will see chop used, especially

in relation to the &quot;\n&quot; value, in several of the scripts

in this book.

<H3><A NAME="InterpolationofScalarsintoStrings">

Interpolation of Scalars into Strings</A></H3>

<P>

Interpolation is related to double-quoted strings. When a string

literal, like &quot;Hey, now!&quot; is double-quoted, Perl knows

to look through the string for any scalar variables. When Perl

finds one of these variables, its literal value is put in its

place before it is output. For example:

<BLOCKQUOTE>

<PRE>

$uid = &quot;user_id&quot;;

$new = &quot;new $uid&quot;; # $new has the value 

# &quot;new user_id&quot;

$x = &quot;$new and $data&quot;; # $x only has 

# the value &quot;new user_id and&quot; because

# $data is an undefined variable

</PRE>

</BLOCKQUOTE>

<P>

Interpolation, a fancy word that means &quot;putting a literal

value into a string&quot; in Perl, works on the first pass through

a line only. You can see this in the following example:

<BLOCKQUOTE>

<PRE>

$min = &quot;$time&quot;;

$clock = &quot;$min left to go&quot;;

</PRE>

</BLOCKQUOTE>

<P>

The value of $clock is &quot;$time left to go.&quot; There is

no double substitution.

<P>

If you want to avoid the substitution of a variable with its value,

then you have to either add single quotes to that area of the

string, or put a backslash in front of the &quot;$&quot; symbol

to turn off its special significance. This might look like:

<BLOCKQUOTE>

<PRE>

$x = &quot;The variable used for the user id is &quot;.'$user';

$y = &quot;The variable used for the user id is \$user&quot;;

</PRE>

</BLOCKQUOTE>

<P>

where both of the outputs will be &quot;The variable used for

the user id is $user.&quot;

<P>

You can use interpolation to avoid tripping up the Perl interpreter

with your variables. Say you want to have some text that remains

constant after a variable. With Perl, the variable name is the

longest possible name it can find, so that causes trouble with

finding your variable. This example might help clear up what I

mean. In this script we are trying to unite a changing variable

with a constant piece of text:

<BLOCKQUOTE>

<PRE>

$old = &quot;name_one&quot;;

$new = &quot;name_two&quot;;

$previous = &quot;You are $old user.&quot;;

$current = &quot;You are {$new} user.&quot;;

</PRE>

</BLOCKQUOTE>

<P>

If we print $previous we get &quot;You are $old user.&quot; because

Perl holds $olduser as a new variable. But if we add curly braces

to the line, we get a more satisfactory result. When $current

is printed we get &quot;You are name_two user.&quot; We avoided

the confusion by enclosing the name of the variable in curly braces.

<P>

Curly braces, also referred to as curly brackets, perform a number

of duties in Perl, from delimiting compound statements, as above,

to marking the beginning and end of loops and subroutines. They

are even involved with regular expressions (a part of Perl explored

in the next chapter). Generally they act as high-end markers,

or delimiters, but there are no hard and fast rules for using

curly braces, so close attention must be paid to when and where

they are used.

<P>

You can also change the case of your variables and values using

the case-shifting backslash operators mentioned earlier. The following

are all ways of changing case:

<BLOCKQUOTE>

<PRE>

$user = &quot;\LNAME&quot;; # changes value

# from 'NAME' to 'name'

$new = &quot;NAME&quot;; $user = &quot;\L$new&quot;; 

# gives us 'name' for the value of

     # $user

</PRE>

</BLOCKQUOTE>

<P>

or the operators can be used together:

<BLOCKQUOTE>

<PRE>

$user = &quot;\LNAME&quot;; # $user is 'name'

$biguser = &quot;\u\LNAME&quot;; # $biguser is 'Name'

$biguser = &quot;\u\L$user&quot;; # $biguser is 'Name' 

</PRE>

</BLOCKQUOTE>

<P>

This method works because Perl will remember case-shifting operators

that are in a string until they are used, so you can put \L between

\u and $user, and \u will see the first letter in the value of

$user as the letter is should modify.

<P>

With variable interpolation, only double-quoted strings can make

use of these functions. That may be why another name for it is

double-quote interpolation.

<H3><A NAME="StandardInputltSTDINgt">

Standard Input &lt;STDIN&gt;</A></H3>

<P>

In Perl there is one special variable called Standard Input, or

&lt;STDIN&gt;. Actually, &lt;STDIN&gt; is much more than a scalar

variable, it is a filehandle. Other common filehandles you might

use in Perl are standard output, or &lt;STDOUT&gt;, and standard

error, or &lt;STDERR&gt;. A filehandle is a way for Perl to make

an input and output, or I/O, connection between a running script,

or process, and a user. A user can be a human, or another process.

<BR>

<P>

<CENTER>

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

<TR VALIGN=TOP><TD WIDTH=576><B>NOTE</B></TD></TR>

<TR VALIGN=TOP><TD WIDTH=576>

⌨️ 快捷键说明

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