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

📄 ch4.htm

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

<PRE>

-e $scalar_name   

</PRE>

</BLOCKQUOTE>

<P>

and can be used in your script like this:

<BLOCKQUOTE>

<PRE>

$name = &quot;/user/passwd&quot;;

if (-e $name){

} else {

print &quot;Sorry, login again.\n&quot;;

}

</PRE>

</BLOCKQUOTE>

<P>

where the script checks the password file for the password held

in $name. If the password is not there, then the user is given

the print statement. If you are going to use this script in your

security procedures, remember to include a block that exists of

users with false passwords.

<P>

There are other file tests that can be used, all of which are

listed in Table 4.1 in order of simplest test to the more involved

or obscure.<BR>

<P>

<CENTER><B>Table 4.1 File Tests</B></CENTER>

<P>

<CENTER>

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

<TR VALIGN=TOP><TD WIDTH=109><CENTER><B>File Test</B></CENTER></TD><TD WIDTH=378><B>Test Action</B>

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=109><CENTER>-r</CENTER></TD><TD WIDTH=378>File or directory is readable

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=109><CENTER>-w</CENTER></TD><TD WIDTH=378>File or directory is writable

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=109><CENTER>-x</CENTER></TD><TD WIDTH=378>File or directory is excutable

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=109><CENTER>-o</CENTER></TD><TD WIDTH=378>File or directory is owned by user

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=109><CENTER>-R</CENTER></TD><TD WIDTH=378>File or directory is readable by real user, not effective user

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=109><CENTER>-W</CENTER></TD><TD WIDTH=378>File or directory is writable by real user, not effective user

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=109><CENTER>-X</CENTER></TD><TD WIDTH=378>File or directory is writable by real user, not effective user

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=109><CENTER>-O</CENTER></TD><TD WIDTH=378>File or directory is owned by real user, not effective user

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=109><CENTER>-e</CENTER></TD><TD WIDTH=378>File or directory exists

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=109><CENTER>-z</CENTER></TD><TD WIDTH=378>File exists and has zero size

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=109><CENTER>-s</CENTER></TD><TD WIDTH=378>File or directory exists and has nonzero size

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=109><CENTER>-f</CENTER></TD><TD WIDTH=378>Entry is plain file

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=109><CENTER>-d</CENTER></TD><TD WIDTH=378>Entry is a directory

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=109><CENTER>-l</CENTER></TD><TD WIDTH=378>Entry is a symlink

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=109><CENTER>-S</CENTER></TD><TD WIDTH=378>Entry is a socket

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=109><CENTER>-p</CENTER></TD><TD WIDTH=378>Entry is a pipe

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=109><CENTER>-b</CENTER></TD><TD WIDTH=378>Entry is a block-special file

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=109><CENTER>-c</CENTER></TD><TD WIDTH=378>Entry is a character-special file

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=109><CENTER>-u</CENTER></TD><TD WIDTH=378>File or directory is setuid

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=109><CENTER>-g</CENTER></TD><TD WIDTH=378>File or directory is setgid

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=109><CENTER>-k</CENTER></TD><TD WIDTH=378>File or directory has the sticky bit set

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=109><CENTER>-t</CENTER></TD><TD WIDTH=378>Isatty() on the filehandle true

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=109><CENTER>-T</CENTER></TD><TD WIDTH=378>File is text

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=109><CENTER>-B</CENTER></TD><TD WIDTH=378>File is binary

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=109><CENTER>-M</CENTER></TD><TD WIDTH=378>Modification age in days

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=109><CENTER>-A</CENTER></TD><TD WIDTH=378>Access age in days

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=109><CENTER>-C</CENTER></TD><TD WIDTH=378>Inode-modification age in days

</TD></TR>

</TABLE></CENTER>

<P>

<P>

where you can see that there are file tests for every occasion

you might need. Each can be used to check whether the file in

question is readable, writable, executible, and so forth. Remember

that Perl lets you use these file tests on both filehandles and

filenames.

<P>

If the information about a file is not covered by a file test,

you can use the stat() operator to get any other data you may

need. You can do this by putting the operator into a statement

like this one:

<BLOCKQUOTE>

<PRE>

($blksize) = (stat(/usr/bin/index.hmtl));

</PRE>

</BLOCKQUOTE>

<P>

where the block size of the file index.html is placed in the variable

$blksize. For a full list of the file information you can receive

(because there is a lot of data concerning each file, most of

it not really useful to you), it is recommended that you check

the stat(2) manpage. If you can't find this manpage on your own,

you really don't need to know the information the stat operator

can give you. 

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

HTML Tagging and Perl</FONT></A></H2>

<P>

Now that we have covered most of the main functions of Perl, the

next step is to begin the interface with the Web browser. To finish

this chapter we'll look at combining a Perl script with HTML tags.

It is assumed that you know your way around HTML, but if you feel

a little rusty, check out this URL to find an HTML tutorial that's

suitable for you: 

<BLOCKQUOTE>

<PRE>

<A 

HREF="javascript:if(confirm('http://www.yahoo.com/Computers_and_Internet/Internet/World_Wide_Web/Information_and_Documentation/Tutorials_Demos_Talks/  \n\nThis file was not retrieved by Teleport Pro, because it is addressed on a domain or path outside the boundaries set for its Starting Address.  \n\nDo you want to open it from the server?'))window.location='http://www.yahoo.com/Computers_and_Internet/Internet/World_Wide_Web/Information_and_Documentation/Tutorials_Demos_Talks/'" tppabs="http://www.yahoo.com/Computers_and_Internet/Internet/World_Wide_Web/Information_and_Documentation/Tutorials_Demos_Talks/">http://www.yahoo.com/Computers_and_Internet/Internet/World_Wide_Web/<BR>Information_and_Documentation/Tutorials_Demos_Talks/</A>

<BR>

</PRE>

</BLOCKQUOTE>

<H3><A NAME="CombiningPerlandHTML">

Combining Perl and HTML</A></H3>

<P>

Remember the guestbook you've been working on? Forms are an excellent

way to retrieve data from a user, so let's add one to our guestbook.

The HTML code might look like this:

<BLOCKQUOTE>

<PRE>

&lt;HTML&gt;

&lt;BODY&gt;

&lt;CENTER&gt;

&lt;H1&gt;Welcome to the Guestbook!&lt;/H1&gt;&lt;BR&gt;

&lt;/CENTER&gt;

&lt;HR&gt;

&lt;P&gt;

Please enter your first name, last name, and your favorite color, then click on submit.&lt;P&gt;

&lt;FORM METHOD=&quot;POST&quot; ACTION=&quot;http://www.yourdomain.com/cgi-bin/guest.pl&quot;&gt;

&lt;STRONG&gt;

First Name: &lt;INPUT TYPE=&quot;TEXT&quot; NAME=&quot;firstname&quot; SIZE=&quot;25&quot;&gt;&lt;BR&gt;

Last Name : &lt;INPUT TYPE=&quot;TEXT&quot; NAME=&quot;lastname&quot; SIZE=&quot;25&quot;&gt;&lt;BR&gt;

Favorite Color: &lt;SELECT NAME=&quot;color&quot;&gt;

&lt;OPTION&gt;Red

&lt;OPTION&gt;Yellow

&lt;OPTION&gt;Blue

&lt;OPTION&gt;Green

&lt;OPTION&gt;Magenta

&lt;/SELECT&gt;

&lt;P&gt;

&lt;INPUT TYPE=&quot;SUBMIT&quot; NAME=&quot;Submit&quot;&gt;

&lt;/FORM&gt;

&lt;/BODY&gt;

&lt;/HTML&gt;

</PRE>

</BLOCKQUOTE>

<P>

to give us a Web page that looks like Figure 4.1.

<P>

<A HREF="f4-1.gif" tppabs="http://210.32.137.15/ebook/PC%20Magazine%20Programming%20Perl%205.0%20CGI%20Web%20Pages%20for%20Microsoft%20Windows%20NT/f4-1.gif"><B>Figure 4.1 :</B> <I>The Guestbook page</I>.</A>

<P>

Now we have to modify our guestbook program to handle this input,

but only slightly:

<BLOCKQUOTE>

<PRE>

read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});

@pairs=split(/&amp;/, $buffer);

# This is the Name-Value pair splitter.. Put into # $FORM array

foreach $pair (@pairs) {

($name,$value)=split(/=/,$pair);

$value=~tr/+//;

$value=~s/%([a-fA-F0-9][a-fA-F0-9])/pack(&quot;C&quot;,hex($1))/eg;

$FORM{$name}=$value;

}

$name=$FORM{firstname};

$lastname=$FORM{lastname};

$color=$FORM{color};

# This line tells the browser what type of data to 	# expect

print &quot;Content-type: text/html\n\n&quot;;

# Now print out the standard HTML header stuff....

print &quot;&lt;HTML&gt;\n&lt;BODY&gt;\n&lt;H3&gt;\n\n&quot;;

$newline=$name.':'.$lastname.':'.$color.&quot;\n&quot;;

# make line delimited with colons

open (GUESTBOOK, &quot;guest.pl&quot;);

while ($line=&lt;GUESTBOOK&gt;) {

($gbname, $gblastname, $gbcolor)=split(':', $line);

if (($gbname=~/^$name/i) &amp;&amp; 	($gblastname=~/^$lastname/i)) {

print &quot;You are already in the guestbook, $name!\n&quot;;

close (GUESTBOOK);

if ($gbcolor!~/$color/i) {

print &quot;You have a different favorite color!\n&quot;;

print &quot;Your old favorite color is: $gbcolor\n&quot;;

print &quot;Your new favorite color is: $color\n&quot;;

}

print &quot;&lt;/H3&gt;\n&lt;/BODY&gt;\n&lt;/HTML&gt;\n&quot;;

exit;

}

}

close (GUESTBOOK);

open (GUESTBOOK, &quot;&gt;&gt;guest.pl&quot;); # Open file for

# append

print GUESTBOOK &quot;$newline&quot;; # Append the field

# line to the guestbook file

print &quot;Thank you, $name!  Your name has been added to the Guestbook.\n&quot;;

print &quot;&lt;/H3&gt;\n&lt;/BODY&gt;\n&lt;/HTML&gt;\n&quot;;

close(GUESTBOOK);

</PRE>

</BLOCKQUOTE>

<P>

to give us a browser output like that shown in Figure 4.2.

<P>

<A HREF="f4-2.gif" tppabs="http://210.32.137.15/ebook/PC%20Magazine%20Programming%20Perl%205.0%20CGI%20Web%20Pages%20for%20Microsoft%20Windows%20NT/f4-2.gif"><B>Figure 4.2 :</B> <I>User acknowledgement page</I>.</A>

<P>

Now, what are the differences? The first one we'll notice is the

way we get information from the user. The input is done through

a Web page form. We have three form fields, one for firstname,

one for lastname, and one for color. These will be passed to the

CGI by the POST method. 

<P>

We read the passed information from STDIN, which is how the POST

method passes its information. We then split the line into name-value

pairs, which include the variable name, an &quot;=&quot; symbol,

and the value. These pairs are separated by an&quot;&amp;&quot;

symbol. 

<P>

Once that is done, we then populate an associative array called

%FORM, which will hold the name of the variable, and the associated

value. The input line for our form would look something like this:

<BLOCKQUOTE>

<PRE>

firstname=Joe&amp;lastname=Van+Horne&amp;color=Red

</PRE>

</BLOCKQUOTE>

<P>

Notice that the space between Van Horne was translated into a

&quot;+&quot; symbol. Any special characters, like slashes and

+, will be converted into hexidecimal escape codes, which the

pack() function will turn back into a readable characters. 

<P>

Another part to remember is to put the &quot;\n&quot; in between

each new HTML tag. Your browser will appreciate the newlines being

given to each consecutive tag. The newlines will organize the

HTML tags in regular script order, instead of running them all

together, where they can cause delays in loading time, and even

errors with the browser.

<P>

Once completed, we assign the $FORM{} variables from the associative

array into the regular variables used by our program. Next we

print the line:

<BLOCKQUOTE>

<PRE>

Content-type: text/html\n\n

</PRE>

</BLOCKQUOTE>

<P>

which tells the browser what type of data is coming next, so that

it will format it correctly. Notice the two newlines. These are

both required for the command to be recognized. 

<P>

Once we have done this, anything we print will automatically be

printed to the browser window. Thus, in the next lines, we print

HTML codes to format our output nicely. This really is all there

is to integrating user input from a Web browser into a CGI program.

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

Conclusion</FONT></A></H2>

<P>

In this chapter, you have done the groundwork for a basic understanding

of Perl and how it works.  By adding user functions like subroutines,

we can make a script that can be substantially shorter (as well

as cutting down the time it takes to write the script) . Subroutines

cut down on processing time, too. Subroutines can be a small as

a single action, to extemely long to include as many actions as

the script needs.

<P>

Additional control structures were added in the form of more operators:

redo,  next, and last. These work very well with subroutines and

the other self-contained blocks of script in Perl.

<P>

Labeling block statements provide a similar functionality to the

ones subroutines do, but in a more singular way, like focusing

on a single loop&nbsp;modifcation.

<P>

Text processing was introduced, with a simple use of regular expressions.

Filehandles and file tests are both ways of manipulating file

contents, or text. 

<P>

To get you on your way to writing CGI scripts in Perl, you also

added HTML to your guestboook script from the previous chapter.

Now you need to look into the ways in which Perl assists us in

programming, which is covered in the next chapter.

<HR>



<CENTER><P><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"><IMG SRC="PC.GIF" tppabs="http://210.32.137.15/ebook/PC%20Magazine%20Programming%20Perl%205.0%20CGI%20Web%20Pages%20for%20Microsoft%20Windows%20NT/PC.GIF" BORDER=0 HEIGHT=88 WIDTH=140></A>

<A HREF="#CONTENTS"><IMG SRC="CC.GIF" tppabs="http://210.32.137.15/ebook/PC%20Magazine%20Programming%20Perl%205.0%20CGI%20Web%20Pages%20for%20Microsoft%20Windows%20NT/CC.GIF" BORDER=0 HEIGHT=88 WIDTH=140></A>

<A HREF="contents.htm" tppabs="http://210.32.137.15/ebook/PC%20Magazine%20Programming%20Perl%205.0%20CGI%20Web%20Pages%20for%20Microsoft%20Windows%20NT/contents.htm"><IMG SRC="HB.GIF" tppabs="http://210.32.137.15/ebook/PC%20Magazine%20Programming%20Perl%205.0%20CGI%20Web%20Pages%20for%20Microsoft%20Windows%20NT/HB.GIF" BORDER=0 HEIGHT=88 WIDTH=140></A>

<A HREF="ch5.htm" tppabs="http://210.32.137.15/ebook/PC%20Magazine%20Programming%20Perl%205.0%20CGI%20Web%20Pages%20for%20Microsoft%20Windows%20NT/ch5.htm"><IMG SRC="NC.GIF" tppabs="http://210.32.137.15/ebook/PC%20Magazine%20Programming%20Perl%205.0%20CGI%20Web%20Pages%20for%20Microsoft%20Windows%20NT/NC.GIF" BORDER=0 HEIGHT=88 WIDTH=140></A>

<HR WIDTH="100%"></P></CENTER>

</BODY>

</HTML>

⌨️ 快捷键说明

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