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

📄 ch2.htm

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

<I>A process is sort of like an application in the UNIX world, except it's more complicated than that. You will see processes referred to in discussions of Perl, CGI, and HTTP servers because of the UNIX link they all share. A process is a fascinating way 

to harness the resources a computer has, but knowing this is not fundamental to what we are looking at in this book. A full discussion of processes isn't necessary here, and we will treat them like the executables with which you are more familiar. </I>

</BLOCKQUOTE>



</TD></TR>

</TABLE></CENTER>

<P>

<P>

When &lt;STDIN&gt; is used in place of a scalar variable, the

next full line of text is read as the value of &lt;STDIN&gt;.

The full line is all the text that occurs until the next newline

or a blank, or null string. &lt;STDIN&gt; usually represents the

user's terminal. This means that usually the string value of &lt;STDIN&gt;

has a new line at the end of it, where the user has hit return

on his or her keyboard, or clicked the &quot;done&quot; button.

Remember the chop operator? It can be used to get rid of that

new line command that has come in as part of the value of &lt;STDIN&gt;.

It might work like this:

<BLOCKQUOTE>

<PRE>

$name = &lt;STDIN&gt;; # the user is asked

# to input their name

     chop $name; # to get rid of the

     # newline command

</PRE>

</BLOCKQUOTE>

<P>

and you can amalgamate these two lines into this form:

<BLOCKQUOTE>

<PRE>

chop ($name = &lt;STDIN&gt;)

</PRE>

</BLOCKQUOTE>

<P>

which allows chop to work on $name at the same time.

<P>

When working with HTML forms, there are all kinds of data that

is input from the user. You can use &lt;STDIN&gt; to deal with

some of this. Every time data is sent via the POST method from

a form, it goes into &lt;STDIN&gt;. A full discussion of HTML

forms (including the command Post), and their relation to the

CGI, can be found in <A HREF="ch10.htm" tppabs="http://210.32.137.15/ebook/PC%20Magazine%20Programming%20Perl%205.0%20CGI%20Web%20Pages%20for%20Microsoft%20Windows%20NT/ch10.htm" >Chapter 10</A>.

<H4>A Note about Filehandles</H4>

<P>

When a file is open to be read, the name of the file is assigned

to the filehandle variable. The Perl script references the filehandle,

instead of the file itself, when it needs to read or write from

that file. Perl considers your keyboard and mouse, or data from

the CGI, as another file, as it puts this data into &lt;STDIN&gt;.

The Perl interpretor also treats your screen as a file, giving

it the filehandle &lt;STDOUT&gt;.

<H3><A NAME="ThePrintOperator">

The Print Operator</A></H3>

<P>

This operator will allow you to use the values and variables we've

been covering here. Once you get the data you need into your script,

you need to get it out. By &quot;getting it out&quot; I mean moving

values around inside your script, and then outputting the results

to the desired location, whether that be to another script, a

file, printer, e-mail address, or a user's screen.

<P>

We've briefly touched on &lt;STDOUT&gt;, and the print operator

that works in conjunction with &lt;STDOUT&gt; to output the script's

data. Perl uses Print to put data where it needs to go. The print

operator takes a scalar value and places it into &lt;STDOUT&gt;.

The print operator can also be used to move an array's list of

values. From &lt;STDOUT&gt; this data is sent to the desired destination,

for example, another script, a file, printer,and so forth.

<H3><A NAME="TheValueundef">

The Value undef</A></H3>

<P>

If no value is assigned to a scalar variable it will not necessarily

crash your script. Perl gives it the value of undef, short for

undefined, which is represented by a zero when used as a number

and a zero length empty string when used as a string.

<P>

One special use of the undef value is when &lt;STDIN&gt; is done

reading a file. When it reads the next line, and finds a blank

line (or null string) then it returns the value undef. This means

you have to account for this in your script when it happens. More

of this is explained in detail in the section dealing with input

and output, or I/O, in <A HREF="ch3.htm" tppabs="http://210.32.137.15/ebook/PC%20Magazine%20Programming%20Perl%205.0%20CGI%20Web%20Pages%20for%20Microsoft%20Windows%20NT/ch3.htm" >Chapter 3</A>

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

Arrays Defined</FONT></A></H2>

<P>

Scalar data can be made into an ordered list, which in Perl is

called an<I> array. </I>An array is made up of separate elements,

which are the individual scalar variables and their values. It

is the elements that are ordered in the array; there is a sequence

from the lowest to the highest. There are no size restrictions

on arrays.

<P>

Arrays use literals, too-otherwise how could we notate them to

Perl in our scripts? An array literal conforms to this format:

<BLOCKQUOTE>

<PRE>

(1,2,3); # or,

($A,$B,$C,); # or,

($A,1,$B,2,$C,3); 

</PRE>

</BLOCKQUOTE>

<P>

where the different variables are separated with commas inside

a pair of parentheses. These variables do not have to be constants,

but can be expressions that are modified within the array, as

with:

<BLOCKQUOTE>

<PRE>

($A+1,$B,$C-1);

</PRE>

</BLOCKQUOTE>

<P>

When dealing with arrays, Perl provides a list constructor operator,

signified by two periods between two values, which can be used

in different ways, for example:

<BLOCKQUOTE>

<PRE>

(1..1&Oslash;); # is equal to (1,2,3,4,5,6,7,8,9,1&Oslash;)

</PRE>

</BLOCKQUOTE>

<P>

or,

<BLOCKQUOTE>

<PRE>

(1.2..4.2); # is equal to (1.2,2.2,3.2,4.2,5.2,6.2)

</PRE>

</BLOCKQUOTE>

<P>

or,

<BLOCKQUOTE>

<PRE>

(1..4,8,1&Oslash;); # is equal to (1,2,3,4,8,1&Oslash;)

</PRE>

</BLOCKQUOTE>

<P>

or,

<BLOCKQUOTE>

<PRE>

($A..$B); # is equal to the range of # values between $A and $B

</PRE>

</BLOCKQUOTE>

<P>

If the value to the left of the list constructor operator is less

than the value to the right, an empty list is created. The list

constructor operator also only works on whole numbers. When it

comes to the last whole number in the sequence, it stops:

<BLOCKQUOTE>

<PRE>

(1.3..4.2); # would create

# (1.3,2.3,3.3) and then stop

</PRE>

</BLOCKQUOTE>

<P>

You can use the print operator with arrays to output a statement

when you want to have a constant piece of text updated, as below:

<BLOCKQUOTE>

<PRE>

$total = @user_list # $total gets the length of 

     # @user_list

print (&quot;You are visitor number &quot;,$total,&quot;\n&quot;);

</PRE>

</BLOCKQUOTE>

<P>

where each time a new user accesses the site, a new value is added

to the array @user_list. The number of values in the array, or

its length, is printed using the variable $total.

<H3><A NAME="ArrayVariables">

Array Variables</A></H3>

<P>

Just like scalars can be variables, so can arrays. While the scalar

variable is marked with the &quot;$&quot; symbol, an array variable

is marked with a &quot;@&quot; symbol, like @total, @DATE, or

@A_very_long_name_for_an_array_variable. Other than this difference,

they follow the same naming format, not being limited in character

length at all. An array variable's value will not replace that

of a scalar variable that has the same name. Perl keeps all these

values separate.

<P>

Two other points about array variables are that an empty array

gives a null value, or empty list, and that an expression can

modify either an entire array variable or only a part of an array

variable. This is different from the scalar variable, which only

holds one value, so it is either completely modified or left the

same.

<H3><A NAME="ArrayOperators">

Array Operators</A></H3>

<P>

Array operators work in much the same way as scalar operators.

They can return values, which can then be placed into another

value for another operator, or assigned into another array variable.

What sets them apart from scalar operators is that array operators

are concerned with modifying a list of values, where as scalar

operators only modify one value.

<P>

Among the array operators that are most important are the assignment,

subscripting, push &amp; pop, shift &amp; unshift, reverse, sort,

and chop operators. Other array features are used with accessing

elements and &lt;STDIN&gt;. An element in an array is the name

given to each value in the list. Elements are also used when dealing

with associative arrays, covered in <A HREF="ch3.htm" tppabs="http://210.32.137.15/ebook/PC%20Magazine%20Programming%20Perl%205.0%20CGI%20Web%20Pages%20for%20Microsoft%20Windows%20NT/ch3.htm" >Chapter 3</A> in referencing

each value/key pair.

<H4>The Assignment Operator</H4>

<P>

This array operator places a value into an array variable. Using

the same symbol for this as scalar variables, the array assignment

operator uses the &quot;=&quot; symbol to place variable values.

Perl differentiates between a scalar and array assignment based

on the variable being assigned; it notes a scalar assignment with

a scalar variable, and an array assignment with an array variable.

When a scalar value is assigned to an array variable it becomes

an element of the array variable. Arrays can hold one or more

elements.

<P>

Array variables can be included inside an array list. They are

converted to their array literal value when the list is determined,

as in this example:

<BLOCKQUOTE>

<PRE>

@olduser = (&quot;Bob&quot;,&quot;John&quot;);

@newuser = (&quot;Nancy&quot;,&quot;Lisa&quot;,@olduser);

@alluser = (@newuser,&quot;Magdalene&quot;);

@alluser = (&quot;Admin&quot;,&quot;Jack&quot;,@alluser);

</PRE>

</BLOCKQUOTE>

<P>

which gives the array list @alluser the literal values of

<BLOCKQUOTE>

<PRE>

 Admin,Jack,Nancy,Lisa,Bob,John,Magdalene

</PRE>

</BLOCKQUOTE>

<P>

You can see where adding the array variable to the array variable's

own list places new values in front, or behind, the current list.

<P>

An array literal housing a list with no expressions can be handled

as a variable as well, but a list with added array elements cannot.

Array literals working as variables would look like this:

<BLOCKQUOTE>

<PRE>

($F,$Y,$I) = (1,2,3); # here $F is 1,

# $Y is 2 and $I is 3.

($P,$C) = ($C,$P); # here $P and $C

# trade values

($P,@C) = ($F,$Y,$I); # here $P is $F

# and @C is ($Y,$I)

($A,@C) = @C; # here the first value

# in @C is transferred to $A, making

# $A = $Y and @C = ($I)

</PRE>

</BLOCKQUOTE>

<P>

where any extra variables on the left side of the assignment operator

are given the undef value, and an extra value on the right disappears

into the ether of Perl.

<P>

If you use an array variable in a literal array list, it must

be the last variable. Any other array variable used in the literal

will be assigned the undef value. This is important if your script

doesn't have values for each element in an array, because the

undef value in Perl is treated as a false value. The undef value

can also be used to find the end of the elements with assigned

values in an array. 

<P>

When you assign an array variable to a scalar variable, the value

defined by the scalar is the number of array list entries, or

length of the list, as with:

<BLOCKQUOTE>

<PRE>

@IMHO = (5,4,6,7,2);

$IMHO= @IMHO; # where $IMHO now has

# the value of 5, the length of

     # the list of @IMHO.

</PRE>

</BLOCKQUOTE>

<P>

This is also true when an array variable name is put in place

of a scalar value, as with:

<BLOCKQUOTE>

<PRE>

$IMHO = @PGP; # where $IMHO gets the

# length of @PGP

</PRE>

</BLOCKQUOTE>

<P>

or

<BLOCKQUOTE>

<PRE>

($SSE) = @VTR; # where $SSE gets

# the first element of @VTR as

    # its value

</PRE>

</BLOCKQUOTE>

<P>

In the last example, if there are other values of @NEC, they are

dropped because there is only one scalar variable on the left.

<P>

You can nest array assignments inside other array assignments

to get this&nbsp;result: 

<BLOCKQUOTE>

<PRE>

@BBQ = (@ORA = (4,8,6)); # where

# @BBQ and @ORA have the list (4,8,6)

</PRE>

</BLOCKQUOTE>

<P>

or 

<BLOCKQUOTE>

<PRE>

@BBQ = @ORA = (4,8,6) # will give

# you the same result.

</PRE>

</BLOCKQUOTE>

<P>

<B>Array and Scalar Context&nbsp;&nbsp;</B>As mentioned earlier,

it depends on which side of the assignment operator the scalar

and/or array appears. If Perl is told the operand is scalar, then

the operation is figured with a scalar context. Conversely, if

Perl feels the operand is an array, then it is computed with an

array context.

<P>

Context can affect your routines by changing the values of your

arrays and scalars, so be careful. An expression can be made to

assess an operand in a scalar context by concatenating a null

string, like so:

<BLOCKQUOTE>

<PRE>

@Z = (&quot;d&quot;,&quot;a&quot;,&quot;v&quot;,&quot;i&quot;,&quot;s&quot;);

print (@Z.&quot; is a magic number\n&quot;);

to output

% 5 is a magic number<BR>



</PRE>

</BLOCKQUOTE>

<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>

<BLOCKQUOTE>

<I>The &quot;%&quot; symbol is used here to denote the command line in the Perl terminal, a terminal screen that looks like the MS DOS terminal screen. The &quot;%&quot; symbol is typically presented by the interpreter in the DOS-like terminal window to 

show the user where the command line is. Perl also uses this symbol for the modulus operator, so please don't get confused between the Perl scripting use of the &quot;%&quot; symbol and my signifying the Perl command line with it. </I>

</BLOCKQUOTE>



</TD></TR>

</TABLE></CENTER>

<P>

<H4>The Subscripting Operator</H4>

<P>

In an array list each element is assigned an integer marker, or

index value, starting on the left with 0 and going up one whole

number to the end of the list. These indicators are used by the

element access operator, or subscripting operator, to transfer

values to other variables. It can be useful to use elements from

an array for another purpose by transfering them into an scalar,

or modifying the existing elements in an array. These abilities

are useful to update any kind of list of data. In this example,

the various ways to use subscripting operators are demonstrated.

<BLOCKQUOTE>

<PRE>

@numbers = (3,5,7);

$pick_one = $numbers[&Oslash;]; # this

# transfers 3 to $pick_one

⌨️ 快捷键说明

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