¡ó Perl-CGI ³£ÎÊÎÊÌ⼯


·¢ÐÅÈË: hsiao@uconnvm.uconn.edu, ÐÅÇø: programming ±ê Ìâ: Perl-CGI ³£ÎÊÎÊÌ⼯ -- part1 ·¢ÐÅÕ¾: 2 Tigers (Fri Jun 20 16:02:02 1997) תÐÅÕ¾: cis_nctu!news.cis.nctu!UUserv.Net.tw!aidebbs!news.yzit!news.ncu!ctu-pe À´ Ô´: times.oneway.net ±¾ Perl-CGI ³£ÎÊÎÊÌ⼯´¿ÎÄ×Ö°æÒÔ²»¶¨ÆÚ·½Ê½ÔÚ tw.bbs.comp.www ¼° tw.bbs.comp.language Á½¸öÐÂÎÅÌÖÂÛȺ×éÉϵÇÔØ¡£±¾Îĵij¬Îļþ°æ¼°½ñááµÚÒ»ÊÖ Ð°æ×ÊÁÏ¿É×ÔÁ½Ö»ÀÏ»¢¹¤×÷ÊÒÈ¡µÃ£¬Î»Ö·Îª£º <http://www.opim.uconn.edu/~2T/perl/perl-cgi-faq-chi/>¡£±¾´¿ÎÄ×Ö°æÖÐ ÄÚÈÝÈôÓкͳ¬Îļþ°æ²»Ò»ÖÂÖ®´¦£¬Ò»ÇоùÒÔááÕßΪ׼¡£±¾ FAQ TANet µÄ mirror £¨Õâ¸öÓ³Éäµã¿ÉÄÜ»¹ÒªµÈ¼¸Ìì²Å»á×¼±¸ºÃ£©Î»ì¶£º ftp://www.math.ncu.edu.tw/pub/staff/chenym/FAQ/Perl/ (anonymous FTP) ºÍ http://www.math.ncu.edu.tw/~chenym/FAQ/Perl/¡£Ì¨ÍåÉÌÒµÍø·µÄ mirror ÔòÔÚ http://www.oneway.net/FAQ/perl/¡£ ÒëÕß±àд±¾ FAQ£¨³£ÎÊÎÊÌ⼯£©Ö®¶¯»ú£¨ºÍÆäËûÐÂÎÅȺ×é FAQ µÄÄ¿µÄ²¢ÎÞ¶þÖ£©£¬ ÄËÓмøì¶ÏàͬµÄÎÊÌâÔÚÌÖÂÛȺÖÐÒ»ÔÙ³öÏÖ£¬Í½Ôö´ó¼ÒÈËÁ¦¡¢Æµ¿í£¬¼°Ê±¼äÉϵÄÀË·Ñ£¬ Òò´ËÏ£Íû½å´Ë FAQ £¬¶ÔÐí¶à·´¸²³öÏÖµÄÎÊÌâÌá³öÕýÈ·µÄ½â´ð£¬²¢¼õÉÙ´íÎó×ÊѶµÄ Á÷´«¡£ËùÒÔÒëÕßÔڴ˺ôÓõÌÖÂÛȺÖеÄÐÂÏÊÈËÔÚѯÎÊÓÐ¹Ø Perl-CGI Ò»ÀàÎÊÌâ֮ǰ£¬ ÎñÇë×ñÊØÍø·Àñ½Ú (netiquette)£¬ÏÈ×Ô±¾ FAQ ÖÐÕÒÑ°½â´ð¡£´ËÍâ±¾ FAQ ÒàÊÊºÏ ×÷Ϊ¸öÈ˽øÐÞ Perl-CGI ³ÌʽÉè¼ÆµÄ²ÄÁÏ¡£ Ϊ·½±ã´¿ÎÄ×Ö°æ¶ÁÕßÆð¼û£¬ËùÓг¬Îļþ°æÖÐËùº¬µÄ http ¼° ftp Á¬½Úµã¾ù¸½¼Óì¶ Êʵ±´¦À¨ºÅÖУ¬µ«Î¨¶À Q1.5 ÖеÄÊéÄ¿±íÀýÍ⣨Òò±àÅŲ»Ò×£©¡£Èô¶Ô±¾ÖÐÎÄ°æ³£ÎÊ ÎÊÌ⼯ÓÐÈκÎÎÊÌâ¼°Òâ¼û£¬Çë email ÒëÕß¡£ Ô­ÎÄ°æ¡¢ÖÐÒë°æ°æȨÊÂÒË£¬Ïê¼ûµÚ 5 ²¿·ÝÎĵף¨¸´ÖÆ¡¢×ªÔØÕßÎñ±Ø×ñÊØ£©¡£ --Ïô°ÙÁä Pai-ling Hsiao <hsiao@uconnvm.uconn.edu> -------- 8< ------- Cut Here ------- 8< -------- ÖÐÎÄ°æ Perl CGI ³Ìʽд×÷³£ÎÊÎÊÌ⼯ (Perl CGI Programming FAQ) ============================================================= °æ±¾£º 1.0 Ô­ÎÄ×÷Õߣº Shishir Gundavaram <shishir@ora.com> Tom Christiansen <tchrist@perl.com> ÖÐÒë¡¢²¹³ä£º Ïô°ÙÁä <hsiao@uconnvm.uconn.edu> °æȨÊÂÒË£º£¨¸´ÖÆ¡¢×ªÔØÕßÎñ±Ø×ñÊØ£© This document, and all its parts, are Copyright (c) 1996, Shishir Gundavaram and Tom Christiansen. All rights reservered. Permisson to distribute this collection, in part or full, via electronic means (emailed, posted or archived) or printed copy are granted providing that no charges are involved, reasonable attempt is made to use the most current version, and all credits and copyright notices are retained. Requests for other distribution rights, including incorporation in commercial products, such as books, magazine articles, or CD-ROMs should be made to either of the authors. ±¾ÎļþÖø×÷ȨÊôì¶ Shishir Gundavaram ¼° Tom Christiansen Ëù ÓУ¬Copyright (C) 1996¡£ÔÚ²»Éæ¼°ÊÕ·ÑÓªÀû¡¢¾¡¿ÉÄܵØʹÓÃ×îР°æ£¬¼°ËùÓÐÖø×÷Ȩ¸æʾ±£³ÖÍêÕûµÄÇé¿öÏ£¬×÷ÕßÔÊÐíÈκÎÈË͸¹ýµç ×ÓÐÎʽ£¨µç×ÓÓʼþ¡¢ÌÖÂÛȺ²¼¸æ£¬»ò´æ·Å£©£¬»òÓ¡±í·½Ê½¶Ô±¾Îļþ ×÷ÍêÕû»ò²¿·Ý·¢ÐС£ÈçÓû½«±¾Îļþ×÷ÆäËû·½Ê½·¢ÐУ¬°üÀ¨½«±¾Îļþ ¸½¼Óì¶ÉÌÒµ²úÆ·£¬ÖîÈçÊé¼®¡¢ÔÓÖ¾ÎÄÕ£¬»ò¹âµúµÈÖ®ÖУ¬±ØÐëÊÂÏÈ¶Ô ¶þλ×÷ÕßÆäÖÐÒ»ÈËÌá³öÇëÇó£¬ÒÔáçµÃÐí¿ÉÊÚȨ¡£ ±¾ÖÐÒë°æ¼°ÒëÕß²¹³ä²¿·ÝÖø×÷ȨÊôÏô°ÙÁä¼°Á½Ö»ÀÏ»¢¹¤×÷ÊÒËùÓУ¬ Copyright (C) 1997¡£±¾ÖÐÒë°æ×ñÊز¢Ê¹ÓÃÓëÉÏÊöÔ­ÎÄ°æÏàͬµÄʹÓà Ìõ¿î·¢ÐС£ --------------------------------------------------------------------------- ÎÄÌå˵Ã÷£º 1. ÒëÕßÒѾÍÔ­ÎÄÖÐÓïÑɲ»Ïê¡¢×ÊѶ¹ýʱ£¬»ò¿É×÷¶îÍâ²¹³äÖ®´¦¸½¼Ó×¢½â¡£ÕâЩע½â ¾ùÒÔÐǺŠ(*) ì¶Êʵ±´¦£¨Í¨³£Îª¾ä⣩±êʾ¡£Êµ¼ÊµÄ˵Ã÷ÎÄ×ÖÔò´ó¶à½ôÁÚÐǺŠËùÔڵĶÎÂ䣬×Ô³ÉÒ»¶Î¡£ËùÓÐ×¢½âÎÄ×Ö¾ùÒÔ¡¸¡¾ÒëÕß¡¿¡¹ÆðÍ·¡£ 2. ¡õÀýÖÐµÄ Perl ³ÌʽÂëÒÑÓÉÒëÕßÊʶȼÓÒÔ·­Ò롢ȥ³ý¶àâŵÄÀ¨»¡£¬²¢ÒÀ perlstyle manpages ÖÐËù½¨ÒéµÄд×÷¸ñʽ¼°Ï°¹ßÉÔ×÷ÐÞÕû¡£ --------------------------------------------------------------------------- Ŀ¼£º 1.0 £­ÈëÃżò½é Q1.1: Ϊʲ÷áÎÒµÄ HTML page/form ÐèÒªÓà script£¿ Q1.2: CGI ¸÷´ú±íʲ÷᣿ Q1.3: µ½µ×ʲ÷á½Ð script£¿ÎÒÄÜÄÃËüÀ´ÄÜ×öʲ÷᣿ Q1.4: ʲ÷áÊÇ Perl£¿ÎªÊ²÷áÓÐÄÇ÷á¶àÈËÓÃËüÀ´×ö CGI£¿ Q1.5: ÓÐûÓÐ½Ì CGI »ò Perl µÄÊé»òÊÇÏßÉÏ×ÊÁÏ£¿ Q1.6: ÊDz»ÊÇÓÐÕâ·½ÃæµÄÓʵÝÂÛ̳ (mailing lists) »òÐÂÎÅÌÖÂÛȺ×飿 Q1.7: Íø·ÉÏÊDz»ÊÇÓÐרÃÅÊÕ²ØÓʵÝÂÛ̳»òÐÂÎÅÌÖÂÛȺ×éµÄ posts µÄÕ¾£¿ 2.0 - Modules £¨Ä£×飩 Q2.1: ÎÒÊDz»ÊǸÃÓà Perl CGI modules À´Ð´ CGI scripts £¿×Ô¼º×ö²»ÊDZȽÏÈÝ Ò×Â𣿠Q2.2: ÈçºÎÖªµÀij¸ö module ¸ÃÔõ÷á¸öÓ÷¨£¿ Q2.3: ÓÐÄÄЩ Perl4 µÄ CGI »ò WWWµÄ libraries £¿ Q2.4: ÓÐÄÄЩ¸ø Perl5 ʹÓÃµÄ CGI modules£¿ÎÒ¸ÃÓÃÄĸö£¿ÎªÊ²÷᣿ Q2.5: Ϊʲ÷áÕâ÷á¶à perl CGI libraries ¶¼ÊÇ object-oriented £¨Îï¼þµ¼Ïò£© µÄ£¿ÎÒ¶ÔÓÃÎï¼þµ¼ÏòÀ´Ð´³ÌʽһÇϲ»Í¨¡£ÄѵÀ¾ÍûÓмòµ¥Ò»µã£¬¸ø²»»áÎï ¼þµ¼ÏòµÄÈËʹÓÃµÄ libraries Âð£¿Îï¼þµ¼ÏòÓжàÄÑ£¿ 3.0 - CGI Óë WWW Server Q3.1: ÎÒµÄ Perl CGI ³ÌʽҪ·ÅÔÚÄÄÀïÖ´ÐУ¿cgi-bin Õâ¸öĿ¼ÊÇ×öÉõ÷áÓõģ¿ Q3.2: ʲ÷áÊǵµ°¸Ê¹ÓÃȨÏÞ£¿ÔõÑù¸Ä£¿ Q3.3: Perl Ó¦¸Ã°²×°ÔÚÄÄÀï²Å¿ÉÒÔÖ´ÐÐËü£¿ Q3.4: ÎÒΪʲ÷áÒ»Ö±µÃµ½ ``Server: Error 500'' µÄѶϢ£¿ Q3.5: ÎÒÊÔÖø´ò¿ªÒ»¸öµµ°¸£¬Ïë°Ñ×ÊÁÏ´¢´æÔÚÀïÍ·£¬µ«ÊÇ open() µÄÖ¸Áîʧ°Ü ÁË¡£µ½µ×ÊÇÔõ÷á¸ãµÄ£¿ 4.0 £­³ÌʽÉè¼ÆÒÉÄÑÔÓÖ¢ Q4.1: ÎÒÏëÈà user ÌîµÄ form ×ÊÁÏ×Ô¶¯¼Ä¸øÎÒ£¬¸ÃÔõ÷á×ö£¿ÓÐûÓСõÀý£¿ Q4.2: ¸Õ²ÅÕâ¸öÓà form ËÍÐÅµÄ script ¿´ÆðÀ´ÓеãÄÑ¡£ÎªÊ²÷᲻Ǭ´àÓà ``mailto: URL''£¬ÕâÑù user ÌîÈëµÄ×ÊÁϾͿÉÒԼĸøÎÒÁË£¿ Q4.3: ÎÒÒªÈçºÎÔÚ UNIX ÒÔÍâµÄƽ̨ÉÏ×ö Perl-CGI£¬Æ©Èç Mac¡¢MS-DOS¡¢ Windows ¼° NT£¿ÎÒµÄ Perl CGI ³ÌʽÄܲ»ÄÜÔÚÕâЩƽ̨֮¼ä»¥ÏàÒÆÖ²ÄØ£¿ Äܲ»ÄܺÜÖ±½Ó£¬Ã»ÓÐÂé·³£¿ÎÒÔÚ UNIX Ö÷»úÉÏÓÐÕʺţ¬µ«ÊǶ¼ÊÇÏÈÔÚ Windows/Mac ÉÏ×ö¡£ÎÒÒªÈçºÎÔÚÎÒ×Ô¼ºµÄ»úÆ÷ÉϲâÊÔдºÃµÄ CGI scripts £¿ Q4.4: ÔÚ Perl CGI ³ÌʽÖУ¬STDERR £¨±ê×¼´íÎóѶϢ)¡¢STDIN (±ê×¼ÊäÈ룩£¬ºÍ STDOUT £¨±ê×¼Êä³ö£©¸÷ÊÇÁ¬µ½ºÎ´¦£¿ Q4.5: ÈçºÎд¼ÆÊýÆ÷£¿ Q4.6: ÒªÈçºÎÓÃÒ»¸ö Perl µÄÈ¡´úÖ¸ÁËùÓÐ HTML ±êÇ©´ÓÒ»·ÝÎļþÖÐɾ³ý£¿ Q4.7: ÒªÈçºÎÖªµÀÊÇË­£¯ÄĄ̈»úÆ÷£¯Äĸöä¯ÀÀÆ÷Ö´ÐÐÁËÎҵijÌʽ£¿ Q4.8: È˼ҿ´µÃµ½ÎÒµÄ Perl CGI ³ÌʽÂð£¿Èç¹ûÊÇÕâÑùµÄ»°£¬ÄDz»¾ÍÈÃËûÃÇÖªµÀ ÎҵijÌʽÊÇÔõ÷áÔË×÷µÄÁË¡£ÕâÊǸö°²È«Â©¶´Âð£¿ÎÒÒªÔõ÷á°ÑËüÒþ²ØÆðÀ´£¿ Q4.9: ÎÒÐèÒª½«Õû¸ö Perl library ¶¼¸´ÖƵ½ÎÒµÄ htdocs Ŀ¼µ×ÏÂÂ𣿠Q4.10: ÎÒΪʲ÷á²»¸Ã½ÐʹÓÃÕßÊäÈëËûÃǵÄÃÜÂë»òÉí·ÝÖ¤×ֺŻòÐÅÓÿ¨ºÅÂ룿ÓÐÒ» ¸ö TYPE="password" ²»ÊǾÍÊÇÄÃÀ´×öÕâ¸öµÄÂ𣿠Q4.11: ÎÒÒªÈçºÎ²úÉúרÃÅÌæ Netscape Éè¼ÆµÄÍøÒ³£¬ÒÔ±ðì¶ÊÀÉÏÆäËûµÄä¯ÀÀÆ÷£¿ Q4.12: Ϊʲ÷áÎÒµÄ system() Ëù²úÉúµÄ×ÊÁÏÊä³ö˳Ðò²»¶Ô£¿ Q4.13: ÎÒÌý˵ Netscape »áÖ§Ô® Java¡£ÕâÊDz»ÊÇ˵ÎÒÏÖÔÚµÃÆú Perl£¬¸ÄJava ÁË£¿ÊDz»ÊǸÃÕâ÷á×ö£¿ Q4.14: ÎÒÒªÈçºÎ¶ÁÈ¡»·¾³±äÊý£¿ÎªÊ²÷áËüÃÇÓÐʱºò»á²»Ò»Ñù£¿ Q4.15: Ϊʲ÷áÎÒÊä³öµÄ×ÊÁϱ»½ÁÂÒÁË£¨Èç ``b < a'' »á±»ÆÆ»µµô£©£¿ Q4.16: Ϊʲ÷áÎÒµÄPerl CGI ³Ìʽ¿ÉÒÔÓÉÖ¸ÁîÁУ¬È´ÎÞ·¨´Óä¯ÀÀÆ÷È¥Ö´ÐУ¿ Q4.17: Ϊʲ÷áÎÒµÄ Perl CGI ³ÌʽÄÜÅÜ£¬µ«ÊDz»»á°Ñ×ÊÁÏдµ½µµ°¸ÖУ¿ Q4.18: ÒªÈçºÎ×öÒ»¸ö»áάϵ״̬£¬»òÔÊÐí¡¾Í¬Ò»Ê¹ÓÃÕß¡¿¶à´ÎÁ¬ÏßµÄ form£¿ Q4.19: Èç¹û²»´Óä¯ÀÀÆ÷È¥Ö´ÐÐÎÒµÄ CGI ³Ìʽ£¬ÒªÈçºÎÌæËü³ý´í£¿ Q4.20: Èç¹û²»¿¿<FORM>±êÇ©£¬ÒªÈçºÎ½Ð³ö Perl CGI ³Ìʽ£¿ Q4.21: ÒªÈçºÎ±ÜÃâÅÔÈ˲»ÏÈÌîÀ¸Î»¾ÍÖ´ÐÐÎÒµÄ form£¿ËûÃÇΪʲ÷áÒ»Ö±²»¶ÏÕâ÷á ×ö£¿ Q4.22: ÄÇЩ server »ØÓ¦Âë (server response codes) ÊǸÉʲ÷áÓõģ¿ÓÐʲ÷áÒâ Ò壿 Q4.23: Ϊʲ÷á print "Location: http://host/page.html\n" ²» work£¿ÓÖΪʲ ÷áËüÖ» work Ò»´Î£¬µ«ËæááµÄתÏò¾Í¶¼Åª´íÁËÄØ£¿ Q4.24: ÒªÈçºÎÈà server ÔÚÿ¸ö HTML ÍøÒ³µÄµ×²¿¶¼×Ô¶¯¼ÓÉÏÒ»¸ö£º¡¸×î½ü¸üРÈÕÆÚ£º ...¡¹µÄ¸æʾ£¿»òÕߣ¬ÊDz»ÊÇÖ»ÓÐ SSI µÄÍøÒ³²ÅÄÜÕâ÷á×ö£¿CGI ³Ì ʽµÄÈÕÆÚÒªÈçºÎÈ¡µÃ£¿ Q4.25: ʲ÷áÑùµÄ³¡ºÏÏÂÒÔ Perl д CGI ³Ìʽ»áÏÔµÃ̫СÌâ´ó×÷£¬ÒòΪÓà shell ¾Í¿ÉÒÔ×öµ½£¿¶øʲ÷áÑùµÄ³¡ºÏ¶Ô Perl À´ËµÓÖ¹ýì¶À§ÄÑ£¿Óà C++ ×öÕâÀàµÄ ʲ»ÊǺõöàÂð£¿ÄÇÓà C ÄØ£¿ 5.0 £­°²È« Q5.1: ÒÔ Perl д³ÉµÄ CGI ³ÌʽÊDz»ÊDz»ÈçÒÔ shell »ò C дµÄÀ´µÃ°²È«£¿ Q5.2: ÎÒ¸ÃÌرðÁôÒâÄÄЩ°²È«ÊÂÏ Q5.3: Ϊʲ÷á´ó¼Ò¶¼Ëµ http://bigidiot.abuse-me.com/perl.exe?foo.pl ÕâÑù ºÜΣÏÕ£¿»áÓжàÔ㣿 Q5.4: ÒªÈçºÎÔÚ³ÌʽÖа²È«µØʹÓÃÄæÏòƲºÅ£¨backticks£¬"`"£©£¿Õâ÷á×ö£º @ans = `grep'$user_field' some.file`; ÊDz»ÊÇÕæµÄ²»°²È«£¿ Q5.5: /$user_variable/ Õâ¸ö¾ä·¨ÊDz»ÊÇ Perl 5 ÖеÄÒ»¸ö°²È«Â©¶´£¿ --------------------------------------------------------------------------- 1.0 £­ÈëÃżò½é --------------------------------------------------------------------------- Q1.1: Ϊʲ÷áÎÒµÄ HTML page/form ÐèÒªÓà script£¿ ÒòΪÓеÄʱºòÄúÐèÒªÔÚ HTML ÎļþÖÐʹÓö¯Ì¬×ÊÁÏ£¨·Ç¹Ì¶¨²»±äµÄ×ÊÁÏ£©¡£Õâ°üÀ¨ÁË ÈÕÆÚºÍʱ¼äÕâÀàµÄ¼òµ¥×ÊÁÏ£¬»òÊÇÒ»¸öÏÔʾ¡¸ÄúÊÇµÚ xxx λ·Ã¿Í¡¹µÄ¼ÆÊýÆ÷¡£µ«Ëü Ò²¿ÉÄÜ°üÀ¨¸ù¾ÝʹÓÃÕßÊäÈë¶øµÃ³öµÄ´ó±ýͼ£¯Ìõͳͼ¡¢×ÊÁÏ¿âËÑÑ°²úÉúµÄ½á¹û£¬»ò¶¯ »­µÈÕâÀàµÄ¶«Î÷¡£Òª×ö³öÏñÕâÑùµÄ¶«Î÷£¬ÄúΨһµÄ·½·¨ÊÇʹÓà CGI scripts £¨¾¡¹ÜÄú Ò²¿ÉÒÔÓ¦Óÿͻ§¶Ë³Ìʽ£¬Èç Java ºÍ JavaScript À´´ïµ½Õâ¸öÄ¿µÄ£¬²»¹ýÄÇÓÖÊÇÍêÈ« ²»Í¬µÄÒ»»ØÊ£¡£©¡£ --------------------------------------------------------------------------- Q1.2: CGI ¸÷´ú±íʲ÷᣿ ÒÔÏÂÊÇÎҵı༭* Andy Oram (<http://jasper.ora.com/andyo>) ºÍ Linda Mui (<http://pcnt1.ora.com>) £¨ËûÃǺܰô£¡£©ËùдµÄÒ»¶Î·Ç³£ºÃµÄÃèÊö£º ¡¾ÒëÕß¡¿ÕâÊÇÔ­ÎĵĵÚÒ»×÷Õß Shishir G. Ö¸ËûËùдµÄ CGI Programming on the World Wide Web (<http://www.ora.com/info/cgi/>) Ò»ÊéµÄ±à ¼­¡£ Common ÏòÄúÈ·±£ CGI ¿ÉÒÔʹÓöàÖÖ³ÌʽÓïÑÔ²¢ºÍ¶àÖÖ ²»Í¬µÄϵͳ»¥¶¯¡£ËüÈÃÄú×ÔÓÉÑ¡Ôñ´ïµ½Ä¿µÄµÄ·½ ·¨£¬²»°ÑÄú°óËÀÔÚµ¥¶ÀÒ»ÖÖ×÷·¨Ö®ÏÂ*¡£ Gateway ÌáʾÄú CGI µÄÁ¦Á¿²»ÔÚì¶Ëü±¾ÉíËù×öµÄÊ£¬¶ø ÔÚì¶ËüÌṩÁËÁ¬½áÆäËûϵͳµÄDZÁ¦£¬ÀýÈç×ÊÁÏ¿â ºÍͼÐÎÖÆÔìÆ÷¡£ Interface Ö»ÊDZíʾ CGI ¶ÔÈçºÎÉÆÓÃÆäÌØÐÔÌṩÁËÃ÷È·µÄ ½ç¶¨£­»»¾ä»°Ëµ£¬Äú¿ÉÒÔÉè¼Æ³ÌʽÀ´Êʵ±ÀûÓÃÕâ ¸ö½éÃæ¡£ ¡¾ÒëÕß¡¿ËƺõÓÐÓ°Éä Java Ö®Òâ¡£ --------------------------------------------------------------------------- Q1.3: µ½µ×ʲ÷á½Ð script£¿ÎÒÄÜÄÃËüÀ´ÄÜ×öʲ÷᣿ ¼òµ¥µÄ˵£¬script ¾ÍÊdzÌʽ£¡ºÃ°É¡¢ºÃ°É£¬Ó¦¸Ã˵¶þÕßÓÐÓïÒâÉϵIJî±ð¡£Èç¹ûÄúÕæ µÄÏëÖªµÀ£¬ÄÇ÷áÈ¥ÂòÒ»±¾µçÄÔ³ÌʽÉè¼ÆµÄÊéÀ´¿´£¨»òÐí¸Ã˵ script Éè¼Æ :-) £©¡£ Äú¿ÉÒÔ½åÖøд CGI ³Ìʽ/script ±äºÜ¶àħÊõ¡£Äú¿ÉÒÔ¼´Ê±ÖÆ×÷ͼÐΡ¢Á¬½á×ÊÁÏ¿â´« »Ø¡¾²éѯ¡¿½á¹û£¬»¹¿ÉÒÔÁ¬µ½ Internet ÉÏÆäËûµÄ servers È¥¡£ --------------------------------------------------------------------------- Q1.4: ʲ÷áÊÇ Perl£¿ÎªÊ²÷áÓÐÄÇ÷á¶àÈËÓÃËüÀ´×ö CGI£¿ ´ð°¸¾ÍÔÚ perl manpage ÖеÄÇ°ÈýÐÐÐðÊö£º Perl ÊÇÒ»½âÒëʽµÄÓïÑÔ£¬×¨Îª¸ßЧÂʼìÊÓÎÄ×Öµµ°¸¡¢´ÓÖгéÈ¡×ÊÁÏ£¬¾Ý ÒÔÓ¡ÖƱ¨±í¶øÉè¼Æ¡£ ¾ø´ó¶àÊý CGI Ó¦ÓóÌʽµÄÈÎÎñ¶¼Éæ¼°¶Ô×ÊÁÏ×÷ijÖ̶ֳȵĴ¦Àí£¬¼°Á¬½áÍâÔÚ³Ìʽ¡£ Perl Ç¡ºÃÌṩÁ˺ÃÓõŤ¾ß£¬ÈÃÈËÇáËÉÓä¿ìµØ´ï³ÉÕâЩÈÎÎñ¡£ --------------------------------------------------------------------------- Q1.5: ÓÐûÓÐ½Ì CGI »ò Perl µÄÊé»òÊÇÏßÉÏ×ÊÁÏ£¿ * NCSA µÄ CGI Îļþ (<http://hoohoo.ncsa.uiuc.edu/cgi>) * Forms ÈëÃÅÖ¸Òý (<http://robot0.ge.uiuc.edu/~carlosp/cs317/ft.4-5.html>) * Ðí¶à CGI ×ÊÔ´½áµã (<http://www.cs.oberlin.edu/students/thirdstream/paxtond/cgi_stuff.html>) * ԭʼµÄ CGI FAQ (<http://www.best.com/~hedlund/cgi-faq>) * Perl FAQ (<http://mox.perl.com/perl/faq/index.html>) * ÓÉ Lincoln Stein ËùÖø£¬Ò»·ÝºÜÍêÕûµÄWWW ¼° CGI ²Ù×÷°²È« FAQ (<http://www-genome.wi.mit.edu/WWW/faqs/www-security-faq.html>) * Paul Phillips ËùÖø£¬CGI °²È« FAQ (<http://www.cerf.net/~paulp/cgi-security/safe-cgi.txt>) * WWW FAQ (<http://boutell.com/faq/>) ¡¾ÒëÕß¡¿´Ë·Ý FAQ µÄÖÐÒë°æ¿ÉÔÚ http://www.acer.net/document/cwwwfaq/ (<http://www.acer.net/document/cwwwfaq/>) ´¦È¡µÃ¡£ ÒÔÏÂÊÇÓÉ Cye H. Waldman (<http://wwwiz.com/books/cgi-perl.html>) ËùÕûÀíµÄ CGI ºÍ Perl Êé¼®Ò»ÀÀ±í*£º ¡¾ÒëÕß¡¿´Ë±íÓÉÒëÕ߸üУ¬Í¬Ê±¼ÓÈëÁËÔ­ÎÄ×÷Õß Tom C. µÄ Perl ÊéÆÀ (<http://www.perl.com/perl/critiques/>)ÒÔ¹©¶ÁÕ߲ο¼¡£Çë×¢Ò⣺CGI ¸÷ÊéµÄµÈ¼¶ÆÀ¶¨Ä˵¥¾ÍÆä Perl ²¿·ÝµÄË®×¼¼°ÕýÈ·ÐÔ¶øÑÔ¡£·ûºÅ˵Ã÷£º £¨Tom C. ÓÃÂæÍÕ¶ø·ÇÐÇÐÇ£© Îå¿ÅÐÇ£¨£ª£ª£ª£ª £ª£© ==> ºÜ°ôµÄÊé (terrific books) ËÄ¿ÅÐÇ£¨£ª£ª£ª £ª£© ==> ²»´íµÄÊé (fine books) Èý¿ÅÐÇ£¨£ª£ª£ª£© ==> ÏñÑùµÄÊé (decent books) Á½¿ÅÐÇ£¨£ª£ª£© ==> ÂíÂí»¢»¢¡¢ÃãÇ¿¹ýµÃÈ¥µÄÊé (so-so books) Ò»¿ÅÐÇ£¨£ª£© ==> ²î¾¢µÄÊé (poor books) Áã¿ÅÐÇ£¨0£ª£© ==> ´óÀÃÊé (terrible books) NA ==> ²»ÊÊÓá¢ÎÞÊéÆÀ (Not Applicable/Available) v4 ==> ÄÚÈݽÏÀϾɣ¬½öº¬¸Ç Perl4 ----------------------------------------------------------------------------- +---------------------------------------------------------------------------+ ×÷Õß ÊéÃû ÆÀ¼Û ³ö°æÕß Ëù¸½ ÊÛ¼Û ÈíÌå (ÃÀ½ð) +---------------------------------------------------------------------------+ Effective Perl Programming: 60 Methods Joseph Hall and Rules for Scripting NA Addison-Wesley   $30 Better Programs £¨Ô¤¶¨³ö°æÈÕÆÚ£º1997Äê1 Ô£© +---------------------------------------------------------------------------+ Perl Developer's Toolkit Jon Orwant £¨Ô¤¶¨³ö°æÈÕÆÚ£º1997Äê1 NA Advice Press   $?? Ô£© +---------------------------------------------------------------------------+ Cgi Programming For ?? Dummies NA IDG Books   $25 £¨Ô¤¶¨³ö°æÈÕÆÚ£º1997Äê1 Ô£© +---------------------------------------------------------------------------+ Mastering Regular Expressions: Powerful Jeffrey Friedl Techniques for Perl and NA O'Reilly   $30 Other Tools £¨Ô¤¶¨³ö°æÈÕÆÚ£º1997Äê1 Ô£© +---------------------------------------------------------------------------+ CGI Developer's Resource J.M. Ivler £¨Ô¤¶¨³ö°æÈÕÆÚ£º1997Äê3 NA Prentice Hall ¹â $45 Ô£©µú +---------------------------------------------------------------------------+ Perl 5 Windows NT Various Programming authors £¨Ô¤¶¨³ö°æÈÕÆÚ£º1997Äê1 NA New Riders   $45 Ô£© +---------------------------------------------------------------------------+ Teach Yourself CGI Eric Herrmann Programming with Perl 5 NA Sams.net ¹â $40 in a Week, 2nd Ed. µú +---------------------------------------------------------------------------+ CGI: Internet Mark Felton Programming C++ & C NA Prentice Hall   $?? £¨Ô¤¶¨³ö°æÈÕÆÚ£º1997Äê3 Ô£© +---------------------------------------------------------------------------+ CGI Bible Ed Tittle £¨Ô¤¶¨³ö°æÈÕÆÚ£º1996Äê11 NA IDG Books   $?? Ô£© +---------------------------------------------------------------------------+ Craig Patchett The CGI Cookbook: Perl & and JavaScript NA Wiley ¹â $40 Matt Wright £¨Ô¤¶¨³ö°æÈÕÆÚ£º1996Äê11 µú Ô£© +---------------------------------------------------------------------------+ Drag-n-Drop CGI Baron Weil £¨Ô¤¶¨³ö°æÈÕÆÚ£º1997Äê1 NA Addison-Wesley   $?? Ô£© +---------------------------------------------------------------------------+ Stephen Lines How To Program CGI with NA Ziff-Davis ¹â $40 Perl 5.0 µú +---------------------------------------------------------------------------+ CGI Programming for Bob Denny et Windows ´Å al. £¨Ô¤¶¨³ö°æÈÕÆÚ£º1997Äê3 NA O'Reilly Ƭ $30 Ô£© +---------------------------------------------------------------------------+ Selena Sol & Gunther Instant Web Scripts with NA MIS:Press/M&T ¹â $40 Birznieks CGI/Perl Books µú +---------------------------------------------------------------------------+ Perl For Dummies ?? £¨Ô¤¶¨³ö°æÈÕÆÚ£º1996Äê10 NA IDG Books   $25 Ô£© +---------------------------------------------------------------------------+ Steven E. CGI Web Scripting With Brenner & Perl NA MIS:Press/M&T ¹â $40 Edwin Aoki £¨³ö°æÈ¡Ïû£© Books µú +---------------------------------------------------------------------------+ ?? Perl & CGI Programming NA Sams.net   $45 Starter Kit +---------------------------------------------------------------------------+ Mohammed J. CGI Primer Plus for ¹â Kabir Windows NA Waite µú $45 +---------------------------------------------------------------------------+ Foundations of WWW Ed Tittel et Programming with HTML ¹â al. and CGI, 2nd Ed. NA IDG Books µú $?? £¨Ô¤¶¨³ö°æÈÕÆÚ£º1996Äê10 Ô£© +---------------------------------------------------------------------------+ Jerry Muelver Creating Cool Web Pages £ª£ª IDG Books ¹â $30 With Perl µú +---------------------------------------------------------------------------+ HTML & CGI Unleashed, John December Professional Reference NA Sams.net ¹â $50 Edition µú +---------------------------------------------------------------------------+ Cross-Platform Perl (See also the author's Eric Johnson book site.) £ª£ª MIS:Press/M&T ¹â $35 £¨Ô¤¶¨³ö°æÈÕÆÚ£º1996Äê9 £ª£ª Books µú Ô£© +---------------------------------------------------------------------------+ Daniel Berlin CGI Programming et al. Unleashed NA Sams.net   $55 +---------------------------------------------------------------------------+ Web Client Programming Clinton Wong in Perl NA O'Reilly   $30 £¨Ô¤¶¨³ö°æÈÕÆÚ£º1997Äê3 Ô£© +---------------------------------------------------------------------------+ Perl 5 Robert Seymour £¨Ô¤¶¨³ö°æÈÕÆÚ£º1996Äê10 NA Springer-Verlag   $40 Ô£© +---------------------------------------------------------------------------+ M¡õhe¡õ ¡õ Foghl¡õ Perl 5 Quick Reference £ª Que   $20 +---------------------------------------------------------------------------+ Robert Niles & ¹â Jeffry Dwight CGI by Example NA Que µú $35 +---------------------------------------------------------------------------+ PC Magazine Programming Jonathan Hagey Perl 5.0 CGI Web Pages 0£ª Ziff-Davis ¹â $35 for Microsoft Windows NT µú +---------------------------------------------------------------------------+ Web Programming with ?? Perl NA Sams.net ¹â $40 £¨Ô¤¶¨³ö°æÈÕÆÚ£º1996Äê8 µú Ô£© +---------------------------------------------------------------------------+ Perl 5 Developer's Guide ?? £¨Ô¤¶¨³ö°æÈÕÆÚ£º1996Äê12 NA Sams ¹â $60 Ô£©µú +---------------------------------------------------------------------------+ David Medinets Perl 5 by Example £ª£ª Que ¹â $35 +1/2 µú +---------------------------------------------------------------------------+ Stephen Asbury ¹â et al. CGI How-To NA Waite µú $40 +---------------------------------------------------------------------------+ Visual Basic Internet Carl Franklin Programming NA Wiley ¹â $40 £¨Ô¤¶¨³ö°æÈÕÆÚ£º1996Äê5 µú Ô£© +---------------------------------------------------------------------------+ InfoMagic Mother of Perl NA SSC ¹â $35 £¨Ò»Ì×Á½ÕŹâµú£©µú +---------------------------------------------------------------------------+ Larry Wall et Programming Perl, 2nd £ª£ª al. Ed. £ª£ª O'Reilly   $40 £ª +---------------------------------------------------------------------------+ CGI Developer's Guide Eugene Kim £¨Ô¤¶¨³ö°æÈÕÆÚ£º1996Äê5 £ª£ª Sams.net ¹â $50 Ô£©µú +---------------------------------------------------------------------------+ Robert CGI Manual of Style McDaniel £¨¿É×ÔÏßÉÏÈ¡µÃ£© NA Ziff-Davis   $25 +---------------------------------------------------------------------------+ ?? Perl 5 for Dummies NA IDG Books ¹â $?? £¨Ô¤¶¨³ö°æÈÕÆÚ£º1996Ä꣩µú +---------------------------------------------------------------------------+ Developing Internet Ginsburg & Applications with Perl Tall £¨Ô¤¶¨³ö°æÈÕÆÚ£º1996Äê3 NA Sams.net   $?? Ô£© +---------------------------------------------------------------------------+ Kamran Husain Perl 5 Unleashed £ª Sams ¹â $50 µú +---------------------------------------------------------------------------+ Teach Yourself Perl 5, Norton 2nd Edition NA Sams ¹â $?? £¨Ô¤¶¨³ö°æÈÕÆÚ£º1996Äê6 µú Ô£© +---------------------------------------------------------------------------+ Michael Erwin Special Edition Using et al. CGI NA Que   $60 +---------------------------------------------------------------------------+ Thomas Boutell CGI Programming in C and £ª£ª Addison-Wesley ¹â $35 Perl £ª£ªµú +---------------------------------------------------------------------------+ Perl 5 Desktop Reference Johan Vromans £¨¿É×Ô CPAN ÏÂÔØԭʼÂë×Ô NA O'Reilly   $7 ÐÐÓ¡ÖÆ£© +---------------------------------------------------------------------------+ Christian The Webmaster's Neuss & Handbook: Perl Power for NA Int'l Thomson ¹â $30 Johan Vromans Your Web Server µú +---------------------------------------------------------------------------+ William E. ¹â Weinman The CGI Book NA New Riders µú $45 +---------------------------------------------------------------------------+ World Wide Web Database Brian Jepson Programming for Windows NA Wiley ¹â $40 NT µú +---------------------------------------------------------------------------+ Steven E. £ª£ª Brenner & Introduction to CGI/ £ª MIS:Press/M&T   $20 Edwin Aoki Perl (v4) Books +---------------------------------------------------------------------------+ Ed Tittel & Perl 5 Programming Sebastion Secrets NA IDG Books ¹â $45 Hassinger £¨Ô¤¶¨³ö°æÈÕÆÚ£º1996Äê3 µú Ô£© +---------------------------------------------------------------------------+ David Harlan Special Edition Using et al. Perl 5 for Web £ª£ª Que   $50 Programming +---------------------------------------------------------------------------+ Shishir CGI Programming on the ¹â Gundavaram World Wide Web £ª£ª O'Reilly µú $30 +---------------------------------------------------------------------------+ The Official 60 Minute £ª£ª Robert Farrel Guide to CGI Programming £ª IDG Books   $20 with Perl (v4) +---------------------------------------------------------------------------+ Ed Tittel & Sebastion Web Programming Secrets NA IDG Books ¹â $45 Hassinger with HTML, CGI & Perl µú +---------------------------------------------------------------------------+ John Deep & Peter Developing CGI £ª£ª Wiley   $30 Holfelder Applications with Perl (v4) +---------------------------------------------------------------------------+ Jon Orwant Perl 5 Interactive £ª£ª Waite ¹â $50 Course £ªµú +---------------------------------------------------------------------------+ Aidan Humphreys et Perl 5 How-To £ª£ª Waite ¹â $45 al. £ªµú +---------------------------------------------------------------------------+ Teach Yourself CGI £ª£ª Eric Herrmann Programming with Perl in £ª Sams.net ¹â $40 a Week (v4) µú +---------------------------------------------------------------------------+ Perl Walnut Creek £¨ÊÕ¼¸÷Àà×ÊÔ´¡¢µµ°¸¿â¡¢ NA Walnut Creek ¹â $40 ÈëÃÅÖ¸µ¼¡¢ÊµÀý¡¢source µú code µÈ£© +---------------------------------------------------------------------------+ Software Engineering Carl Dichter & with Perl ´Å Mark Pease £¨½ø½×¼¶£¬ÄÚÈÝÕë¶ÔÈíÌåר£ª Prentice Hall Ƭ $30 ÒµÈËÊËÉè¼Æ£»·ÇÈëÃÅÖ¸Òý£© +---------------------------------------------------------------------------+ £ª£ª Ellie Quigley Perl by Example £ª Prentice Hall   $27 (v4) +---------------------------------------------------------------------------+ John December & HTML & CGI Unleashed NA Sams.net ¹â $50 Mark Ginsburg µú +---------------------------------------------------------------------------+ David Till Teach Yourself Perl in £ª£ª Sams   $30 21 Days (v4) +---------------------------------------------------------------------------+ Larry Wall & Randal L. Programming Perl NA O'Reilly   $30 Schwartz +---------------------------------------------------------------------------+ Randal L. £ª£ª Schwartz Learning Perl £ª£ª O'Reilly   $25 (v4) +---------------------------------------------------------------------------+ Ed Tittel et Foundations of WWW ¹â al. Programming with HTML NA IDG Books µú $40 and CGI +---------------------------------------------------------------------------+ How To Set Up and Maintain a World Wide Lincoln Stein Web Site, 2nd Edition £ª£ª Addison-Wesley ¹â $55 £¨Ô¤¶¨³ö°æÈÕÆÚ£º1997Äê1 £ª£ªµú Ô£© +---------------------------------------------------------------------------+ ----------------------------------------------------------------------------- ¡¾ÒëÕß¡¿ÖÐÎÄµÄ CGI ºÍ Perl µÄÊéÄ¿ÔòÉдýºÃÐĵĶÁÕßÌṩ¡£ --------------------------------------------------------------------------- Q1.6: ÊDz»ÊÇÓÐÕâ·½ÃæµÄÓʵÝÂÛ̳ (mailing lists) »òÐÂÎÅÌÖÂÛȺ×飿 ÿÌìÓÐÎÞÊýµÄ CGI ¸ßÊÖÔÚ¡¸¼àÊÓ¡¹ÖøÕâ¸öºÜÓÐÓõÄÐÂÎÅÌÖÂÛȺ×飺 comp.infosystems.www.authoring.cgi*¡£ ¡¾ÒëÕß¡¿ÖÐÎĵÄÌÖÂÛȺÖÐ×îÊʺÏ×öÕâ·½ÃæµÄÌÖÂ۵Ĵó¸ÅÒªËãÊÇ tw.bbs.comp.www ÁË¡£»¹ÓУ¬Çë²»Òª½« CGI ÎÊÌâ post µ½ comp.lang.perl.misc ÉÏÍ·È¥£¬±ð˵ÒëÕßûÏȾ¯¸æÄúภ;-) ¡£ µ«ÊÇ£¬³ý·ÇÄúÏÈ°Ñ FAQ ¶Á¹ýÒ»±é£¬·ñÔòÄú²»Ó¦¸Ãµ½Õâ¸ö°æ×ÓÉÏÈ¥Ëæ±ã post ÎÊÌâ £¨»òÆäËûÈκΰæ×Ó£»Í¬ÑùµÄµÀÀí£©¡£ Íø·ÉÏÓи÷ʽ¸÷ÑùÌÖÂÛ Perl¡¢CGI£¬¼° Web µÄÓʵÝÂÛ̳ (<http://perl.com/perl/info/mailing-lists.html>)£¬µ«ÒÔµ×ÏÂÕâÁ½¸ö×îÊÜ»¶Ó­£º cgi-perl-request@webstorm.com ¡¾Hypermail µµ°¸¿â¡¿ (<http://www.webstorm.com/local/cgi-perl>) Õâ¸öÂÛ̳ÊǸø·¢Õ¹ Perl5 CGI Ä£×é¡¢»ò¶Ô´ËÓÐÐËȤµÄÈËÓõġ£Ëü²¢²»ÌṩÈÎ ºÎ CGI Ö§Ô®*¡£ ¡¾ÒëÕß¡¿´Ë mailing list ÒѾ­ÊýÔÂûÓÐ posts £¬ÊÙÖÕÕýÇÞÁË¡£Õâ ¿ÉÄÜÊÇÓÉì¶ÌÖÂÛµÄÖ÷Ì⣭CGI::* Ä£×éÌ×¼þ£¨Ïê¼ûÒÔϵڶþƪ ¡¸Modules £¨Ä£×飩¡¹µÄ˵Ã÷£©ÒѾ­ÓкÃÒ»¶Îʱ¼äûÓиüÐÂÁË¡£Ä£ ×éµÄά»¤ÈË Lincoln Stein ½üÒ»Äê¶àÀ´Ëƺõ¶¼½«ÖØÐÄ·ÅÔÚËûµÄ CGI.pm ÉÏ£¬¶ø CGI.pm Ҳȡ´úÁË CGI::* Õâ¸öÌ×¼þµÄµØλ¡£¶ÁÕßÈç ¶Ô CGI::* »ò CGI.pm ÓÐÎÊÌâµÄ»°¿Éдµ½ comp.lang.perl.modules ÉÏÍ·È¥¡£ libwww-perlrequest@ics.uci.edu ¡¾Hypermail µµ°¸¿â¡¿ (<http://www.ics.uci.edu/WebSoft/libwww-perl/archive>) libwww-perl Õâ¸ö library Ì×¼þΪ Web ¿Í»§¼°ËÅ·þ³ÌʽÉè¼ÆÌṩÁËÒ»Ì×¼ò±ãµÄ ½éÃæ*¡£ ¡¾ÒëÕß¡¿´Ë mailing list ²»ÊʺÏ×ö CGI µÄÌÖÂÛ¡£libwww-perl ÊÇ Ò»ÏîÒÔ Perl Ϊ·¢Õ¹¹¤¾ß¡¢ÀàËÆ W3C/CERN µÄ libwww (<http://www.w3.org/pub/WWW/Library/>) µÄ¼Æ»­£¨ááÕßÓõÄÊÇ C £©¡£ Äú¿ÉÒÔÔÚ£º http://www.ics.uci.edu/pub/websoft/libwww-perl (<http://www.ics.uci.edu/pub/websoft/libwww-perl>)È¡µÃ¾ÉµÄ (Perl4) libwww-perl µÄÏà¹Ø×ÊÁÏ¡£ ×îÐ嵀 Perl5 libwww Ì×¼þ¿ÉÔÚ£º http://www.sn.no/~libwww-perl/ (<http://www.sn.no/libwww-perl/>) ´¦È¡ µÃ¡£ CPAN: Perl Ä£×éͬʱҲ¿ÉÒÔÔÚ¶àÖظ´ÖÆ¡¢·ÖɢʽµÄ CPAN (<http://www.perl.com/CPAN/README.html>) ϵͳȡµÃ¡£Õâ»á×Ô¶¯ÌæÄúÑ¡ÔñÒ»¸ö¡¸ ¿¿½üÄúµÄÕ¾ (<http://www.perl.com/perl/news/cpan-mux.html>)¡¹¡£Æ©Èç˵£¬Äú¿É ÒÔ×¥ LWP Ä£×éµÄ source (<http://perl.com/cgi-bin/cpan_mod?module=LWP>) »òÖ» ×¥ËüµÄ readme (<http://perl.com/cgi-bin/cpan_mod?module=LWP&readme=1>)¡£ --------------------------------------------------------------------------- Q1.7: Íø·ÉÏÊDz»ÊÇÓÐרÃÅÊÕ²ØÓʵÝÂÛ̳»òÐÂÎÅÌÖÂÛȺ×éµÄ posts µÄÕ¾£¿ Êǵģ¬Äú¿ÉÒÔÊÔÊÔ The Usenet Newstand (<http://CriticalMass.com/Concord/>)¡£ ËùÓÐcomp.infosystems.www.* ÌÖÂÛȺ×éµÄÎÄÕ¶¼ÊÕ¼¯ÔÚÄǶù*¡£Í¬Ê±£¬cgi-perl ¼° libwww ÕâÁ½¸öÓʵÝÂÛ̳ËûÃÇÒ²ÓÐÊÕ¼¯¡£ ¡¾ÒëÕß¡¿Äúͬʱ¿ÉÒÔÊÔÊÔ DejaNews (<http://www.dejanews.com/>) ¡¢ AltaVista (<http://www.altavista.digital.com/>) £¬ºÍ HotBot (<http://www.hotbot.com/>) Õ⼸¸öËÑÑ°ÒýÇ棯×ÊÁÏ¿â¡£ DejaNews (<http://www.dejanews.com/>) ÉõÖÁÁ¬ tw.bbs.* ¶¼ÊÕ¼¯£¨ÆäËûÁ½¸ö»ò ÐíÒ²ÓУ¬µ«ÒëÕßûÊÔ¹ý£©¡£ --------------------------------------------------------------------------- ·¢ÐÅÈË: hsiao@uconnvm.uconn.edu, ÐÅÇø: programming ±ê Ìâ: Perl-CGI ³£ÎÊÎÊÌ⼯ -- part2 ·¢ÐÅÕ¾: 2 Tigers (Fri Jun 20 16:02:09 1997) תÐÅÕ¾: cis_nctu!news.cis.nctu!ctu-peer!news.nctu!serv.hinet.net!netnews.hinet À´ Ô´: times.oneway.net ±¾ Perl-CGI ³£ÎÊÎÊÌ⼯´¿ÎÄ×Ö°æÒÔ²»¶¨ÆÚ·½Ê½ÔÚ tw.bbs.comp.www ¼° tw.bbs.comp.language Á½¸öÐÂÎÅÌÖÂÛȺ×éÉϵÇÔØ¡£±¾Îĵij¬Îļþ°æ¼°½ñááµÚÒ»ÊÖ Ð°æ×ÊÁÏ¿É×ÔÁ½Ö»ÀÏ»¢¹¤×÷ÊÒÈ¡µÃ£¬Î»Ö·Îª£º <http://www.opim.uconn.edu/~2T/perl/perl-cgi-faq-chi/>¡£±¾´¿ÎÄ×Ö°æÖÐ ÄÚÈÝÈôÓкͳ¬Îļþ°æ²»Ò»ÖÂÖ®´¦£¬Ò»ÇоùÒÔááÕßΪ׼¡£±¾ FAQ TANet µÄ mirror £¨Õâ¸öÓ³Éäµã¿ÉÄÜ»¹ÒªµÈ¼¸Ìì²Å»á×¼±¸ºÃ£©Î»ì¶£º ftp://www.math.ncu.edu.tw/pub/staff/chenym/FAQ/Perl/ (anonymous FTP) ºÍ http://www.math.ncu.edu.tw/~chenym/FAQ/Perl/¡£Ì¨ÍåÉÌÒµÍø·µÄ mirror ÔòÔÚ http://www.oneway.net/FAQ/perl/¡£ ÒëÕß±àд±¾ FAQ£¨³£ÎÊÎÊÌ⼯£©Ö®¶¯»ú£¨ºÍÆäËûÐÂÎÅȺ×é FAQ µÄÄ¿µÄ²¢ÎÞ¶þÖ£©£¬ ÄËÓмøì¶ÏàͬµÄÎÊÌâÔÚÌÖÂÛȺÖÐÒ»ÔÙ³öÏÖ£¬Í½Ôö´ó¼ÒÈËÁ¦¡¢Æµ¿í£¬¼°Ê±¼äÉϵÄÀË·Ñ£¬ Òò´ËÏ£Íû½å´Ë FAQ £¬¶ÔÐí¶à·´¸²³öÏÖµÄÎÊÌâÌá³öÕýÈ·µÄ½â´ð£¬²¢¼õÉÙ´íÎó×ÊѶµÄ Á÷´«¡£ËùÒÔÒëÕßÔڴ˺ôÓõÌÖÂÛȺÖеÄÐÂÏÊÈËÔÚѯÎÊÓÐ¹Ø Perl-CGI Ò»ÀàÎÊÌâ֮ǰ£¬ ÎñÇë×ñÊØÍø·Àñ½Ú (netiquette)£¬ÏÈ×Ô±¾ FAQ ÖÐÕÒÑ°½â´ð¡£´ËÍâ±¾ FAQ ÒàÊÊºÏ ×÷Ϊ¸öÈ˽øÐÞ Perl-CGI ³ÌʽÉè¼ÆµÄ²ÄÁÏ¡£ Ϊ·½±ã´¿ÎÄ×Ö°æ¶ÁÕßÆð¼û£¬ËùÓг¬Îļþ°æÖÐËùº¬µÄ http ¼° ftp Á¬½Úµã¾ù¸½¼Óì¶ Êʵ±´¦À¨ºÅÖУ¬µ«Î¨¶À Q1.5 ÖеÄÊéÄ¿±íÀýÍ⣨Òò±àÅŲ»Ò×£©¡£Èô¶Ô±¾ÖÐÎÄ°æ³£ÎÊ ÎÊÌ⼯ÓÐÈκÎÎÊÌâ¼°Òâ¼û£¬Çë email ÒëÕß¡£ Ô­ÎÄ°æ¡¢ÖÐÒë°æ°æȨÊÂÒË£¬Ïê¼ûµÚ 5 ²¿·ÝÎĵף¨¸´ÖÆ¡¢×ªÔØÕßÎñ±Ø×ñÊØ£©¡£ --Ïô°ÙÁä Pai-ling Hsiao <hsiao@uconnvm.uconn.edu> -------- 8< ------- Cut Here ------- 8< -------- 2.0 - Modules £¨Ä£×飩 --------------------------------------------------------------------------- Q2.1: ÎÒÊDz»ÊǸÃÓà Perl CGI modules* À´Ð´ CGI scripts £¿×Ô¼º×ö²»ÊDZȽÏÈÝÒ× Â𣿠¡¾ÒëÕß¡¿ CGI modules ÔÚ´ËÖ¸ CGI.pm ¼°ÆäËû CGI::* µÄÄ£×飻Ïê¼û Q2.4 ¡£ ÕâÍêÈ«È¡¾öì¶ÄúÒª×öµÄÊÇʲ÷á¡£CGI modules ½ÏÊʺÏÖØÁ¿¼¶µÄ CGI scripts ¡£Èç¹û ÊǼòµ¥µÄ scripts µÄ»°£¬×Ô¼º×ö»òÕßÊÇÓà CGI Lite Õâ¸öÄ£×é»á¼ò±ãÐí¶à¡£Èç¹ûÄú ÕæµÄÔ¸Ò⣬ÄúÉõÖÁ¿ÉÒÔÓÃ¾ÉµÄ Perl4 cgi-lib.pl Õâ¸ö library*¡£ ¡¾Òë×¢¡¿×÷Õß Tom C. Ö®ËùÒÔÕâ÷á˵ÊÇÒòΪËû»ù±¾Éϲ»ÔÞ³ÉʹÓà cgi-lib.pl £¬ÓÐÐËȤµÄ¶ÁÕß¿ÉÒÔ¿´ËûдµÄ Why I am not fond of cgi-lib.pl (<http://www.perl.com/perl/info/www/!cgi-lib.html>) £¨ÎªÊ²÷áÎÒ²»ÐÀÉÍ cgi-lib.pl £©¡£ --------------------------------------------------------------------------- Q2.2: ÈçºÎÖªµÀij¸ö module ¸ÃÔõ÷á¸öÓ÷¨£¿ ´ó¶àÊý modules µÄʹÓÃ˵Ã÷ÊÇÖ±½ÓǶÔÚ³ÌʽÀïµÄ*¡£Èç¹ûÊÇÕâÑùµÄ»°£¬Äú¿ÉÒÔÓà pod2man Õâ¸ö script À´ÔĶÁʹÓÃÖ¸ÄÏ£º % pod2text name_of_module.pm % pod2man name_of_module.pm | nroff -man | more ¡¾Òë×¢¡¿Èç¹ûÄúÔÚ *.pm µµÖп´µ½ ``=head1''¡¢``=cut'' ÕâÀàµÄ¶«Î÷±ã ÊÇÏÔʾʹÓÃ˵Ã÷Óи½ÔÚÀïÍ·¡£ÕâÊÇ Larry Wall Éè¼ÆµÄ POD (Plain Old Document) ¸ñʽ¡£Ïê¼û perlpod manpages¡£ --------------------------------------------------------------------------- Q2.3: ÓÐÄÄЩ Perl4 µÄ CGI »ò WWWµÄ libraries £¿ ×î¹ãΪʹÓÃµÄ Perl4 CGI library ÊÇ Steve Brenner µÄ cgi-lib.pl (<http://www.bio.cam.ac.uk/web/form.html>)¡£ --------------------------------------------------------------------------- Q2.4: ÓÐÄÄЩ¸ø Perl5 ʹÓÃµÄ CGI modules£¿ÎÒ¸ÃÓÃÄĸö£¿ÎªÊ²÷᣿ CGI.pm (<http://www-genome.wi.mit.edu/ftp/pub/software/WWW/CGI.pm.tar.gz>)* Õâ¸öºÜ°ôµÄ module ÔÚ¹¦ÄÜÉϺͲ¿·Ö CGI::* modules ÏàÖصþ¡£Èç¹ûÄú²»ÏëºÍ¶à ÖØ modules ´ò½»µÀµÄ»°£¬Äú¿ÉÒÔÖ»ÓÃÕâÒ»¸ö¡£ÎÒÃǵÈÒ»ÏÂÔÚááÍ·»á¸øÄú¿´Ò»¸ö Óà CGI.pm À´Ìæ CGI scripts ³ý´íµÄʵÀý¡£ ´ËÍ⣬Lincoln £¨Lincoln Stein£»CGI.pm µÄ×÷Õߣ©»¹Ð´ÁËÒ»±¾ÌÖÂÛ Web ºÍ CGI ·Ç³£ºÃµÄÊ飬½Ð How To Set Up and Maintain a World Wide Web Site (<http://www.aw.com/cp/Stein2e.html>)¡£ ¡¾ÒëÕß¡¿½¨Òé¶ÁÕ߶àʹÓà CGI.pm ¡£CGI::* ÒѾ­Óкܳ¤Ò»¶Îʱ¼äû ÓиüÐÂÁË£¬¶ø CGI.pm Ôò²»¶ÏµÄÓиÄÁ¼µÄаæÍƳö£¬²¢ÒѼÓÈë¶Ô FastCGI (<http://www.fastcgi.com/>) µÄÖ§Ô®£¬Òò´Ë¶Ô FastCGI µÄʹÓÃÕßÒ²Ï൱·½±ã¡£´ËÍ⣬mod_perl (<http://www.osf.org/~dougm/apache/>) £¨Apache (<http://www.apache.org/>) µÄ perl module£©ÖÐËù¸½µÄ CGI::XA £¬Ò²ÊÇÓÉ CGI.pm ¸Ä½ø¶øÀ´µÄ¡£ CGI.pm ÈÃʹÓÃÕßÃâ³ý×Ô¼º×öµÍ½××Ê ÁÏ´¦Àí£¨ÈçÓ¡ HTTP ±êÍ·¡¢Ìæ form, cookies ×ÊÁϽâÂëµÈ£©µÄÂé ·³¡£ CGI::* Ä£×éÌ×¼þ (<http://www-genome.wi.mit.edu/ftp/pub/software/WWW/CGIperl/>) ÕâЩģ×éÔ­ÏÈ´ó¶àÊÇÓÉ Tim Bunce Ëùд£¬ÏÖÔÚÔòÓÉ Lincoln Stein À´¸ºÔðά »¤¡£ËüÃǵŦÄÜ°üÀ¨ÁË°ïÄú²úÉú¼°´¦Àí form £¬Ìæ CGI ³Ìʽ³ý´í£¬ÒÔ¼°ÔÚ²»Í¬ µÄ forms Ö®¼äάϵһ¸ö״ֵ̬¡£ CGI Lite (<http://dongpo.math.ncu.edu.tw/perl/CPAN/authors/id/SHGUN/CGI_Lite-1.62.pm.gz>) Õâ¸öÇáÁ¿¼¶µÄ module ÊÇ CGI::* modules Ö®ÍâµÄÁíÒ»¸öÑ¡Ôñ¡£Ëü¿ÉËãÊÇÔÚÀÏ¾É µÄ cgi-lib.pl (<http://www.bio.cam.ac.uk/web/form.html>) Ö®ÉϸÄÁ¼¡¢¼Ó ÁÏááµÄ²úÎï¡£ ÒÔÉÏÈýÕß¾ùÓÐÄÜÁ¦´¦Àí multipart form ×ÊÁÏ£¨¼´ÉÏ´«£©¡£ --------------------------------------------------------------------------- Q2.5: Ϊʲ÷áÕâ÷á¶à perl CGI libraries ¶¼ÊÇ object-oriented £¨Îï¼þµ¼Ïò£©µÄ£¿ ÎÒ¶ÔÓÃÎï¼þµ¼ÏòÀ´Ð´³ÌʽһÇϲ»Í¨¡£ÄѵÀ¾ÍûÓмòµ¥Ò»µã£¬¸ø²»»áÎï¼þµ¼ÏòµÄÈËʹÓà µÄ libraries Âð£¿Îï¼þµ¼ÏòÓжàÄÑ£¿ ÆäʵʹÓÃÎï¼þµ¼ÏòµÄ modules ²¢²»ÄÑ¡£ÏÈÈ¥¿´¿´ Tom Christiansen µÄ Easy Intro to Using Perl Objects (<http://perl.com/perl/everything_to_know/easy_objects.html>) £¨¼òµ¥½éÉÜÈç ºÎʹÓà Perl Îï¼þ£©°É¡£ Ç°Í·Ìáµ½µÄ CGI modules ÓÃÆðÀ´ÕæµÄÊǺÜÈÝÒ×£¡ÕâÀïÓÐÒ»¸öÓà CGI Lite Ó¡³ö form ×ÊÁϵÄʵÀý*£º ¡¾ÒëÕß¡¿CGI.pm µÄʹÓÃ˵Ã÷ (<http://www-genome.wi.mit.edu/ftp/pub/software/WWW/>)ÖÐÓиü¶à¡õ Àý¿É¹©²Î¿¼¡£ #!/usr/local/bin/perl -w use CGI_Lite; $cgi = new CGI_Lite; %data = $cgi->parse_form_data; print "Content-type: text/plain", "\n\n" foreach $key (keys %data) { print $key, " = ", $data{$key}, "\n" } exit 0; ҪעÒâµÄÊÇ£¬¼´Ê¹Äú»úÆ÷É쵀 Perl ²»ÊÇÄú×°µÄ£¨Äú²»ÊÇϵͳ¹ÜÀíÕߣ©£¬»òÊÇÄúȨÏÞ ²»¹»ÎÞ·¨½«ÕâЩ modules ºÍÆäËûµÄ Perl library µµ°¸°²×°ÔÚͬһ¸öµØ·½£¬Äú»¹ÊÇ ¿ÉÒÔʹÓÃÕâЩ modules µÄ -- Ö»Òª½«ËüÃÇ°ÚÔÚÒ»¸ö×Ô¼º·½±ãµÄµØ·½£¬È»ááÔÚÄúµÄ script ¿ªÊ¼´¦¼ÓÉÏ*£º BEGIN { unshift @INC, "/your/dir/favorite/place" } ¡¾ÒëÕß¡¿½¨Òé¸ÄÓýÏеķ½Ê½£º use lib qw(/your/dir/favorite/place); ºÃ¡£ÒÔÏÂÊÇÒ»¸öÓõ½ CGI::* modules µÄÀý×Ó£º #!/usr/local/bin/perl -w use CGI::Form; $cgi_form = new CGI::Form; print <<'End_of_Header'; <HTML> <HEAD><TITLE>¿´ÖøÎÒ×ö£¡</TITLE></HEAD> <BODY> <H1>¿´ÖøÎÒ×ö£¡</H1> End_of_Header print $form->startform; ## ²úÉúÒ»¸öµ¥ÐÐÊäÈëÀ¸Î» (text field) print 'ÐÕÃû£º '; print $form->textfield('name'), "<BR>\n" ## ²úÉúÒ»×鵥ѡ°´Å¦ (radio buttons) print '<P>ÄúסÄĶù£º <BR>'; print $form->radio_group(-name => 'where', -values => ['±±ÃÀÖÞ', 'ÄÏÃÀÖÞ', 'Å·ÖÞ', '°ÄÖÞ', 'ÑÇÖÞ', 'Äϼ«ÖÞ'], -default => '±±ÃÀÖÞ', -linebreak => 'true'); ## ²úÉúÒ»¸ö¶àÐÐÊäÈëÀ¸Î» (textarea) print 'Òâ¼û£º '; print $form->textarea('comments', undef, 5, 40); print "<P>" print $form->reset; print $form->defaults; print $form->submit('Ëͳö£¡', 'Submit'); print $form->endform; print "</BODY></HTML>" reset¡¢defaults£¬¼°submitÈýÖÖ·½·¨( methods ) »á²úÉú²»Í¬µÄÀàÐ͵İ´Å¥¡£reset Õâ¸ö°´Å¥ÈÃÄú°ÑÄ¿Ç° form À¸Î»ÖÐËùÌîµÄ×ÊÁÏÏ´µô£¬²¢ÏÔʾÊôì¶ÉÏÒ»¸ö״̬£¨»ò»Ø ºÏ£©µÄ×ÊÁÏ¡£ defaults Õâ¸ö°´Å¥ÔòÊǽ«form À¸Î»×ÊÁϳ¹µ×Ï´È¥¡£»¹ÓÐ submit Õâ ¸ö·½·¨²úÉúÒ»¸öËͳöÅ¥ÒÔ±ãÈÃÈ˽«×ÊÁÏËÍÖÁ server ´¦¡£ Äú¿´£¬²»ÊǺÜÈÝÒ×Â𣿠·¢ÐÅÈË: hsiao@uconnvm.uconn.edu, ÐÅÇø: programming ±ê Ìâ: Perl-CGI ³£ÎÊÎÊÌ⼯ -- part3 ·¢ÐÅÕ¾: 2 Tigers (Fri Jun 20 16:02:11 1997) תÐÅÕ¾: cis_nctu!news.cis.nctu!news.csie.nctu!aidebbs!news.yzit!news.ncu!news. À´ Ô´: times.oneway.net ±¾ Perl-CGI ³£ÎÊÎÊÌ⼯´¿ÎÄ×Ö°æÒÔ²»¶¨ÆÚ·½Ê½ÔÚ tw.bbs.comp.www ¼° tw.bbs.comp.language Á½¸öÐÂÎÅÌÖÂÛȺ×éÉϵÇÔØ¡£±¾Îĵij¬Îļþ°æ¼°½ñááµÚÒ»ÊÖ Ð°æ×ÊÁÏ¿É×ÔÁ½Ö»ÀÏ»¢¹¤×÷ÊÒÈ¡µÃ£¬Î»Ö·Îª£º <http://www.opim.uconn.edu/~2T/perl/perl-cgi-faq-chi/>¡£±¾´¿ÎÄ×Ö°æÖÐ ÄÚÈÝÈôÓкͳ¬Îļþ°æ²»Ò»ÖÂÖ®´¦£¬Ò»ÇоùÒÔááÕßΪ׼¡£±¾ FAQ TANet µÄ mirror £¨Õâ¸öÓ³Éäµã¿ÉÄÜ»¹ÒªµÈ¼¸Ìì²Å»á×¼±¸ºÃ£©Î»ì¶£º ftp://www.math.ncu.edu.tw/pub/staff/chenym/FAQ/Perl/ (anonymous FTP) ºÍ http://www.math.ncu.edu.tw/~chenym/FAQ/Perl/¡£Ì¨ÍåÉÌÒµÍø·µÄ mirror ÔòÔÚ http://www.oneway.net/FAQ/perl/¡£ ÒëÕß±àд±¾ FAQ£¨³£ÎÊÎÊÌ⼯£©Ö®¶¯»ú£¨ºÍÆäËûÐÂÎÅȺ×é FAQ µÄÄ¿µÄ²¢ÎÞ¶þÖ£©£¬ ÄËÓмøì¶ÏàͬµÄÎÊÌâÔÚÌÖÂÛȺÖÐÒ»ÔÙ³öÏÖ£¬Í½Ôö´ó¼ÒÈËÁ¦¡¢Æµ¿í£¬¼°Ê±¼äÉϵÄÀË·Ñ£¬ Òò´ËÏ£Íû½å´Ë FAQ £¬¶ÔÐí¶à·´¸²³öÏÖµÄÎÊÌâÌá³öÕýÈ·µÄ½â´ð£¬²¢¼õÉÙ´íÎó×ÊѶµÄ Á÷´«¡£ËùÒÔÒëÕßÔڴ˺ôÓõÌÖÂÛȺÖеÄÐÂÏÊÈËÔÚѯÎÊÓÐ¹Ø Perl-CGI Ò»ÀàÎÊÌâ֮ǰ£¬ ÎñÇë×ñÊØÍø·Àñ½Ú (netiquette)£¬ÏÈ×Ô±¾ FAQ ÖÐÕÒÑ°½â´ð¡£´ËÍâ±¾ FAQ ÒàÊÊºÏ ×÷Ϊ¸öÈ˽øÐÞ Perl-CGI ³ÌʽÉè¼ÆµÄ²ÄÁÏ¡£ Ϊ·½±ã´¿ÎÄ×Ö°æ¶ÁÕßÆð¼û£¬ËùÓг¬Îļþ°æÖÐËùº¬µÄ http ¼° ftp Á¬½Úµã¾ù¸½¼Óì¶ Êʵ±´¦À¨ºÅÖУ¬µ«Î¨¶À Q1.5 ÖеÄÊéÄ¿±íÀýÍ⣨Òò±àÅŲ»Ò×£©¡£Èô¶Ô±¾ÖÐÎÄ°æ³£ÎÊ ÎÊÌ⼯ÓÐÈκÎÎÊÌâ¼°Òâ¼û£¬Çë email ÒëÕß¡£ Ô­ÎÄ°æ¡¢ÖÐÒë°æ°æȨÊÂÒË£¬Ïê¼ûµÚ 5 ²¿·ÝÎĵף¨¸´ÖÆ¡¢×ªÔØÕßÎñ±Ø×ñÊØ£©¡£ --Ïô°ÙÁä Pai-ling Hsiao <hsiao@uconnvm.uconn.edu> -------- 8< ------- Cut Here ------- 8< -------- 3.0 - CGI Óë WWW Server --------------------------------------------------------------------------- Q3.1: ÎÒµÄ Perl CGI ³ÌʽҪ·ÅÔÚÄÄÀïÖ´ÐУ¿cgi-bin Õâ¸öĿ¼ÊÇ×öÉõ÷áÓõģ¿ server ͨ³£ÊÇÉ趨³ÉÈ¥Ö´ÐзÅÔÚ ``cgi-bin'' Ŀ¼µ×Ï嵀 CGI ³Ìʽ¡£²»¹ý£¬ server ¹ÜÀíÕßͬʱҲ¿ÉÒÔÔÚÉ趨µµÖÐÉ趨 aliases£¬ÈÃËùÓк¬Ä³Ð©¸±µµÃû£¨Èç ..cgi ¡¢.pl£©µÄ scripts ¶¼ÄÜÖ´ÐÐ*¡£ ¡¾ÒëÕß¡¿É趨 CGI aliases ºÍ¸±µµÃû¹ÌÈ»ºÜ·½±ã£¬µ« server ¹ÜÀíÕßÐë×¢ Òâµ½Ïà¹ØµÄ°²È«ÎÊÌâ¡£ --------------------------------------------------------------------------- Q3.2: ʲ÷áÊǵµ°¸Ê¹ÓÃȨÏÞ£¿ÔõÑù¸Ä£¿ µµ°¸È¨ÏÞÊǸù¾ÝʹÓÃÕßʶ±ð´úºÅ£¨ÓÖ³Æ uid£©£¬ÒÔ¼°ËûÃÇËùÊôµÄÍÅÌåÀ´¾ö¶¨ÊÇ·ñ¸³Óë ʹÓÃÕ߶Á¡¢Ð´£¬ºÍÖ´ÐÐijµµ°¸µÄȨÀû¡£Äú¿ÉʹÓà chmod Õâ¸öÖ¸ÁîÈ¥Ð޸ĵµ°¸µÄʹÓà ȨÏÞ¡£ÀýÈ磺 % ls -ls form.cgi 1 -rwx------ 1 shishir 974 Oct 31 22:15 form.cgi* ´ËÒ»µµ°¸µÄȨÏÞΪ 0700£¨°Ë½øÖÆ£©£¬ÒâζÖøûÓÐÈË£¨µµ°¸ËùÓÐÈ˳ýÍ⣩Äܹ»¶ÁÈ¡¡¢ д½ø£¬»òÖ´ÐÐÕâ¸öµµ°¸¡£ÎÒÃÇ¿ÉÒÔÓà chmod Õâ¸öÖ¸ÁîÈ¥ÐÞ¸ÄËüµÄȨÏÞ£º % chmod 755 form.cgi % ls -ls form.cgi 1 -rwxr-xr-x 1 shishir 974 Oct 31 22:15 form.cgi* ÕâÑùÒ»À´£¬È¨Ï޾ͱäÁË¡£ÏÖÔÚºÍ ``shishir'' ÔÚͬһ¸öÍÅÌåµÄʹÓÃÕߣ¬»¹ÓÐÆäËûÈÎ ºÎµÄʹÓÃÕ߶¼ÓÐȨÀûÈ¥¶ÁÈ¡ºÍÖ´ÐÐÕâ¸öµµ°¸ÁË¡£ ÈçÓûÖª chmod Ö¸Áî¸÷°Ë½øÖÆÊýÂëËù´ú±íµÄº¬Ò⣬Çë²ÎÔÄ chmod manpagesµÄ˵Ã÷¡£ --------------------------------------------------------------------------- Q3.3: Perl Ó¦¸Ã°²×°ÔÚÄÄÀï²Å¿ÉÒÔÖ´ÐÐËü£¿ Perl ¿ÉÒÔ°²×°ÔÚϵͳÉÏÈκÎÒ»¸ö½ÇÂ䣡ÄúΨһҪעÒâµÄÊÇ server ²»ÄÜÔÚchroot µÄ »·¾³ÏÂÅÜ£¬·ñÔòËü±ãÎÞ·¨ÅÜ perl ½âÒëÆ÷¡£»»ÑÔÖ®£¬ÏµÍ³¹ÜÀíÕß¿ÉÒ԰ѸùĿ¼¸Ä±ä£¬ Èà ``/'' Ö¸µ½ÁíÒ»¸öĿ¼£¬¶ø²»ÊÇʵ¼ÊÕæÕýµÄ¸ùĿ¼(``/'')¡£ --------------------------------------------------------------------------- Q3.4: ÎÒΪʲ÷áÒ»Ö±µÃµ½ ``Server: Error 500'' µÄѶϢ£¿ ÒÔϼ¸ÖÖÇéÐλᴥ·¢Õâ¸ö´íÎóѶϢ£º * Èç¹û script ¿ªÍ·µÄµØ·½Ã»ÓÐ ``#!/usr/local/bin/perl'' Õâ¸öÖ¸µ½ Perl ½â ÒëÆ÷µÄ±êÍ·£¬»òÕßÊÇÖ¸µ½½âÒëÆ÷£¨»ò library µµ£©µÄ·¾¶´íÎó¡£ * Èç¹û script Êä³öµÄµÚÒ»ÐÐÊÇÒ»¸ö²»ÕýÈ·µÄ±êÍ·£¨¼´ ``Content-Type: text/html'' £©£¬»òÕßÊǸñêÍ·ááÃæûÓиúÖøÒ»¸ö¿ÕÐС£ * Èç¹ûÄúµÄ script Óо䷨ÉϵĴíÎó¡£ÄúµÄ scripts ¶¼Ó¦ÔÚÖ¸ÁîÁÐÏÈÅÜÅÜ¿´²Å ÊÇ¡£ --------------------------------------------------------------------------- Q3.5: ÎÒÊÔÖø´ò¿ªÒ»¸öµµ°¸£¬Ïë°Ñ×ÊÁÏ´¢´æÔÚÀïÍ·£¬µ«ÊÇ open() µÄÖ¸Áîʧ°ÜÁË¡£µ½ µ×ÊÇÔõ÷á¸ãµÄ£¿ Ò»°ã˵À´£¬HTTP server ÊÇÒÔ ``nobody''¡¢``www''£¬»òÆäËûÕâÀàȨÏ޵͵ÄʹÓÃÕßµÄ Éí·ÝÀ´Åܵġ£ÒòΪÕâ¸öÔµ¹Ê£¬Äú´òËãÔÚÆäÖÐÖÆÔìеµ°¸µÄĿ¼£¬¶Ô server ÅܵÄʹÓà Õß ID ±ØÐëÒªÊÇ¿Éд(writable)²ÅÐС£ ΪÁËÈ·¶¨Æð¼û£¬Äúÿ´Î¶¼Ó¦¸ÃÏȼì²é open Õâ¸öÖ¸ÁîËͻصĽáÊø״̬(return status )£¬¿´¿´ open ÓÐûÓгɹ¦¡£ open(FILE, "/abc/data.txt") || error("Could not open file /abc/data.txt"); . . . sub error { my($message) = @_; print <<End_of_Error; Content-type: text/html Status: 500 CGI Error <HTML> <HEAD><TITLE>CGI Error</TITLE></HEAD> <BODY> <H1>Oops! Error</H1> <HR> $message <HR> </BODY> </HTML> End_of_Error } ·¢ÐÅÈË: hsiao@uconnvm.uconn.edu, ÐÅÇø: programming ±ê Ìâ: Perl-CGI ³£ÎÊÎÊÌ⼯ -- part4 ·¢ÐÅÕ¾: 2 Tigers (Fri Jun 20 16:02:22 1997) תÐÅÕ¾: cis_nctu!news.cis.nctu!UUserv.Net.tw!aidebbs!spring!serv.hinet.net!net À´ Ô´: times.oneway.net ±¾ Perl-CGI ³£ÎÊÎÊÌ⼯´¿ÎÄ×Ö°æÒÔ²»¶¨ÆÚ·½Ê½ÔÚ tw.bbs.comp.www ¼° tw.bbs.comp.language Á½¸öÐÂÎÅÌÖÂÛȺ×éÉϵÇÔØ¡£±¾Îĵij¬Îļþ°æ¼°½ñááµÚÒ»ÊÖ Ð°æ×ÊÁÏ¿É×ÔÁ½Ö»ÀÏ»¢¹¤×÷ÊÒÈ¡µÃ£¬Î»Ö·Îª£º <http://www.opim.uconn.edu/~2T/perl/perl-cgi-faq-chi/>¡£±¾´¿ÎÄ×Ö°æÖÐ ÄÚÈÝÈôÓкͳ¬Îļþ°æ²»Ò»ÖÂÖ®´¦£¬Ò»ÇоùÒÔááÕßΪ׼¡£±¾ FAQ TANet µÄ mirror £¨Õâ¸öÓ³Éäµã¿ÉÄÜ»¹ÒªµÈ¼¸Ìì²Å»á×¼±¸ºÃ£©Î»ì¶£º ftp://www.math.ncu.edu.tw/pub/staff/chenym/FAQ/Perl/ (anonymous FTP) ºÍ http://www.math.ncu.edu.tw/~chenym/FAQ/Perl/¡£Ì¨ÍåÉÌÒµÍø·µÄ mirror ÔòÔÚ http://www.oneway.net/FAQ/perl/¡£ ÒëÕß±àд±¾ FAQ£¨³£ÎÊÎÊÌ⼯£©Ö®¶¯»ú£¨ºÍÆäËûÐÂÎÅȺ×é FAQ µÄÄ¿µÄ²¢ÎÞ¶þÖ£©£¬ ÄËÓмøì¶ÏàͬµÄÎÊÌâÔÚÌÖÂÛȺÖÐÒ»ÔÙ³öÏÖ£¬Í½Ôö´ó¼ÒÈËÁ¦¡¢Æµ¿í£¬¼°Ê±¼äÉϵÄÀË·Ñ£¬ Òò´ËÏ£Íû½å´Ë FAQ £¬¶ÔÐí¶à·´¸²³öÏÖµÄÎÊÌâÌá³öÕýÈ·µÄ½â´ð£¬²¢¼õÉÙ´íÎó×ÊѶµÄ Á÷´«¡£ËùÒÔÒëÕßÔڴ˺ôÓõÌÖÂÛȺÖеÄÐÂÏÊÈËÔÚѯÎÊÓÐ¹Ø Perl-CGI Ò»ÀàÎÊÌâ֮ǰ£¬ ÎñÇë×ñÊØÍø·Àñ½Ú (netiquette)£¬ÏÈ×Ô±¾ FAQ ÖÐÕÒÑ°½â´ð¡£´ËÍâ±¾ FAQ ÒàÊÊºÏ ×÷Ϊ¸öÈ˽øÐÞ Perl-CGI ³ÌʽÉè¼ÆµÄ²ÄÁÏ¡£ Ϊ·½±ã´¿ÎÄ×Ö°æ¶ÁÕßÆð¼û£¬ËùÓг¬Îļþ°æÖÐËùº¬µÄ http ¼° ftp Á¬½Úµã¾ù¸½¼Óì¶ Êʵ±´¦À¨ºÅÖУ¬µ«Î¨¶À Q1.5 ÖеÄÊéÄ¿±íÀýÍ⣨Òò±àÅŲ»Ò×£©¡£Èô¶Ô±¾ÖÐÎÄ°æ³£ÎÊ ÎÊÌ⼯ÓÐÈκÎÎÊÌâ¼°Òâ¼û£¬Çë email ÒëÕß¡£ Ô­ÎÄ°æ¡¢ÖÐÒë°æ°æȨÊÂÒË£¬Ïê¼ûµÚ 5 ²¿·ÝÎĵף¨¸´ÖÆ¡¢×ªÔØÕßÎñ±Ø×ñÊØ£©¡£ --Ïô°ÙÁä Pai-ling Hsiao <hsiao@uconnvm.uconn.edu> -------- 8< ------- Cut Here ------- 8< -------- 4.0 £­³ÌʽÉè¼ÆÒÉÄÑÔÓÖ¢ --------------------------------------------------------------------------- Q4.1: ÎÒÏëÈà user ÌîµÄ form ×ÊÁÏ×Ô¶¯¼Ä¸øÎÒ£¬¸ÃÔõ÷á×ö£¿ÓÐûÓСõÀý£¿ Æäʵ×öÕâ¸öºÜÈÝÒס£ÄúµÄ CGI script ±ØÐëÄÜ×öµ½ÕâÁ½¼þÊ£º 1. ½« form ÖеÄ×ÊÁÏÕûÀí³öÀ´¡£±ðÍüÁË£¬ËùÓÐµÄ form ×ÊÁ϶¼»á±» URL-±àÂëÆðÀ´ £¨ÏȲ»¿¼ÂÇ Netscape 2.0 ¡¾¼° 2.0 ÒÔÉÏËùÖ§Ô®¡¿µÄ multipart MIME×ÊÁÏ£©¡£ 2. ¿ªÒ»¸ö¹Ü· (pipe) µ½ mail £¨»ò sendmail £©£¬È»áá°Ñ form ×ÊÁÏд¹ýÈ¥¡£ ÎÒÃǾͼÙÉèÄúÓõÄÊÇ CGI::* Ä£×é¡£Äú¿ÉÓÃÒÔϵķ½·¨È¥½Ð sendmail£º $cgi_form = new CGI::Form; $from = $cgi_form->param('from'); $name = $cgi_form->param('name'); $to = $cgi_form->param('to'); $subject = $cgi_form->param('subject'); $message = $cgi_form->param('message'); open SENDMAIL, "| /usr/bin/sendmail -t -n" print SENDMAIL <<End_of_Mail; From: $from <$name> To: $to Reply-To: $from Subject: $subject $message End_of_Mail ÓÐÒ»¸ö¸Ã×¢ÒâµÄµØ·½ÊÇ ``Reply-To:'' µÄÐÅÍ·¡£ÓÉì¶ server ÊÇÒÔ ``nobody'' Õâ¸öʹÓÃÕßµÄÉí·ÝÀ´ÅÜ£¬ÐÅÍ·µÄµØ·½¿ÉÄܻᱻ¸ã»µ£¨ÓÈÆäÊǵ±ÓÐÈËÏë»ØÕâ·âÐÅ µÄʱáᣩ¡£¼ÓÉÏ ``Reply-To'' µÄÐÅÍ·Õâ¸öÎÊÌâ±ã½â¾öÁË¡£ Íø·ÉÏÓÐÐí¶àµÄ mail ÇþµÀ (gateway)* ÊÇÒÔµ×ÏÂÕâÖÖ·½·¨À´ËÍ mail£º ¡¾ÒëÕß¡¿gateway ÔÚ´ËÖ¸ËÍ email µÄ CGI ³Ìʽ open MAIL, "| mail -s 'Subject' $to" ^ | +-- ¿ÉÄÜ»á³öÎÊÌâµÄ©¶´£¡£¡£¡ Èç¹ûÄúûÓÐÏȼì²é¿´ $to Õâ¸ö±äÊýÓÐûÓÐÄÚº¬ shell µÄÌØÊâ·ûºÅ (metacharacters)£¬ÄúÊÇÔÚ×ÔÌÖ¿à³Ô£¡Æ©È磬Èç¹ûÄĸö¶ñÁ user ÊäÈëÁËÒÔÏ µÄ×ÊÁÏ£º ; rm -fr / ; ÄÇ÷áÄúµÄÂé·³¿É´óÁË*¡£ ¡¾ÒëÕß¡¿ÕâÀïÍ·µÄ ``;'' ±ãÊÇÒ»¸öΣÏÕµÄ shell metacharacter¡£ ÁíÒ»¸öΣÏյķûºÅÊÇ ``&''¡£ ÔÚÕâ¸ö¼ÙÏëµÄÇé¿öÖУ¬ÓжàÉÙ¸öµµ°¸»á±»Ô¶·½µÄ user ¸øɱµô£¬»¹µÃ ÊÓ server ÅܵÄʹÓÃÕßµÄȨÏÞ¶ø¶¨£¨Õâ¾ÍÊÇΪʲ÷á server ÒªÒÔµÍȨ ÏÞʹÓÃÕßÉí·ÝÅܵÄÔ­Òò£©¡£ÖÁÉÙÄÇЩÓÉ CGI ³ÌʽÖÆÔì³öÀ´£¬µ«ÓÖû Óб¸·ÝµÄµµ°¸£¬ÊÇÕæµÄÒª¸úËüÃÇÓÀ±ðÁË¡£ ; mail joe@crackerland.org </etc/passwd ÄÇÄúµÄ CGI script ¾ÍÌæÄú°Ñ /etc/passwd ¸ø¹°ÊÖËÍÉÏÁË¡£Õâ¶ÔÒ» ¸ö¡¸Î´¼Ó¹¤¡¹µÄ Linux¡¢SunOS 4.1£¬»¹ÓÐÆäËûÈκÎû°²×° shadow-password µÄ UNIX ϵͳÀ´Ëµ£¬ÊµÔÚ²»Ì«ºÃÍæ¡£Èç¹û server ´íÎóµØÅÜÁË root£¬ÄÇ÷á¾ÍËã×°ÁË shadow-password ҲûÓÐÓã¬ÒòΪ Ô¶·½µÄ cracker ÉõÖÁ¿ÉÒÔÈÃÕâ¸ö CGI µÄ email script ¸øËûËÍ /etc/shadow £¨ÊÓϵͳ¶ø¶¨£¬²»Ò»¶¨ÔÚ /etc µ×Ï»ò½ÐÕâ¸öÃû×Ö£©¡£ ---------------------------------------------------------------------- Q4.2: ¸Õ²ÅÕâ¸öÓà form ËÍÐÅµÄ script ¿´ÆðÀ´ÓеãÄÑ¡£ÎªÊ²÷᲻Ǭ´àÓà ``mailto: URL''£¬ÕâÑù user ÌîÈëµÄ×ÊÁϾͿÉÒԼĸøÎÒÁË£¿ ºÜ²»Ðҵأ¬mailto: µÄÖ¸Áî²¢²»ÊÇËùÓеÄä¯ÀÀÆ÷¶¼Ö§Ô®¡£Èç¹ûÄúÔÚµµ°¸ÖÐÓÃÁËËü µÄ»°£¬»áÏÞÖÆÁËÄÇЩʹÓÃûÓÐÖ§Ô® mailto: µÄä¯ÀÀÆ÷µÄÈË£¬ÈÃËûÃÇÎÞ·¨ËÍ mail ¸øÄú*¡£ ¡¾ÒëÕß¡¿¾¡¹ÜÈç´Ë£¬Äú»òÐí²»»áÔÚºõÄÇÕ¼¼«ÉÙÊý±ÈÀýµÄʹÓÃÕß £¨Netscape ¡¢ IE£¬ºÍ lynx µÈä¯ÀÀÆ÷¶¼Ö§Ô® mailto: £©¡£ ---------------------------------------------------------------------- Q4.3: ÎÒÒªÈçºÎÔÚ UNIX ÒÔÍâµÄƽ̨ÉÏ×ö Perl-CGI£¬Æ©Èç Mac¡¢MS-DOS¡¢ Windows ¼° NT£¿ÎÒµÄ Perl CGI ³ÌʽÄܲ»ÄÜÔÚÕâЩƽ̨֮¼ä»¥ÏàÒÆÖ²ÄØ£¿Äܲ» ÄܺÜÖ±½Ó£¬Ã»ÓÐÂé·³£¿ÎÒÔÚ UNIX Ö÷»úÉÏÓÐÕʺţ¬µ«ÊǶ¼ÊÇÏÈÔÚ Windows/Mac ÉÏ×ö¡£ÎÒÒªÈçºÎÔÚÎÒ×Ô¼ºµÄ»úÆ÷ÉϲâÊÔдºÃµÄ CGI scripts*£¿ Perl ÒѾ­±»ÒÆÖ²µ½ÉÏÊöËùÓеÄƽ̨ÉÏÁË¡£Òò´Ë£¬ÄúµÄ Perl CGI ³ÌʽÕÕÀíÓ¦²» ÄÑÒÆÖ²¡£µ«Èç¹ûÄúʹÓõ½Ò»Ð© UNIX ÉϵijÌʽ£¬ÄÇ÷áÄúµÄ³Ìʽ¿ÉÄܻ᲻ºÃÒÆ Ö²¡£Èç¹ûÄúÖ»ÊÇ×ö×ÊÁÏ´¦Àí£¬»ò¿ªÆô¡¢¶Á½øµµ°¸µÈµÄ»°£¬ÄÇ÷áÒÆÖ²Ó¦¸Ã²»»áÓÐ ÎÊÌâ¡£ ¡¾ÒëÕß¡¿Ô­ FAQ ²¢Î´»Ø´ð×îááÕâ¸öÎÊÌâ¡£ÒªÔÚ Windows/OS2/Mac µÈ ·Ç UNIX ƽ̨ÉϲâʽÄúµÄ scripts £¬Äú¿ÉÒÔʹÓà CGI.pm £¨Ö§Ô®ÒÔ ÉÏËùÓÐƽ̨£©£¬ÅäºÏ Q4.19 ÖÐÌáʾµÄ³ý´í¼¼ÇÉ£¬»òÔÚ×Ô¼ºµÄ»úÆ÷ÉÏ °²×° HTTP server ÈíÌå¡£Èç´Ë¾Í²»ÓÃÐÁ¿àµÄÁ¬ÉÏÖ÷»úÈ¥²âʽÁË¡£ ---------------------------------------------------------------------- Q4.4: ÔÚ Perl CGI ³ÌʽÖУ¬STDERR £¨±ê×¼´íÎóѶϢ)¡¢STDIN (±ê×¼ÊäÈ룩£¬ ºÍ STDOUT £¨±ê×¼Êä³ö£©¸÷ÊÇÁ¬µ½ºÎ´¦£¿ ÔÚ CGI »·¾³Ï£¬STDERR »áÖ¸Ïò server µÄ´íÎóѶϢµµ (error log)¡£Äú¿ÉÒÔ ÉƼÓÀûÓÃÕâ¸öÌØÐÔ£¬°Ñ³ý´íµÄѶϢдµ½ STDERR£¬È»ááÄú±ã¿É½å²é¿´´íÎóѶϢµµ À´°ïÄú³ý´í¡£ STDIN ºÍ STDOUT Ôò¶¼ºÍä¯ÀÀÆ÷ÏàÁ¬¡£Êµ¼ÊÉÏ£¬STDIN Á¬µÄÊÇ server¡£ server »áÏȽâ¶Á client £¨»òä¯ÀÀÆ÷£©ËͳöµÄÇëÇóºÍ×ÊÁÏ£¬ÔÙ½«ÆäË͸ø script¡£ ÄúÒ²¿ÉÒÔÓý« STDERR ¡¸¸´ÖÆ¡¹µ½ STDOUT µÄ·½·¨À´×¥´íÎóѶϢ¡£ÕâÓ¦¸ÃÔÚ script ¿¿Ç°Í·µÄµØ·½×ö£¨µ«Ó¦ÔÚÄúÊä³öºÏÊ浀 HTTP ±êÍ·Ö®áᣩ£º open STDERR, ">&STDOUT" Õâ»á½«ËùÓеĴíÎóѶϢ¶¼×ªË͵½ STDOUT £¨¼´ä¯ÀÀÆ÷£©È¥¡£ ---------------------------------------------------------------------- Q4.5: ÈçºÎд¼ÆÊýÆ÷£¿ ¼ÆÊýÆ÷Ò»ÀàµÄ³ÌʽÏ൱Á÷ÐС£Æäʵ¼ÆÊýÆ÷µÄÔ­ÀíºÜ¼òµ¥£¬²»¹ýÊÇ£º o ÓÃÒ»¸öµµ°¸È¥´¢´æ×ÊÁÏ¡£ o ÿµ±ÓÐÈ˹âÁÙÍøÕ¾£¬Ôö´óµµ°¸ÖÐËù¼ÆµÄÊý×Ö¡£ ÒÔÏÂÊÇÒ»¸ö¼òµ¥µÄ¼ÆÊýÆ÷µÄʵÀý£º #!/usr/local/bin/perl -w $counter = "/home/shishir/counter.dat" print "Content-type: text/plain", "\n\n" open(FILE, $counter) || die "Cannot read from the counter file.\n" flock FILE, 2; $visitors = <FILE> flock FILE, 8; close FILE; open(FILE, ">$counter") || die "Cannot write to counter file.\n" flock FILE, 2; print FILE $visitors; flock FILE, 8; close FILE; ÏÖÔÚÄú¿ÉÒÔÔÚ HTML µµ°¸ÖÐÓà SSI (Server Side Include; ËÅ·þ¶Ë²åÈë)* µÄ·½ ʽÀ´ÏÔʾ¸Ã¼ÆÊýÆ÷£º ¡¾ÒëÕß¡¿SSI ÊÇ server ËùÌṩµÄÒ»ÏÄÜ£¬¿É½«¶¯Ì¬×ÊÁÏ£¬ÀýÈçÈÕ ÆÚºÍʱ¼ä£¬»ò¼ÆÊýÆ÷ÏÔʾµÈ£¬ÔÚ¿Í»§ÇëÇóÒ»Íøҳʱ¼´Ê±¼ÓÈë¸ÃÎļþ ÖС£Ö§Ô® SSI µÄ servers °üÀ¨ÁË NCSA (<http://hoohoo.ncsa.uiuc.edu>)¡¢Apache (<http://www.apache.org/>)£¬ºÍNetscape Enterprise Server (<http://home.netscape.com/comprod/server_central/product/enterprise/>) µÈ¡£ SSI ¹ÌÈ»ÊÇÒ»Ïî±ãÀûµÄÉè¼Æ£¬µ«Èç¹ý·ÝÀÄÓ㬲»µ«»á¼õµÍ server ÐÔÄÜ£¬¸ü¿ÉÄÜÕÐÀ´°²È«ÉϵÄΣ»ú¡£ ÄúÊÇµÚ <!--#exec cgi="/cgi-bin/counter.pl--> λ¹âÁÙ±¾Õ¾µÄ¿ÍÈË¡£ ---------------------------------------------------------------------- Q4.6: ÒªÈçºÎÓÃÒ»¸ö Perl µÄÈ¡´úÖ¸ÁËùÓÐ HTML ±êÇ©´ÓÒ»·ÝÎļþÖÐɾ³ý£¿ ÒÔÏÂÕâ¸ö¼òµ¥µÄ regular expression ¿ÉÓÃÀ´È¥³ý HTML ±êÇ©*£º ¡¾ÒëÕß¡¿ 1. ÒªÈÃÕâ¸ö regular expression ¿çÐÐÖ´ÐУ¬Äú±ØÐëÏȽ«ÄúµÄ script ÓÉÔ¤ÉèµÄ°´ÐÐÖ´ÐÐģʽ (line mode) ¸ÄΪ°´¶ÎÖ´ÐÐÄ£ ʽ (paragraph mode)¡£Äú¿ÉÒÔÔÚÖ¸ÁîÁÐÒÔ£º perl -00 -we '...' µÄ·½Ê½£»»òÊÇÔÚ script ÖÐÒÔ£º #!/usr/bin/perl -00 »ò $/ = "" µÄ·½Ê½À´É趨°´¶ÎÖ´ÐÐģʽ¡£ 2. ³ý·ÇÄúÐèÒª¶ÔÓûɾ³ýµÄ HTML ±êÇ©ÖеÄÄÚÈÝ×ö½øÒ»²½µÄ´¦Àí»ò ÀûÓ㬷ñÔò±¾ÀýÖÐ×îÍâΧµÄÒ»¶ÔÀ¨»¡¿ÉÈ¥µô¡£ $line =~ s/<(([^ >]|\n)*)>//g; ÏêϸµÄÏà¹Ø×ÊÁÏ£¬Çë¿´ Tom [Christiansen] µÄ striphtml ³Ìʽ (<ftp://perl.com/perl/scripts/striphtml>)£¬Õâ¸ö³ÌʽͬʱҲÊÕ¼ÔÚËûµÄ tour of perl5 regexps (<http://perl.com/perl/all_about/regexps.html>) ½²ÒåÖС£ ---------------------------------------------------------------------- Q4.7: ÒªÈçºÎÖªµÀÊÇË­£¯ÄĄ̈»úÆ÷£¯Äĸöä¯ÀÀÆ÷Ö´ÐÐÁËÎҵijÌʽ£¿ Äú¿ÉÒÔ´Ó HTTP_USER_AGENT Õâ¸ö»·¾³±äÊýµÃ֪ʹÓÃÕßËùÓõÄä¯ÀÀÆ÷¡£ ¡¾Õª×Ô WWW FAQ¡¿ ÄúµÄ CGI script ¿ÉÒÔÀûÓÃÎå¸öÖØÒªµÄ»·¾³±äÊýÀ´°ïæ±æʶʹÓÃÕßµÄÉí·Ý¡£ * HTTP_FROM Õâ¸ö»·¾³±äÊýÀíÂÛÉÏÓ¦ÉèΪʹÓÃÕßµÄemailµØÖ·¡£µ«ÊÇÐí¶àä¯ÀÀ Æ÷ÍêÈ«²»¼ÓÒÔÉ趨¡¾¼´²»Ö§Ô®¡¿£¬¶ø´ó²¿·ÝÖ§Ô®Õâ¸ö±äÊýµÄä¯ÀÀÆ÷ÓÖÈÃʹ ÓÃÕß×ÔÓÉÉ趨Õâ¸öÖµ¡£Òò´Ë£¬½¨Òé¶ÁÕ߶¥¶àÄÃËüÀ´×öΪ email form ÖÐ»Ø ÐŵØÖ·µÄÔ¤ÉèÖµ¡£ * REMOTE_USER Õâ¸ö±äÊýΨÓе± script ÔÚ°²È«ÈÏÖ¤µÄ±£»¤ÏÂÖ´ÐÐʱ²Å»á±» É趨¡£´Ó AUTH_TYPE Õâ¸ö±äÊý¿ÉÒÔÖªµÀËùÓõÄÈÏÖ¤·½·¨ÊÇÊôì¶ÄÄÒ»¸öÀà ÐÍ¡£REMOTE_USER Ôò»áº¬ÓÐÕý½ÓÊÜÈÏÖ¤µÄʹÓÃÕßµÄÃû×Ö¡£Òª×¢ÒâµÄÊÇ£¬ REMOTE_USER Ö»ÓÐÔÚʹÓð²È«ÈÏÖ¤µÄʱºò²Å»á±»É趨£¬¶øÇÒ²»ÊÇËùÓÐµÄ servers ¶¼Ö§Ô®¡£ÔÚ NCSA server µ×Ï£¬Èç¹ûÈÏÖ¤ËùʹÓõĴ«Ê䷽ʽû ÓÐÁÐÈë access.conf µµÖÐ(Ò²¾ÍÊÇ˵£¬Ó¦Ê¹Óà <Limit GET POST>£¬¶ø²»ÊÇ ½ö½öÓÃÔ¤ÉèµÄ <Limit GET>)£¬ÈÏÖ¤¿ÉÄÜ»á³öÈËÒâÍâµØʧ°Ü¡£ * REMOTE_IDENT Èç¹û server ÄÜÁ¬½ÓÉÏ¿Í»§¶ËµÄ IDENT server£¬Ëü»á½«Õâ ¸ö±äÊýÉè³ÉÔ¶·½Ê¹ÓÃÕßµÄÉí·Ý¡£µ«ÓÉì¶ÏòIDENT server ²éѯµÄ¶¯×÷Ì«»¨ ʱ¼ä£¬´ó²¿·ÝµÄ servers ¶¼°ÑÕâÏÄܹصô¡£¸üºÎ¿ö£¬¿Í»§¶ËµÄ»úÆ÷ÊÇ ·ñ»á»ØÓ¦²éѯ£¬ÓÖÊÇ·ñ»á³ÏʵÒÔ¶Ô£¬¶¼ÊÇÎÞ·¨È·¶¨µÄ¡£ * REMOTE_HOST * Õâ¸ö±äÊýµÄÉ趨ֵ²¢²»°üÀ¨Ô¶¶ËʹÓÃÕßµÄÕæʵÉí·Ý£¬µ«ÊÇ»áÌṩʹÓÃÕßÕý ÓÃÀ´Á¬ÏߵĻúÆ÷Ãû³Æ£¬Ö»Òª server ÄÜÕҵóöÀ´¡£ÓÉì¶ÎÒÃÇÎÞ·¨È·ÇеÃÖª ʹÓÃÕßµÄÕæʵÉí·Ý¡¾Çë¿´Ç°Ò»¸ö»·¾³±äÊýµÄ˵Ã÷¡¿£¬ÓеÄʱºòʹÓÿÉÈ·ÈÏ µÄλַÀ´Ìæ´ú£¬²»Ê§ÎªÒ»¸ö¿ÉÐеıäͨ·½·¨¡£ÔÚ server ²é²»µ½Ô¶¶ËµÄ»ú Æ÷Ãû³Æ£¬»òÕßÊÇΪÔö¼Ó server µÄ´¦ÀíËٶȶø½«Õâ¸ö²éѯ¹¦ÄܹصôµÄÇé¿ö Ï£¬Õâ¸ö±äÊýÊǿյģ»Çë¿´µ×Ï REMOTE_ADDRESS Ò»ÏîµÄ˵Ã÷¡£»¹ÓУ¬±ð ÍüÁËÄú¿ÉÄܻᷢÏÖËùÓÐʹÓÃͬһ¸ö proxy £¨´úÀíÈË£© server µÄʹÓÃÕßµÄ »úÆ÷Ãû¶¼±ä³ÉÁËÄÇ̨ proxy server µÄÃû×Ö¡£ * REMOTE_ADDR Õâ¸ö±äÊýµÄÉ趨ֵ²¢²»°üÀ¨Ô¶¶ËʹÓÃÕßµÄÕæʵÉí·Ý£¬µ«ÊÇ»á ÌṩʹÓÃÕßÕýÓÃÀ´Á¬ÏߵĻúÆ÷µÄ×ÊÁÏ¡£REMOTE_ADDR »á°üº¬¿Í»§¶ËµÄ IP λַ£¬ÒÔÓõã¸ô¿ªµÄÊ®½øλÊý×ÖµÄÐÎʽÀ´±íʾ¡£ÓÉì¶ÎÒÃÇÎÞ·¨È·ÇеÃ֪ʹ ÓÃÕßµÄÕæʵÉí·Ý [Çë¿´Ç°Ò»¸ö»·¾³±äÊýµÄ˵Ã÷]£¬ÓеÄʱºòʹÓÿÉÈ·ÈÏµÄ Î»Ö·À´Ìæ´ú£¬²»Ê§ÎªÒ»¸ö¿ÉÐеıäͨ·½·¨¡£ºÍÇ°Ò»Ïî REMOTE_HOST ²»Í¬ µÄÊÇ£¬Õâ¸ö±äÊýÒ»¶¨»á±»É趨¡£»¹ÓУ¬±ðÍüÁËÄú¿ÉÄܻᷢÏÖËùÓÐʹÓÃͬһ ¸ö proxy £¨´úÀíÈË£© server µÄʹÓÃÕߵĻúÆ÷λַ¶¼±ä³ÉÁËÄÇ̨ proxy server µÄλַ¡£ ¡¾ÕªÂ¼×Ô WWW FAQ ²¿·ÝÍê¡¿ ---------------------------------------------------------------------- Q4.8: È˼ҿ´µÃµ½ÎÒµÄ Perl CGI ³ÌʽÂð£¿Èç¹ûÊÇÕâÑùµÄ»°£¬ÄDz»¾ÍÈÃËûÃÇÖªµÀ ÎҵijÌʽÊÇÔõ÷áÔË×÷µÄÁË¡£ÕâÊǸö°²È«Â©¶´Âð£¿ÎÒÒªÔõ÷á°ÑËüÒþ²ØÆðÀ´£¿ Èç¹ûÄú½«ÄúµÄ server Éè³É¶ÔËùÓÐÔÚÒ»¸öÌض¨Ä¿Â¼£¨Èç cgi-bin£©Ïµĵµ°¸£¬»ò ÕßÊǾßÓÐijЩ¸±µµÃû£¨Èç ``.pl''¡¢``.tcl''¡¢``.sh''£©µÄµµ°¸Ò»Âɶ¼ÒÔ CGI ³Ìʽ¿´´ý£¬ÄÇ÷á server Ö»»áÖ´ÐÐÕâЩ³Ìʽ¡£ÖÁì¶Ê¹ÓÃÕßÊÇÎÞ·¨¿´µ½ script ±¾ÉíµÄÄÚÈݵġ£ µ«ÊÇÈç¹ûÄúÔÊÐíÈËÃÇ¿´ÄúµÄ script (Æ©Èç°ÑËü·Åµ½ HTML ÎļþµÄ¸ùĿ¼ÏÂ)£¬ ÄÇ÷áÖ»ÒªÊÇÕâ¸ö³ÌʽûÓа²È«ÉϵÄ©¶´£¬Õâ²¢²»ÄÜËãÊÇ°²È«ÎÊÌâ¡£Èç¹ûÕâ¸ö³Ì ʽÕæµÄÓа²È«ÉϵÄÆÆÕÀ¶øÄúÓÖÔÊÐíʹÓÃÕß¿´Õâ¸ö³Ìʽ£¬ÄÇ÷áËûÃDZãÓлú¿É³Ë£¬ ½ø¶øÀûÓÃÕâ¸öÈõµã¡£ ¡¾ÒëÕß¡¿ÉÏÃæÕâ¶ÎÔ­ÎÄ×÷ÕßÊǾÍÔ¶·½µÄ¿Í»§¶ËµÄʹÓÃÕ߶øÑÔ¡£ºÍÕâ¸ö Ö÷ÌâÏà¹ØµÄÒ» ¸ö³£ÎÊÎÊÌâÊÇ£º Q: ÎÒµÄ Perl CGI scripts ±ØÐ뽫ȨÏÞÉèΪȫÊÀ½ç¿É¶Á¡£¿ÉÊÇÕâÑùÒ» À´£¬ºÍÎÒͬ »úÆ÷ÓÐÕÊ»§µÄÈË£¬Ö»ÒªÖªµÀÎҵijÌʽÃû³Æ£¬¾Í¿ÉÒÔä¯ÀÀÎÒ µÄ Perl ³ÌʽµÄÄÚÈÝ£»ÓÈ Æäµ±ÆäÖÐÇ£Éæµ½ÃÜÂëµÄÎÊÌâʱ¡£ A: ÖÁÉÙÓÐÁ½¸ö½â¾ö·½·¨£¬Ò»¸ö¼òµ¥£¬Ò»¸ö¸´ÔÓ£º ¼òµ¥µÄ·½·¨ÊÇ£¬ÇëÄúµÄϵͳ¹ÜÀíÕߣ¨Èç¹û²»ÊÇÄú×Ô¼ºµÄ»°£©£¬½«ÄúµÄ CGI scripts ¼°ÃÜÂëµµ£¨Èç¹ûÄúÑ¡Ôñ½«ÃÜÂë´æ·ÅÔÚÁíÒ»¸öµµ°¸ÖÐµÄ »°£©µÄËùÓÐÕßÉè³É Web server ÅܵÄʹÓÃÕߣ¨×î³£¼ûµÄÊÇʹÓÃÕß nobody £»Ê¹ÓÃȺ nogroup »ò nobody£©£¬ È»áὫ CGI scripts µÄʹ ÓÃȨÏÞÉ趨³É 550 (-r-xr-x---)£¬ÃÜÂëµµµÄȨÏÞÉè³É 440 (-r--r-----)¡£Èç´ËÒ»À´£¬Ò»·½ÃæÄúµÄ³ÌʽµÃÒÔÖ´ÐУ¬¶øÇÒÆäËûͬ»ú Æ÷ÉÏµÄ Ê¹ÓÃÕßҲûÓа취͵¿´µ½ÄúµÄ³ÌʽºÍÃÜÂë¡£ ±È½Ï¸´ÔӵĽâ¾ö·½·¨ÊÇÏÈÌô¸öÄÑÆƵÄÃÜÂ뽫Õû¸ö³Ìʽ¼ÓÃÜÆðÀ´£¬È»áá ÔÙʹÓà Filter::decrypt Õâ¸öÄ£×éÔÚÁÙÖ´ÐÐÇ°½«Æä½â¿ª£¬Ôڴ˲»¶à ˵¡£ÓÐÐËȤµÄ¶ÁÕßÇë¿´ Filter::decrypt µÄʹÓÃ˵Ã÷£»´ËÍâÐ嵀 perl FAQ µÚÈý²¿·ÖÖÐÕâÒ»¶Î£º``How can I hide the source for my Perl program?'' £¬´ó¼ÒÒ²¿É²Î¿¼¡£ ---------------------------------------------------------------------- Q4.9: ÎÒÐèÒª½«Õû¸ö Perl library ¶¼¸´ÖƵ½ÎÒµÄ htdocs Ŀ¼µ×ÏÂÂ𣿠²»ÐèÒª¡£ÄúµÄ CGI scripts ¿ÉÒÔʹÓà server ºÍÎļþ¸ùĿ¼֮ÍâµÄÈκεµ°¸£¬ ³ý·Ç server ÊÇÔÚÒ»¸ö chroot µÄ»·¾³ÏÂÖ´ÐС£ ---------------------------------------------------------------------- Q4.10: ÎÒΪʲ÷á²»¸Ã½ÐʹÓÃÕßÊäÈëËûÃǵÄÃÜÂë»òÉí·ÝÖ¤×ֺŻòÐÅÓÿ¨ºÅÂ룿ÓÐ Ò»¸ö TYPE="password" ²»ÊǾÍÊÇÄÃÀ´×öÕâ¸öµÄÂ𣿠No! form µÄ½éÃæÖÐÓÐÒ»¸ö ``password'' µÄÀ¸Î»£¬µ«ÊÇÄú²»Ó¦¸ÃÄÃËüÀ´´¦ÀíÈÎ ºÎ»úÃÜÐÔµÄ×ÊÁÏ¡£²»¸ÃÕâ÷á×öµÄÔ­ÒòÊÇÒòΪËùÓÐµÄ form ×ÊÁÏ(°üÀ¨ ``password'' À¸) ¶¼ÊÇÒÔ´¿ÎÄ×ÖÐÎʽ£¬¶ø·ÇÒÔ¼ÓÃÜÐÎʽÓÉä¯ÀÀÆ÷ËÍÖÁ server¡£ Èç¹ûÄúÏëÒª°²È«µØ´«ËÍ×ÊÁÏ£¬ÄÇ÷áÄúÐèҪʹÓþßÓа²È«¹¦ÄÜµÄ server£¬ÀýÈç Netscape µÄ Commerce Server (<http://home.netscape.com/comprod/products/iapps/platform.html>)*¡£ ¡¾ÒëÕß¡¿Apache SSL £¬ÀýÈç Stronghold °æ (<http://stronghold.c2.net>)£¬Í¬Ñù¾ßÓÐÕâ¸ö¹¦ÄÜ¡£ ---------------------------------------------------------------------- Q4.11: ÎÒÒªÈçºÎ²úÉúרÃÅÌæ Netscape Éè¼ÆµÄÍøÒ³£¬ÒÔ±ðì¶ÊÀÉÏÆäËûµÄä¯ÀÀ Æ÷£¿ Äú¿ÉÒÔ͸¹ý HTTP_USER_AGENT Õâ¸ö»·¾³±äÊýÔÚÄúµÄ CGI script ÖеÃÖªÊÇ·ñ Netscape ÕýÔÚÖ´ÐÐÄúµÄ script¡£ÒÔÏÂΪһÀý£º $browser = $ENV{'HTTP_USER_AGENT'}; if ($browser =~ /Mozilla/) { # # Netscape # } else { # # Non Netscape # } ---------------------------------------------------------------------- Q4.12: Ϊʲ÷áÎÒµÄ system() Ëù²úÉúµÄ×ÊÁÏÊä³ö˳Ðò²»¶Ô£¿ ÕâÊÇÓÉ춱ê×¼Êä³öµÄ²úÉú·½Ê½Í¨³£ÊÇÏÈÀÛ»ýÏ൱µÄ×ÊÁÏÔÙÊä³ö(buffered)¡£Òª ÈÃÊä³öµÄ×ÊÁÏÒÔÕýÈ·µÄ˳ÐòÏÔʾ£¬Äú±ØÐë½åÓÉ $| Õâ¸ö±äÊýµÄÉ趨½« buffering µÄÌØÐԹصô¡£ ---------------------------------------------------------------------- Q4.13: ÎÒÌý˵ Netscape »áÖ§Ô® Java*¡£ÕâÊDz»ÊÇ˵ÎÒÏÖÔÚµÃÆú Perl£¬¸Ä Java ÁË£¿ÊDz»ÊǸÃÕâ÷á×ö£¿ ¡¾ÒëÕß¡¿Ô­ FAQ ÒÑÓÐÏ൱һ¶Îʱ¼äδ¸üС£Õâ¾ä»°ÏÖÔÚÓ¦¸Ã¸Ä×÷ ¡¸Netscape ºÍ IE Á½´óä¯ÀÀÆ÷¶¼ÒÑÖ§Ô® Java¡¹¡£ ²»¡¢²»¡¢²»¡£Java ºÍ CGI µÄ¸ÅÄîÍêÈ«²»Í¬¡£CGI ÊÇÔÚ server ¶ËÖ´ÐУ¬¶ø JavaÔòÊÇÔÚ client ¶ËÖ´ÐС£ÓÐЩ¶«Î÷£¨È綯»­£©¿É½åÓÉʹÓà Java ¶øµÃµ½½ÏºÃ µÄЧ¹û¡£µ«Äú¿É¼ÌÐøʹÓà perl À´·¢Õ¹ server ¶ËµÄÓ¦ÓóÌʽ¡£ Èç¹ûÄúÐèÒªÓÐ¹Ø Java ½øÒ»²½µÄ×ÊÁÏ£¬µ×ÏÂÁÐÁ˼¸¸öÎļþÄú¿ÉÒÔÈ¥¿´¿´*£º * ÉýÑô¹«Ë¾µÄ Java Îļþ (<http://java.sun.com>) * Tom C.ËùдµÄ Java uber Alles£¨Java µÄÖÖÖÖ£© * Java, the Illusion£¨Java »ÃÏñ£© ¡¾ÒëÕß¡¿ááÃæÕâÁ½ÆªÎÄÕÂ¶Ô Java ¼°Õâ¸öÈȳ±×÷Á˺ÜÑÏÀ÷µÄÅúÅС£±¾ FAQ ×÷Õß Tom C. µÄ Java uber Alles ÖеÄÂÛµãÖ÷ÒªÖøÖØ춼¼Êõ²ã Ãæ¡£Tom ¶Ô Java µÄ̬¶È»òÐí´ú±íÁ˲»ÉÙ Perl ÕóÓªÈËÊ˵ÄÐÄÉù¡£ ---------------------------------------------------------------------- Q4.14: ÎÒÒªÈçºÎ¶ÁÈ¡»·¾³±äÊý£¿ÎªÊ²÷áËüÃÇÓÐʱºò»á²»Ò»Ñù£¿ Äú¿ÉÒÔ͸¹ý %ENV Õâ¸ö¹ØÁ¬ÕóÁÐÀ´¶ÁÈ¡»·¾³±äÊý¡£ÒÔÏÂÕâ¸ö¼òµ¥µÄ script »á°Ñ ËùÓеĻ·¾³±äÊýÓ¡³öÀ´£¨ÅźÃ˳Ðò£©£º #!/usr/local/bin/perl -w print "Content-type: text/plain", "\n\n" foreach $key (sort keys %ENV) { print $key, " = ", $ENV{$key}, "\n" } exit 0; ºÜ²»Ðҵģ¬ÓÐЩ»·¾³±äÊý»á±»Ä³Ð©ä¯ÀÀÆ÷ºöÂÔµô¡£Æ©È磬ÓÐЩä¯ÀÀÆ÷¾Í²»É趨 HTTP_REFERER¡£ ---------------------------------------------------------------------- Q4.15: Ϊʲ÷áÎÒÊä³öµÄ×ÊÁϱ»½ÁÂÒÁË£¨Èç ``b < a'' »á±»ÆÆ»µµô£©£¿ Èç¹ûÄúË굀 MIME ÀàÐÍÊÇ HTML µÄ»°£¬Äú±ØÐ롸ÌøÍÑ¡¹ (escape) ijЩ·ûºÅ£¬ Èç ``<''¡¢``&anp;''£¬¼° ``>''£¬·ñÔòä¯ÀÀÆ÷»áÒÔΪËüÊÇ HTML ¡¾±êÇ©¡¿¡£ Äú±ØÐëʹÓÃÒÔϸñʽÀ´ÌøÍÑÌØÊâ×ÖÔª£º &#ASCII ´úÂë; Äú¿ÉÒÔÔÚÖ¸ÁîÁÐÖ´ÐÐÕâ¸ö¼òµ¥µÄ script£¬±ã¿ÉµÃµ½·Ç×ÖĸÊý×ÖÐÔ×ÖÔª (non alpha-numeric characters) Ëù¶ÔÓ¦µÄ ASCII Â룺 #!/usr/local/bin/perl -w print 'ÇëÊäÈë×Ö´®£º '; chop($string = <STDIN>); $string =~ s/([^\w\s])/sprintf("&#%d;", ord($1))/ge; print 'ÌøÍѹýµÄ×Ö´®ÊÇ£º ', "$string\n" exit 0; ---------------------------------------------------------------------- Q4.16: Ϊʲ÷áÎÒµÄPerl CGI ³Ìʽ¿ÉÒÔÓÉÖ¸ÁîÁУ¬È´ÎÞ·¨´Óä¯ÀÀÆ÷È¥Ö´ÐУ¿ ×î¿ÉÄܵÄÔ­ÒòÊÇȨÏÞµÄÎÊÌâ¡£±ðÍüÁË£¬ÄúµÄ server ¿ÉÄÜÊÇÒÔ ``nobody''¡¢ ``www''£¬»òÆäËûȨÏ޺ܵ͵ÄÕÊ»§Éí·ÝÀ´Ö´Ðеġ£Òò´Ë£¬³ý·ÇËüÓÐ×ã¹»µÄȨÏÞ£¬ ·ñÔòÊÇÎÞ·¨Ö´ÐÐÄúµÄ script µÄ¡£ ---------------------------------------------------------------------- Q4.17: Ϊʲ÷áÎÒµÄ Perl CGI ³ÌʽÄÜÅÜ£¬µ«ÊDz»»á°Ñ×ÊÁÏдµ½µµ°¸ÖУ¿ ÕâÓÖÊÇȨÏÞÔÚ×÷¹Ö£¡server ³ý·ÇÓÐ×ã¹»µÄȨÏÞ·ñÔòÊÇÎÞ·¨½«×ÊÁÏд½øijĿ¼Ï µÄijµµ°¸ÀïÈ¥µÄ¡£ ÄúÓ¦¸ÃÑø³ÉÏ°¹ß¼ì²é open Ö¸ÁîµÝ»ØµÄ´íÎó״̬ (error status)£º print "Content-type: text/plain\n\n" . . . open(FILE, ">/some/dir/some.file") || print 'ÎÞ·¨Ð´½øµµ°¸£º ', "$!\n" . . . ---------------------------------------------------------------------- Q4.18: ÒªÈçºÎ×öÒ»¸ö»áάϵ״̬£¬»òÔÊÐí¡¾Í¬Ò»Ê¹ÓÃÕß¡¿¶à´ÎÁ¬ÏßµÄ form£¿ Äú¿ÉÒÔÓà CGI::MiniSvr Õâ¸ö module À´Î¬³Ö¡¾¼Çס¡¿¼¸´Î²»Í¬µÄÁ¬ÏßÖ®¼äµÄ×´ ̬×ÊÁÏ¡£ »òÕߣ¬Äú¿ÉÒÔÖÆ×öһϵÁеĶ¯Ì¬Îļþ£¬Ôڱ˴ËÖ®¼äÏ໥´«µÝÒ»¸öÆÚ¼ä´úÂë (session ID)£¬´Ë´úÂë¿ÉÒÔÒÔѯÎÊ (query)¡¢¶îÍâ·¾¶£¬»òÒþ²ØʽÀ¸Î»µÈÐÎʽ´æ ÔÚ*¡£ ¡¾ÒëÕß¡¿CGI.pm »áÌæÄú°ÑÕⲿ·Ý£¨Î¬³Ö״̬)×öºÃ (ÓÃÉÏÊöµÄÔ­Àí £©£¬¹ÊʹÓà CGI.pm ¿É×Ô¶¯ÏíÊÜÕâÏÄÜ£¬²»ÐèÒª×Ô¼ºÈ¥×ö¡£ÕâÓÖ¶à ÁËÒ»¸ö¸ÃʹÓà CGI.pm µÄÀíÓÉ¡£ ---------------------------------------------------------------------- Q4.19: Èç¹û²»´Óä¯ÀÀÆ÷È¥Ö´ÐÐÎÒµÄ CGI ³Ìʽ£¬ÒªÈçºÎÌæËü³ý´í£¿ CGI ³Ìʽ²»ÈÝÒ׳ý´í¡£Äú¿ÉÒÔ½åÖøÊÖ¶¯É趨»·¾³±äÊýÀ´Ä£Äâ server£º setenv HTTP_USER_AGENT "Mozilla/2.0b6" (csh) »ò export HTTP_USER_AGENT = "Mozilla/2.0b6" (ksh, bash) ҪģÄâ POST ÇëÇó£¬Äú¿ÉÒÔ°Ñ×ÊÁÏÏȷŽøÒ»¸öµµ°¸ÀȻáá°ÑËü pipe µ½ÄúµÄ³Ì ʽȥ£º cat data.file | some_program.pl »òÕßÄú¿ÉÒÔÓà CGI.pm À´°ïÄú³ý´í¡£¼ÙÉèÄúÓÐÒ»¸öÏñÏÂÃæÕâÑùµÄ script£¬Ëü»á °ÑËùÓÐÄú´«¸øËüµÄË÷Òý£¯É趨ֵ¶ÔÓ¦×ÊÁÏ (key/value pairs) ¶¼ÁÐÓ¡³öÀ´¡£ #!/usr/local/bin/perl -w use CGI; $cgi = new CGI; print $cgi->header; print $cgi->start_html("Simple CGI.pm Program"); print "<H1>Simple CGI.pm Program</H1>\n" print "<HR >" print 'ÒÔÏÂËùÁеÄÊÇÄú´«Ë͵ÄÉ趨ֵ£º'; print $cgi->dump; exit 0; Õâ¸ö script ²»»áÔÚºõÄúÊÇ͸¹ý GET¡¢POST£¬»ò ISINDEX ÇëÇ󣬻òÕßÊÇÓÉÖ¸Áî ÁС¢±ê×¼ÊäÈ룬»òÎÄ×Öµµ½«×ÊÁÏ´«Ë͸øËü¡£ÎªÁË·½±ã³ý´í£¬ÎÒÃǾÍÖ±½Ó´ÓÖ¸Áî Áд«Ò»Ð©×ÊÁϸøËü°É£º % simple.cgi first=shishir last=gundavaram document='CGI\ FAQ' »ò % simple.cgi "first=shishir&last=gundavaram&document='CGI\ FAQ'" ÔÚµÚ¶þ¸öÀý×ÓÖУ¬Õû¸ö×Ö´®ÖÜΧ±ØÐë¼ÓÒýºÅ(")£¬·ñÔò shell ¿´µ½ ``&'' Õâ¸ö ·ûºÅ»áÎó½â¡£ºÃ£¬½ÓÏÂÀ´ÊÇ´Ó±ê×¼ÊäÈëÀ´³ý´íµÄ·½·¨£º % simple.cgi (waiting for standard input) first=shishir last=gundavaram document=CGI\ FAQ ^D µ±È»£¬ÄúÒ²¿ÉÒÔÏÈÓÃÒ»¸öµµ°¸À´´¢´æ×ÊÁÏ£¬È»ááÔÙ×öÊäÈëתÏò£¬ÏñÕâÑù£º % simple.cgi < form.data ÄúÒ²¿ÉÒÔÓà CGI Lint £¨¼´½«³ö°æ£©¡£ËüÄÜ´ïµ½ÏàͬµÄ¹¦Ð§¡£ÁíÍ⣬ËüÒ²ÄÜ°ïæ ¼ì²éÓÐÎÞ°²È«ÎÊÌ⣬²»µ±Ê¹Óà open()£¬ÒÔ¼°²»ÕýÈ·µÄ HTTP ±êÍ·µÈ¡£ ---------------------------------------------------------------------- Q4.20: Èç¹û²»¿¿<FORM>±êÇ©£¬ÒªÈçºÎ½Ð³ö Perl CGI ³Ìʽ£¿ Äú¿ÉÒÔÖ±½ÓÈ¥´ò¿ª¸Ã CGI ³ÌʽµÄ URL£º http://some.machine/cgi-bin/your_program.pl ÄúÒ²¿ÉÒÔÔÚÎļþÖÐʹÓÃÁ¬½áµÄ·½Ê½£¬ÀýÈ磺 <A HREF="http://some.machine/cgi-bin/your_program.pl"> ÒªÊÔÊÔÎÒµÄCGI³ÌʽÇëÔÚÕâÀïµãÒ»ÏÂ</A> ---------------------------------------------------------------------- Q4.21: ÒªÈçºÎ±ÜÃâÅÔÈ˲»ÏÈÌîÀ¸Î»¾ÍÖ´ÐÐÎÒµÄ form£¿ËûÃÇΪʲ÷áÒ»Ö±²»¶ÏÕâ÷á ×ö£¿ ÕâЩÈËÀ¸Î»ÍêÈ«¿Õ°×¾ÍÈ¥Ö´ÐÐ form ÊÇÒòΪËûÃÇ°ÑÕâ¸ö form µÄ URL ´¢´æÆðÀ´ ¡¾´¢´æµ½ÊéÇ©ÀïÃæ¡¿µÄ¹Øϵ¡£µ±ËûÃÇÏ´νгöÕâ¸ö form µÄʱºò£¬Õâ¸öÇëÇó¾Í»á ±ä³ÉÊÇÒ»¸ö¿ÕµÄ GET £¨¶ø·Ç POST »òÌîÓÐ×ÊÁ쵀 GET£©¡£ Äú¿ÉÒÔÏȼì²éËùÓÐÀ¸Î»ÖеÄ×ÊÁÏ£¬Èç¹ûÆäÖÐÓÐÀ¸Î»Áô°×µÄ»°£¬Äú¿ÉÒÔËÍ»ØÒ»¸ö ``No Content'' µÄ״̬ÊôÐÔ*¡£ÒÔÏÂÊÇÒ»Àý(¼ÙÉè¹ØÁ¬ÕóÁÐ %form Öк¬ÓÐÄú form µÄ×ÊÁÏ)£º ¡¾ÒëÕß¡¿×´Ì¬Âë 204 µÄÊôÐÔÒÑÓÉ HTTP 0.9 (<http://www.w3.org/pub/WWW/Protocols/HTTP/HTTP2.html>) µÄ ``No Response'' ±äΪ HTTP 1.0 (<ftp://ds.internic.net/rfc/rfc1945.txt>) ºÍ HTTP 1.1 (<ftp://ds.internic.net/rfc/rfc2068.txt>) ÖÐµÄ ``No Content'' ÁË¡£ $error = 0; foreach $value (values %form) { $value =~ s/\s//g; $error = 1 unless $value; } if ($error) { print "Content-type: text/plain\n" print "Status: 204 No Content\n\n" print '³ý·ÇÄúµÄä¯ÀÀÆ÷²»Ö§Ô®×´Ì¬Âë 204 £¬·ñÔòÄú²»¸Ã¿´µ½Õⲿ·Ý', "\n" } else { # # Process Data Here # } ---------------------------------------------------------------------- Q4.22: ÄÇЩserver »ØÓ¦Âë (server response codes) (<http://www.w3.org/hypertext/WWW/Protocols/HTTP/HTRESP.html>)ÊǸÉʲ÷á Óõģ¿ÓÐʲ÷áÒâÒ壿 CGI ³Ìʽ¿ÉÒÔ´«ËÍ server È»áá server »á°ÑËüתË͸øä¯ÀÀÆ÷¡£ÀýÈ磺¼ÙÉèÄú ÏëËÍ``No Content'' £¨Òâ˼ÊǸæËßä¯ÀÀÆ÷²»ÒªÔÙÖØÐÂÏÂÔظÃÍøÒ³£©£¬ÄÇ÷áÄúµÃ ËÍÒ»¸ö 204 µÄ»ØÓ¦Â루¼ûÉÏÀý£©¡£ ---------------------------------------------------------------------- Q4.23: Ϊʲ÷á print "Location: http://host/page.html\n" ²» work£¿ÓÖΪ ʲ÷áËüÖ» work Ò»´Î£¬µ«ËæááµÄתÏò¾Í¶¼Åª´íÁËÄØ£¿ CGI ³ÌʽֻÄÜËÍÒ»¸ö Location ±êÍ·¡£»¹ÓУ¬Èç¹ûÄúÒª server ×öתÏòµÄ¶¯×÷ Äú¾Í²»¸ÃËÍ MIME Àà±ð¡£Æ©È磬ÒÔϵÄÀý×ÓÊÇ´íÎóµÄʾ¡õ£¬¾¡¹ÜÔÚÓÐЩ servers ÉÏÐеÄͨ£º #!/usr/local/bin/perl -w . . . print "Content-type: text/plain\n" print "Location: http://some.machine/some.doc\n\n"" ---------------------------------------------------------------------- Q4.24: ÒªÈçºÎÈà server ÔÚÿ¸ö HTML ÍøÒ³µÄµ×²¿¶¼×Ô¶¯¼ÓÉÏÒ»¸ö£º¡¸×î½ü¸ü ÐÂÈÕÆÚ£º ...¡¹µÄ¸æʾ£¿»òÕߣ¬ÊDz»ÊÇÖ»ÓÐ SSI µÄÍøÒ³²ÅÄÜÕâ÷á×ö£¿CGI ³Ìʽ µÄÈÕÆÚÒªÈçºÎÈ¡µÃ£¿ Èç¹ûÄúÊÇ͸¹ý CGI ÒÔ¶¯Ì¬·½Ê½À´²úÉúÄúµÄÎļþ£¬ÄÇ÷áÒª²åÈëÒ»¸öʱ¼ä±ê¼Ç·Ç³£ ¼òµ¥¡£ÒÔÏÂÊÇÒ»Àý£¨½öÊÊÓÃì¶ Perl 5£©£º $last_updated = localtime; print '×î½ü¸üÐÂÈÕÆÚ£º ',"$last_updated\n" »òÕßÊÇ£º require "ctime.pl" $last_updated = &ctime(time); print '×î½ü¸üÐÂÈÕÆÚ£º ', "$last_updated\n" ÉõÖÁÏñÕâÑù£º chop($date = `/usr/local/bin/date`); print '×î½ü¸üÐÂÈÕÆÚ£º ', "$last_updated\n" Äú¿ÉÒÔÓà SSI À´´ïµ½Õâ¸öЧ¹û£¬ÏñÕâÑù£º <--#echo var="LAST_MODIFIED"--> ---------------------------------------------------------------------- Q4.25: ʲ÷áÑùµÄ³¡ºÏÏÂÒÔ Perl д CGI ³Ìʽ»áÏÔµÃ̫СÌâ´ó×÷£¬ÒòΪÓà shell ¾Í¿ÉÒÔ×öµ½£¿¶øʲ÷áÑùµÄ³¡ºÏ¶Ô Perl À´ËµÓÖ¹ýì¶À§ÄÑ£¿Óà C++ ×öÕâÀàµÄʲ» ÊǺõöàÂð£¿ÄÇÓà C ÄØ£¿ ÿһ¸öÓïÑÔ¶¼ÓÐÆ䳤´¦ºÍ¶Ì´¦¡£ÏàÐÅÕâ¾ä»°ÄúÌý¹ýºÜ¶à´ÎÁË¡£ËùÒÔÒ»ÇÐÈ«¿´ÄúÒª ×öµÄÊÇʲ÷á¶ø¶¨¡£Èç¹ûÄúÔ¤ÆÚÕý×¼±¸Ð´µÄ CGI ³Ìʽÿ¸öÖÓÍ·»áÓм¸Ç§¼¸ÍòÈË´Î Á¬È¥Ê¹Óã¬ÄÇ÷áÄúÓ¦¸ÃÑ¡Óà C »ò C++À´Ð´¡£Èç¹ûÄúÇó¿ìµÄ»°£¨Ö¸·¢Õ¹Ëù»¨·ÑµÄ ʱ¼ä¶øÑÔ£©£¬ÄÇ÷á Perl ÊÇÕýÈ·µÄÑ¡Ôñ£¡ Ò»°ã˵À´£¬ÄúÓ¦±ÜÃâÓà shell À´×öÈκÎÐÎʽµÄ CGI ³ÌʽÉè¼Æ£¬ÒòΪ shell ÔÚ ÏÈÌìÉÏÈÝÒײúÉú°²È«ÎÊÌâ¡£ ----------------------------------------------------------------------