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

📄 ch7.htm

📁 美国Macmillan出版社编写的Perl教程《Perl CGI Web Pages for WINNT》
💻 HTM
📖 第 1 页 / 共 2 页
字号:
            $y--;

        }

    }

    @orig=@new;

    undef @new;

}

</PRE>

</BLOCKQUOTE>

<H3><A NAME="FormtoUpdatetheMailingList">

Form to Update the Mailing List</A></H3>

<P>

Many Web sites house mailing lists. These provide a way for site

users who have mutual interests to share the ideas and information

that the site features without having to spend large amounts of

time administrating and monitoring the interchange. In addition,

mailing lists are easier to create than Usenet newsgroups.

<P>

This script will allow you to take new user input and add it to

an existing mailing list that your Web site may be hosting.

<P>

The HTML form to update the mailing list follows:

<BLOCKQUOTE>

<PRE>

&lt;HTML&gt;

&lt;TITLE&gt;Subscribe to Mailing List&lt;/TITLE&gt;

&lt;BODY&gt;

&lt;H1&gt;Subscribe to mailing list&lt;/H1&gt;

&lt;HR&gt;

&lt;P&gt;

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

Please enter your E-Mail address:&lt;BR&gt;

&lt;INPUT TYPE=&quot;TEXT&quot; NAME=&quot;email&quot; SIZE=3&Oslash;&gt;&lt;BR&gt;

&lt;INPUT TYPE=&quot;RADIO&quot; name=&quot;subscribe&quot; value=&quot;1&quot;&gt; Subscribe&lt;BR&gt;

&lt;INPUT TYPE=&quot;RADIO&quot; name=&quot;subscribe&quot; value=&quot;&Oslash;&quot;&gt; Unsubscribe&lt;BR&gt;

&lt;P&gt;

&lt;INPUT TYPE=&quot;SUBMIT&quot; VALUE=&quot;Subscribe&quot;&gt;

&lt;BR&gt;

&lt;/BODY&gt;

&lt;/HTML&gt;

</PRE>

</BLOCKQUOTE>

<P>

This creates a page that is the same as that shown in Figure 7.3.

<P>

<A HREF="f7-3.gif" tppabs="http://210.32.137.15/ebook/PC%20Magazine%20Programming%20Perl%205.0%20CGI%20Web%20Pages%20for%20Microsoft%20Windows%20NT/f7-3.gif"><B>Figure 7.3 :</B> <I>Subscribing to a mailing list</I>.</A>

<P>

And, of course, mailist.pl is as follows:

<BLOCKQUOTE>

<PRE>

#!/usr/bin/perl

# maillist.pl

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-F&Oslash;-9][a-fA-F&Oslash;-9])/pack(&quot;C&quot;,hex($1))/eg;

    $FORM{$name}=$value;

}

if ($FORM{subscribe}==1) {

    open(LIST, &quot;&gt;&gt;mailing.lst&quot;);

    print LIST &quot;$FORM{email}\n&quot;;

    close(LIST);

}

else {

    open(LIST, &quot;mailing.lst&quot;);

    undef $/;

    $file=&lt;LIST&gt;;

    $/=&quot;\n&quot;;

    close(LIST);

    $file=~s/$FORM{email}\n//;

    open(LIST, &quot;&gt;mailing.lst&quot;);

    print LIST $file;

    close(LIST);

}

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

print &quot;&lt;HTML&gt;\n&lt;BODY&gt;\n&lt;TITLE&gt;Subscribe Completed!&lt;/TITLE&gt;\n&quot;;

print &quot;&lt;H1&gt;Subscribe Completed!&lt;/H1&gt;\n&lt;HR&gt;\n&lt;P&gt;\n&quot;;

print &quot; Your E-Mail address has been successfully &quot;;

if ($FORM{subscribe}==1) {

    print &quot;added to &quot;;

}

else {

    print &quot;deleted from &quot;;

}

print &quot;the mailing list!\n&quot;;

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

</PRE>

</BLOCKQUOTE>

<P>

When the user is successfully added to the mailing list, the page

shown in Figure 7.4 is returned.

<P>

<A HREF="f7-4.gif" tppabs="http://210.32.137.15/ebook/PC%20Magazine%20Programming%20Perl%205.0%20CGI%20Web%20Pages%20for%20Microsoft%20Windows%20NT/f7-4.gif"><B>Figure 7.4 :</B> <I>Successful addition to an e-mail list</I>.</A>

<H3><A NAME="EmailRequestForm">

E-mail Request Form</A></H3>

<P>

Getting feedback and response from a site's users is very desirable.

This can be done by using a simple form that e-mails the desired

user input to a specified e-mail address.

<P>

Having the user input e-mailed somewhere is desirable because

that way you do not have to give the people who are dealing with

this input access to your server. With e-mail they don't even

have to be connected to your network, except perhaps via the Internet.

For those working with user input, it also eliminates the need

to know a lot about how your server is setup and works. They only

have to know how to use e-mail.

<P>

This next example is a form that accepts letters to the editor

for <I>The Imprint</I>, the student newspaper for the University

of Waterloo.

<P>

The HTML form for the user input looks like this:

<BLOCKQUOTE>

<PRE>

&lt;HTML&gt;

&lt;HEAD&gt;

&lt;TITLE&gt;Letter to the Webmaster!&lt;/TITLE&gt;

&lt;/HEAD&gt;

&lt;BODY&gt;

&lt;H1&gt;Letter to the Webmaster!&lt;/H1&gt;&lt;BR&gt;

&lt;HR&gt;

&lt;P&gt;

&lt;B&gt;To the Webmaster:&lt;/B&gt;

&lt;P&gt;

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

&lt;Textarea name=&quot;body&quot; ROWS=2&Oslash; COLS=5&Oslash;&gt;

&lt;/Textarea&gt;

&lt;P&gt;

&lt;B&gt;Sincerely,&lt;B&gt;

&lt;P&gt;

Name: &lt;input type=text name=&quot;name&quot; size=25&gt;&lt;BR&gt;

&lt;BR&gt;

&lt;CENTER&gt;&lt;input type=submit value=&quot;Send&quot;&gt;&lt;input type=reset&gt;&lt;/CENTER&gt;

&lt;P&gt;

&lt;/FORM&gt;

&lt;/HTML&gt;

</PRE>

</BLOCKQUOTE>

<P>

This will produce an output to a browser as shown in Figure 7.5

and Figure 7.6. The script called by the form follows:

<P>

<A HREF="f7-5.gif" tppabs="http://210.32.137.15/ebook/PC%20Magazine%20Programming%20Perl%205.0%20CGI%20Web%20Pages%20for%20Microsoft%20Windows%20NT/f7-5.gif"><B>Figure 7.5 :</B> <I>Form to submit user input to e-mail (first

screen) </I>.</A>

<P>

<A HREF="f7-6.gif" tppabs="http://210.32.137.15/ebook/PC%20Magazine%20Programming%20Perl%205.0%20CGI%20Web%20Pages%20for%20Microsoft%20Windows%20NT/f7-6.gif"><B>Figure 7.6 :</B> <I>Form to submit user input to e-mail (second

screen) </I>.</A>

<BLOCKQUOTE>

<PRE>

#!/usr/bin/perl

# letter.pl

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-F&Oslash;-9][a-fA-F&Oslash;-9])/pack(&quot;C&quot;,hex($1))/eg;

    $FORM{$name}=$value;

}

$good=&quot;&lt;HTML&gt;\n&lt;TITLE&gt;Thanks!&lt;/TITLE&gt;\n&lt;h2&gt;Thank you!&lt;/h2&gt;&lt;P&gt;Thank you for your submission. 

While we cannot guarantee that we will print every letter we receive, look for yours 

in the next edition of &lt;B&gt;Imprint&lt;/B&gt;!\n&lt;/HTML&gt;\n&quot;;

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

open(MAIL, &quot;|mail webmaster\@www.myserver.com&quot;);

print MAIL &quot;Subject: Letter to the Webmaster from 	$FORM{name}\n&quot;;

print MAIL &quot;\nThe following is a letter to the Webmaster submitted from the Web Page.\n&quot;;

print MAIL &quot;To the Webmaster:\n\n&quot;;

print MAIL &quot;$FORM{body}\n\n&quot;;

print MAIL &quot;$FORM{name}\n&quot;;

close MAIL;

print $good;

</PRE>

</BLOCKQUOTE>

<P>

where the users will get a response in the form of a Web page

that confirms what they have sent. This response will look like

Figure 7.7.

<P>

<A HREF="f7-7.gif" tppabs="http://210.32.137.15/ebook/PC%20Magazine%20Programming%20Perl%205.0%20CGI%20Web%20Pages%20for%20Microsoft%20Windows%20NT/f7-7.gif"><B>Figure 7.7 :</B> <I>E-mail submission response</I>.</A>

<H3><A NAME="SimpleDatabaseManipulation">

Simple Database Manipulation</A></H3>

<P>

Database manipulation, or DBM routines, is not supported by WinPerl.

The best way to do any type of database manipulation is by using

the split and join operators to separate plain text fields with

delimiters, as we did with the guestbook.

<H3><A NAME="TheAnimatedLogo">

The Animated Logo</A></H3>

<P>

One of the growing uses for the CGI is to put an animated logo

for the Web site on the home page. Although the language of choice

for this is Java script, Perl can also perform this procedure.

The following animated logo makes use of a series of simple JPEG

images to create a blinking eye.

<BLOCKQUOTE>

<PRE>

&lt;HTML&gt;

&lt;BODY&gt;

&lt;TITLE&gt;Animated Logo&lt;/TITLE&gt;

&lt;CENTER&gt;

&lt;IMG SRC=&quot;http://www.myserver.com/cgi-bin/anim.pl&quot;&gt;

&lt;h1&gt;

Welcome to the Hand of Vision Page

&lt;/h1&gt;

&lt;/CENTER&gt;

&lt;/HTML&gt;

&lt;/BODY&gt;

</PRE>

</BLOCKQUOTE>

<P>

This produces a Web page that looks like Figure 7.8. The eye blinks

when it runs in real time on a browser.

<P>

<A HREF="f7-8.gif" tppabs="http://210.32.137.15/ebook/PC%20Magazine%20Programming%20Perl%205.0%20CGI%20Web%20Pages%20for%20Microsoft%20Windows%20NT/f7-8.gif"><B>Figure 7.8 :</B> <I>A Web page with an animated logo</I>.</A>

<BLOCKQUOTE>

<PRE>

#!/usr/bin/perl

# anim.pl

@files = (&quot;logo-1.gif&quot;,&quot;logo-2.gif&quot;,&quot;logo-3.gif&quot;,&quot;logo-4.gif&quot;,&quot;logo-5.gif&quot;,&quot;logo-6.gi

f&quot;,&quot;logo-7.gif&quot;);

print &quot;Content-Type: multipart/x-mixed-replace;boundary=myboundary\n\n&quot;;

print &quot;--myboundary\n&quot;;

foreach $file (@files) {

         print &quot;Content-Type: image/gif\n\n&quot;;

          open(LOGO,&quot;$file&quot;);

          print &lt;LOGO&gt;;

          close(LOGO);

          print &quot;\n--myboundary\n&quot;;

          sleep(1);

}

</PRE>

</BLOCKQUOTE>

<P>

This script will print each of the GIF files in @files in sequence.

<P>

The final sleep() command may be modified or removed as required

to make the animation more effective. The sleep operator paces

how fast or slow your images are presented. For a smoother animation,

change this value.

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

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

<P>

The scripts covered in this chapter are only a smattering of what

you can do with Perl to enhance your Web pages. The only limit

to how you can use Perl is your imagination, and even then there

are always new Perl libraries and archives coming online all the

time. Check for these new resources regularly.

<P>

The past two chapters have dealt with Perl programs explicitly

for your Web pages, but Perl can also be used to help you on your

server, which subject is explored in the next chapter. Also, you

might now have some questions concerning the CGI and related protocols;

these are covered in the final chapters in this book.

<HR>



<CENTER><P><A HREF="ch6.htm" tppabs="http://210.32.137.15/ebook/PC%20Magazine%20Programming%20Perl%205.0%20CGI%20Web%20Pages%20for%20Microsoft%20Windows%20NT/ch6.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="ch8.htm" tppabs="http://210.32.137.15/ebook/PC%20Magazine%20Programming%20Perl%205.0%20CGI%20Web%20Pages%20for%20Microsoft%20Windows%20NT/ch8.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 + -