📄 ch7.htm
字号:
$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>
<HTML>
<TITLE>Subscribe to Mailing List</TITLE>
<BODY>
<H1>Subscribe to mailing list</H1>
<HR>
<P>
<FORM METHOD="POST" ACTION="http://www.myserver.com/cgi-bin/maillist.pl">
Please enter your E-Mail address:<BR>
<INPUT TYPE="TEXT" NAME="email" SIZE=3Ø><BR>
<INPUT TYPE="RADIO" name="subscribe" value="1"> Subscribe<BR>
<INPUT TYPE="RADIO" name="subscribe" value="Ø"> Unsubscribe<BR>
<P>
<INPUT TYPE="SUBMIT" VALUE="Subscribe">
<BR>
</BODY>
</HTML>
</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(/&/, $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Ø-9][a-fA-FØ-9])/pack("C",hex($1))/eg;
$FORM{$name}=$value;
}
if ($FORM{subscribe}==1) {
open(LIST, ">>mailing.lst");
print LIST "$FORM{email}\n";
close(LIST);
}
else {
open(LIST, "mailing.lst");
undef $/;
$file=<LIST>;
$/="\n";
close(LIST);
$file=~s/$FORM{email}\n//;
open(LIST, ">mailing.lst");
print LIST $file;
close(LIST);
}
print "Content-type: text/html\n\n";
print "<HTML>\n<BODY>\n<TITLE>Subscribe Completed!</TITLE>\n";
print "<H1>Subscribe Completed!</H1>\n<HR>\n<P>\n";
print " Your E-Mail address has been successfully ";
if ($FORM{subscribe}==1) {
print "added to ";
}
else {
print "deleted from ";
}
print "the mailing list!\n";
print "</BODY>\n</HTML>\n";
</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>
<HTML>
<HEAD>
<TITLE>Letter to the Webmaster!</TITLE>
</HEAD>
<BODY>
<H1>Letter to the Webmaster!</H1><BR>
<HR>
<P>
<B>To the Webmaster:</B>
<P>
<FORM METHOD=POST ACTION="http://www.myserver.com/cgi-bin/letter.pl">
<Textarea name="body" ROWS=2Ø COLS=5Ø>
</Textarea>
<P>
<B>Sincerely,<B>
<P>
Name: <input type=text name="name" size=25><BR>
<BR>
<CENTER><input type=submit value="Send"><input type=reset></CENTER>
<P>
</FORM>
</HTML>
</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(/&/, $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Ø-9][a-fA-FØ-9])/pack("C",hex($1))/eg;
$FORM{$name}=$value;
}
$good="<HTML>\n<TITLE>Thanks!</TITLE>\n<h2>Thank you!</h2><P>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 <B>Imprint</B>!\n</HTML>\n";
print "Content-type: text/html\n\n";
open(MAIL, "|mail webmaster\@www.myserver.com");
print MAIL "Subject: Letter to the Webmaster from $FORM{name}\n";
print MAIL "\nThe following is a letter to the Webmaster submitted from the Web Page.\n";
print MAIL "To the Webmaster:\n\n";
print MAIL "$FORM{body}\n\n";
print MAIL "$FORM{name}\n";
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>
<HTML>
<BODY>
<TITLE>Animated Logo</TITLE>
<CENTER>
<IMG SRC="http://www.myserver.com/cgi-bin/anim.pl">
<h1>
Welcome to the Hand of Vision Page
</h1>
</CENTER>
</HTML>
</BODY>
</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 = ("logo-1.gif","logo-2.gif","logo-3.gif","logo-4.gif","logo-5.gif","logo-6.gi
f","logo-7.gif");
print "Content-Type: multipart/x-mixed-replace;boundary=myboundary\n\n";
print "--myboundary\n";
foreach $file (@files) {
print "Content-Type: image/gif\n\n";
open(LOGO,"$file");
print <LOGO>;
close(LOGO);
print "\n--myboundary\n";
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 + -