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

📄 ch4.htm

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

<HEAD>

<TITLE>Chapter 4 -- HTML and Perl</TITLE>



<META>

</HEAD>

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

<H1><FONT SIZE=6 COLOR=#FF0000>Chapter&nbsp;4</FONT></H1>

<H1><FONT SIZE=6 COLOR=#FF0000>HTML and Perl</FONT></H1>

<HR>

<P>

<CENTER><B><FONT SIZE=5><A NAME="CONTENTS">CONTENTS</A></FONT></B></CENTER>

<UL>

<LI><A HREF="#UserFunctions">

User Functions</A>

<UL>

<LI><A HREF="#Subroutines">

Subroutines</A>

<LI><A HREF="#SubroutinesandReturnValues">

Subroutines and Return Values</A>

<LI><A HREF="#UserFunctionsandLocalVariables">

User Functions and Local Variables</A>

<LI><A HREF="#MoreControlStructures">

More Control Structures</A>

</UL>

<LI><A HREF="#ProcessingText">

Processing Text</A>

<UL>

<LI><A HREF="#FileTests">

File Tests</A>

</UL>

<LI><A HREF="#HTMLTaggingandPerl">

HTML Tagging and Perl</A>

<UL>

<LI><A HREF="#CombiningPerlandHTML">

Combining Perl and HTML</A>

</UL>

<LI><A HREF="#Conclusion">

Conclusion</A>

</UL>

</UL>



<HR>

<P>

In this chapter we begin to apply what you are learning in Perl

to HTML. With this we can start to integrate Perl into your Web

server environment.

<P>

Using the basic guestbook script from the last chapter, we can

begin to integrate Perl scripts with HTML. You will begin to understand

how Perl works with the Common Gateway Interface (CGI). In this

chapter you will also cover more details involved in Perl programming:

how Perl processes a text file, and how it then manipulates it.

<P>

Before we get to these topics though, there are a few final areas

of Perl programming that you should be introduced to: specifically,

user functions (or subroutines), and more control structures,

such as the Last and Next operators.

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

User Functions</FONT></A></H2>

<P>

When anything happens in Perl it is called an <I>action</I>. One

of the actions that takes place in a script is when variables

are modified by operators. These operators are part of the methods,

or functions, which Perl provides. We have already touched on

a few of Perl's functions-the operators print and chop- that are

standard issue Perl system functions. A system function's action

is defined by the Perl source code, but Perl also has other functions

which do not have a predetermined action. These are called user

functions. Unlike the print system function, which is defined

by Perl,<I> user functions</I> are defined by you, the programmer.

Another name for the Perl user function is a<I> subroutine, </I>or<I>

sub.</I>

<H3><A NAME="Subroutines">

Subroutines</A></H3>

<P>

Here is an example of a general format for a user function:

<BLOCKQUOTE>

<PRE>

sub subroutine_name {

statement_1;

statement_2;

statement_3;

}

</PRE>

</BLOCKQUOTE>

<P>

where the subroutine_name is the name of the user function, or

subroutine, and the statements that define the user function.

<P>

To create a subroutine called hey_now, you might write some code

that looks like this:

<BLOCKQUOTE>

<PRE>

sub hey_now {

print &quot;Hey, now!\n&quot;;

}

</PRE>

</BLOCKQUOTE>

<P>

where the print statement will be invoked when the subroutine

hey_now is used.

<P>

This subroutine acts the same as using the print statement

<BLOCKQUOTE>

<PRE>

print &quot;Hey, now!/n&quot;;

</PRE>

</BLOCKQUOTE>

<P>

and can be used, or invoked, in the active part of the script

by using the &quot;&amp;&quot; symbol, like so:

<BLOCKQUOTE>

<PRE>

&amp; hey_now;

</PRE>

</BLOCKQUOTE>

<P>

I'll bet you are asking, &quot;Why go to all the trouble of creating

a subroutine, when you could just as easily give a variable, like

$hey, the value of 'Hey, now!/n' and 'print $hey' where it is

needed?&quot; This next example illustrates the advantages.

<BLOCKQUOTE>

<PRE>

&amp;scan;

sub scan {

foreach $file (&lt;*&gt;) { # Get each file in the directory

if (-d $file) { # check to see if it is a directory

chdir($file); # Change to it

&amp;scan; # Do the scan again

chdir('..'); # Change back...

}

elsif ($file=~/\.txt/) { # Standard stuff to change each filename

$newfile=$file;

$newfile=~s/\.txt/\.htm/i;

rename($file, $newfile);

}

}

}

</PRE>

</BLOCKQUOTE>

<P>

What does this do? Well, it uses a subroutine and recursion to

change the foo.txt files in a directory (and all of its subdirectories)

to foo.htm. It performs the scan() on the current directory, getting

all the files. If the file turns out to be a directory, it will

change the current directory to that one, execute scan() again,

and change back. This will ensure that all the subdirectories

are scanned. Every time you need to perform this scan all you

need to do is to call up the subroutine.

<P>

While a variable can only hold a value, or series of values, a

subroutine can take those variables and perform endless actions

on them. You do not have to type out the entire action again.

Proper use of subroutines are invaluable for this reason alone.

<H4>Subroutine Guidelines</H4>

<P>

It is important to know how Perl deals with Perl subroutines in

your scripts. The following points outline how Perl handles subroutines:

<UL>

<LI>Subroutines are global. They maintain their value throughout

the script, and if two have the same name, the latter one has

precedence.

<LI>Subroutine definitions may be stored anywhere in your script.

It is recommended that they be placed at the end of the script

for easier organization.

<LI>Variables can be modified, or created, inside a subroutine.

The only exceptions are local variables.

</UL>

<H3><A NAME="SubroutinesandReturnValues">

Subroutines and Return Values</A></H3>

<P>

You cannot use a subroutine in Perl without including it in some

kind of expression. When a subroutine is invoked in an expression,

it produces a value that is called a <I>return value.</I> The

current, or real, return value of a subroutine is the one given

by the final expression in the subroutine, the last time it was

invoked. The following example is an illustration:

<BLOCKQUOTE>

<PRE>

sub two_variables {

$P + $Q;

{

</PRE>

</BLOCKQUOTE>

<P>

where the subroutine two_variables is given the simple expression

of adding two scalars. This value will be the return value. When

used in a script it might look like this:

<BLOCKQUOTE>

<PRE>

sub two_variables {

$P + $Q;

{	

$P = 1;

$Q = 2;

$S = &amp;two_variables; # now $S has value of 3

$R = 3*&amp;two_variables; # $R has value of 21<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>While it is recommended that you put your subroutines at the end of your scripts, I am placing them at the beginning for tutorial demonstration purposes only. </I>

</BLOCKQUOTE>



</TD></TR>

</TABLE></CENTER>

<P>

<P>

When a subroutine is used with an array, you get a list of return

values:

<BLOCKQUOTE>

<PRE>

sub two_variables {

$P + $Q;

{

$P = 1&Oslash;;

$Q = 1;

@S = &amp;two_variables;

</PRE>

</BLOCKQUOTE>

<P>

where @s now has the value of (10,1).

<P>

It is important to remember that the last expression evaluated

may not be the last expression in the subroutine, as in this example:

<BLOCKQUOTE>

<PRE>

sub yes_or_no {

if ($yes &gt; &Oslash;) {

print &quot;You win!\n&quot;;

$yes;

} else {

print &quot;You lose!/n&quot;;

$no;

}

}

</PRE>

</BLOCKQUOTE>

<P>

where you will get a return value of &quot;You win!&quot; if any

value higher than 0 is entered into the script. The return value

depends on whether the $P or $Q is evaluated last, for this becomes

the return value. If you print a variable before this, the default

value will be 1-the value of a true print function.

<P>

What is more valuable in Perl than using global variables in our

subroutines is using different values in each subroutine, each

time they are invoked. To do this we use arguments.

<H4>Subroutine Arguments</H4>

<P>

The way in which subroutines use arguments demonstrates a clever

use of the default variables in Perl because it avoids the pitfall

of changing the values of already established variables used elsewhere

in your script.

<P>

What if you took a subroutine and invoked it in front of a list,

for example? This list would then be automatically placed into

the &quot;@_&quot; variable for the entire time the subroutine

is in operation. This gives your script greater flexibility, because

the subroutine then can use this variable to find the number of

arguments and their values. Doing this might look like this:

<BLOCKQUOTE>

<PRE>

sub hey_now {

print &quot;Hey, now! Here's $_[0]!\n&quot;;

}

</PRE>

</BLOCKQUOTE>

<P>

where the first parameter will be the target.<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>

<BLOCKQUOTE>

<I>The odd scalar $_[0] is the first element in the @_ array. This is the first parameter of the array, and the script will print whatever value is then passed to @_. 

Both the special scalar variable $_ and the special element $_[0], the first element in the array @_, look alike, but they have no relation, so be careful not to confuse them.</I> 

</BLOCKQUOTE>



</TD></TR>

</TABLE></CENTER>

<P>

<P>

We might pass a value to @_ like this:

<BLOCKQUOTE>

<PRE>

&amp;hey_now(&quot;Larry&quot;);

</PRE>

</BLOCKQUOTE>

<P>

which prints

<BLOCKQUOTE>

<PRE>

% Hey, now! Here's Larry!

</PRE>

</BLOCKQUOTE>

<P>

But we can use other ways to pass a value to @_, like

<BLOCKQUOTE>

<PRE>

$a = &quot;Hank&quot;;

⌨️ 快捷键说明

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