📄 asg05.htm
字号:
<H3 ALIGN=CENTER>
<CENTER>
<FONT SIZE=5 COLOR="#FF0000"><B>What Programming Language Should You Use?</B></FONT></CENTER></H3>
<BR>
<P>CGI programs can be written in any language that has access to environment variables. However, you will find that most CGI programs are written in Perl, C, sh, or Tcl. Perl is widely used because of its strengths in text processing; currently, most Web applications are heavily weighted toward searching for and retrieving textual data. However, the language you choose will depend on your programming abilities and the complexity of your CGI. This chapter is by no means intended to be a programming reference. If you don't know how to program, there are many books that will help you get started. This chapter, however, will cover the basics regarding what makes a CGI program different. I will discuss how to access data passed from a browser in languages such as Perl, C, and sh. Many of the examples will be in Perl because it offers many facilities that make some of the basic CGI development tasks easier.
<BR>
<BR>
<A NAME="E69E30"></A>
<H4 ALIGN=CENTER>
<CENTER>
<FONT SIZE=4 COLOR="#FF0000"><B>Perl</B></FONT></CENTER></H4>
<BR>
<P>Perl is an excellent choice for CGI programs. In its current version, 5.002 as of this writing, it provides object-oriented modular programming, an excellent regular expression engine, and the best free support of any language I've seen. Perl allows for very rapid development. It originally was developed as an improved sh-sed-awk hybrid, but it has evolved into a feature-rich, structured, object-oriented language suitable for many programming tasks. Perl is great for text processing, and it has the extremely useful associative array data type built right into the language—An associative array is basically a key/value pair relationship. Unlike a list array, the key or index portion of the array is a string—a feature that makes it quite easy to work with the CGI environment variables.
<BR>
<P>There aren't too many downsides to Perl. One, however, is that it generally runs slower than a compiled language such as C. Perl is an interpreted language rather than a compiled language. This means that the Perl interpreter must parse, assemble, and compile the code each time a Perl program is called. This adds overhead (albeit small) to running a Perl program. Another downside is that Perl can't (yet) generate a standalone executable file. There is an effort underway to create a Perl compiler, and an alpha version has actually been released, but it is not yet the "accepted" way to run Perl programs. Another downside of interpreted versus compiled programs is that anyone with enough cleverness can view your Perl source code within the Web tree.
<BR>
<P>Overall, though, Perl is an excellent language for CGI programming, and the one on which I will focus in this chapter.
<BR>
<BR>
<A NAME="E69E31"></A>
<H4 ALIGN=CENTER>
<CENTER>
<FONT SIZE=4 COLOR="#FF0000"><B>C</B></FONT></CENTER></H4>
<BR>
<P>C has long been the industry-standard workhorse language for both small and large projects. Development of CGI programs in C usually takes a little longer, due to compiling time and the nature of C itself, but the resulting program runs orders of magnitude faster than any interpreted code. There aren't as many shortcuts as there are in Perl, however. For example, memory for dynamic arrays isn't automatically allocated or deallocated; you need to do all your housekeeping yourself. In contrast, Perl will grow an array automatically, freeing your time to do other things. Overall, though, C is very robust and is well-suited to CGI programming; and if your CGI program needs critical performance, it is a good candidate for the task.
<BR>
<BR>
<A NAME="E69E32"></A>
<H4 ALIGN=CENTER>
<CENTER>
<FONT SIZE=4 COLOR="#FF0000"><B>Shell (</B><B>sh</B><B>)</B></FONT></CENTER></H4>
<BR>
<P>Some CGI programs are written as shell scripts. Although this may be quick and easy in some cases, sh is generally not suited for complicated programming. Use sh for the simplest of activities. Its advantages are that your system probably has a sh, or any of its variants, in it and, because sh is the environment, accessing environment variables is trivial.
<BR>
<P>The main problem with sh CGI programs is that it requires a bit of effort to decode encoded information sent to the CGI program via the QUERY_STRING environment variable or through the stdin or POST queries. There are a few tools on the Net that alleviate either of these problems quite a bit. I will cover these later.
<BR>
<BR>
<A NAME="E69E33"></A>
<H4 ALIGN=CENTER>
<CENTER>
<FONT SIZE=4 COLOR="#FF0000"><B>Where to Learn More</B></FONT></CENTER></H4>
<BR>
<P>You will find tons of useful information about programming for the Web on the Internet. The following are a few of my favorite sites and resources.
<BR>
<BR>
<A NAME="E70E3"></A>
<H5 ALIGN=CENTER>
<CENTER>
<FONT SIZE=4 COLOR="#FF0000"><B>Perl</B></FONT></CENTER></H5>
<BR>
<P>If you are interested in learning Perl , two great books written by the main developers of the language will provide about all the reference you will ever need:
<BR>
<UL>
<LI><I>Learning Perl</I> by Randal L. Schwartz, published by O'Reilly & Associates, Inc., ISBN 1-56592-042-2, is a great introduction to Perl with numerous tutorials to get you going quickly.
<BR>
<BR>
<LI><I>Teach Yourself Perl 5 in 21 Days </I>by David Till, published by Sams Publishing, ISBN 0-672-30894-0, is an ideal book for users who are interested in getting a basic understanding of Perl 4 and 5 with tutorials.
<BR>
<BR>
<LI><I>Perl 5 Unleashed</I> by Kamran Husain, published by Sams Publishing, ISBN 0-672-30891-6, takes a comprehensive look at Perl.
<BR>
<BR>
<LI><I>Programming Perl</I> by Larry Wall and Randal L. Schwartz, published by O'Reilly & Associates, Inc., ISBN 0-937175-64-1, is the authoritative guide to the Perl language. By the time this book is published, the second edition of Programming Perl, which covers Perl 5, should be out. Perl 5 is a complete overhaul of Perl 4, it provides a zillion new features. If you are running Perl 4, download the new version. It's better .
<BR>
<BR>
</UL>
<P>On the Internet, Usenet carries many things. Some of the newsgroups to check are
<BR>
<PRE>
<FONT COLOR="#000080"><A HREF="news:comp.lang.perl.announce"> news:comp.lang.perl.announce</A>
<A HREF="news:comp.lang.perl.misc"> news:comp.lang.perl.misc</A>
<A HREF="news:comp.lang.perl.modules"> news:comp.lang.perl.modules</A></FONT></PRE>
<P>A special note about the newsgroups for Perl: Do NOT post CGI-related issues to the comp.lang.perl.* hierarchy. Rather, divert any CGI-related questions to <A HREF="news:comp.infosystems.www.authoring.cgi"> comp.infosystems.www.authoring.cgi</A>. Only post to the Perl newsgroups as a last resort. There are many common questions already answered in this frequently asked questions (FAQ) document:
<BR>
<BR>
<PRE>
<FONT COLOR="#000080"><A HREF="javascript:if(confirm('http://www.perl.com/perl/faq/perl-cgi-faq.html \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.perl.com/perl/faq/perl-cgi-faq.html'" tppabs="http://www.perl.com/perl/faq/perl-cgi-faq.html"> http://www.perl.com/perl/faq/perl-cgi-faq.html</A></FONT></PRE>
<P>In addition to answering many commonly asked questions about Perl, this FAQ contains pointers for everything else you could possibly imagine about Perl.
<BR>
<P>The latest version of the Perl software is available from any of the Comprehensive Perl Archive Network (CPAN) sites listed. Its aim is to be the only Perl archive you will ever need. The CPAN archive can be found at various locations. Table 5.1 gives a list of all known sites, at the time of this writing, grouped by continent.
<BR>
<BR>
<P ALIGN=CENTER>
<CENTER>
<FONT COLOR="#000080"><A NAME="I2"></A><A NAME="I3"></A><B>Table 5.1. Comprehensive Perl archive network sites</B><B>.</B></FONT></CENTER>
<BR>
<TABLE BORDERCOLOR=#000040 BORDER=1 CELLSPACING=2 WIDTH="80%" CELLPADDING=2 >
<TR>
<TD ALIGN=center VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
<I>Africa</I>
</FONT>
<TD ALIGN=center VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080><BR></FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
South Africa
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
<A HREF="javascript:if(confirm('ftp://ftp.is.co.za/programming/perl/CPAN/ \n\nThis file was not retrieved by Teleport Pro, because it did not meet the project\'s file type specifications. \n\nDo you want to open it from the server?'))window.location='ftp://ftp.is.co.za/programming/perl/CPAN/'" tppabs="ftp://ftp.is.co.za/programming/perl/CPAN/"> ftp://ftp.is.co.za/programming/perl/CPAN/</A>
</FONT>
<TR>
<TD ALIGN=center VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
<I>Asia</I>
</FONT>
<TD ALIGN=center VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080><BR></FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Hong Kong
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
<A HREF="javascript:if(confirm('ftp://ftp.hkstar.com/pub/CPAN/ \n\nThis file was not retrieved by Teleport Pro, because it did not meet the project\'s file type specifications. \n\nDo you want to open it from the server?'))window.location='ftp://ftp.hkstar.com/pub/CPAN/'" tppabs="ftp://ftp.hkstar.com/pub/CPAN/"> ftp://ftp.hkstar.com/pub/CPAN/</A>
</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Japan
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
<A HREF="javascript:if(confirm('ftp://ftp.lab.kdd.co.jp/lang/perl/CPAN/ \n\nThis file was not retrieved by Teleport Pro, because it did not meet the project\'s file type specifications. \n\nDo you want to open it from the server?'))window.location='ftp://ftp.lab.kdd.co.jp/lang/perl/CPAN/'" tppabs="ftp://ftp.lab.kdd.co.jp/lang/perl/CPAN/"> ftp://ftp.lab.kdd.co.jp/lang/perl/CPAN/</A>
</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Taiwan
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
<A HREF="javascript:if(confirm('ftp://dongpo.math.ncu.edu.tw/perl/CPAN/ \n\nThis file was not retrieved by Teleport Pro, because it did not meet the project\'s file type specifications. \n\nDo you want to open it from the server?'))window.location='ftp://dongpo.math.ncu.edu.tw/perl/CPAN/'" tppabs="ftp://dongpo.math.ncu.edu.tw/perl/CPAN/"> ftp://dongpo.math.ncu.edu.tw/perl/CPAN/</A>
</FONT>
<TR>
<TD ALIGN=center VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
<I>Pacific</I>
</FONT>
<TD ALIGN=center VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080><BR></FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Australia
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
<A HREF="javascript:if(confirm('ftp://dongpo.math.ncu.edu.tw/perl/CPAN/ \n\nThis file was not retrieved by Teleport Pro, because it did not meet the project\'s file type specifications. \n\nDo you want to open it from the server?'))window.location='ftp://dongpo.math.ncu.edu.tw/perl/CPAN/'" tppabs="ftp://dongpo.math.ncu.edu.tw/perl/CPAN/"> ftp://dongpo.math.ncu.edu.tw/perl/CPAN/</A>
<BR><A HREF="javascript:if(confirm('ftp://ftp.mame.mu.oz.au/pub/perl/CPAN/ \n\nThis file was not retrieved by Teleport Pro, because it did not meet the project\'s file type specifications. \n\nDo you want to open it from the server?'))window.location='ftp://ftp.mame.mu.oz.au/pub/perl/CPAN/'" tppabs="ftp://ftp.mame.mu.oz.au/pub/perl/CPAN/"> ftp://ftp.mame.mu.oz.au/pub/perl/CPAN/</A>
</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
New Zealand
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
<A HREF="javascript:if(confirm('ftp://ftp.tekotago.ac.nz/pub/perl/CPAN/ \n\nThis file was not retrieved by Teleport Pro, because it did not meet the project\'s file type specifications. \n\nDo you want to open it from the server?'))window.location='ftp://ftp.tekotago.ac.nz/pub/perl/CPAN/'" tppabs="ftp://ftp.tekotago.ac.nz/pub/perl/CPAN/"> ftp://ftp.tekotago.ac.nz/pub/perl/CPAN/</A>
</FONT>
<TR>
<TD ALIGN=center VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
<I>Europe</I>
</FONT>
<TD ALIGN=center VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080><BR></FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Austria
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
<A HREF="javascript:if(confirm('ftp://ftp.tuwien.ac.at/pub/languages/perl/CPAN/ \n\nThis file was not retrieved by Teleport Pro, because it did not meet the project\'s file type specifications. \n\nDo you want to open it from the server?'))window.location='ftp://ftp.tuwien.ac.at/pub/languages/perl/CPAN/'" tppabs="ftp://ftp.tuwien.ac.at/pub/languages/perl/CPAN/"> ftp://ftp.tuwien.ac.at/pub/languages/perl/CPAN/</A>
</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Belgium
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
<A HREF="javascript:if(confirm('ftp://ftp.kulnet.kuleuven.ac.be/pub/mirror/CPAN/ \n\nThis file was not retrieved by Teleport Pro, because it did not meet the project\'s file type specifications. \n\nDo you want to open it from the server?'))window.location='ftp://ftp.kulnet.kuleuven.ac.be/pub/mirror/CPAN/'" tppabs="ftp://ftp.kulnet.kuleuven.ac.be/pub/mirror/CPAN/"> ftp://ftp.kulnet.kuleuven.ac.be/pub/mirror/CPAN/</A>
</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Czech Republic
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -