home *** CD-ROM | disk | FTP | other *** search
/ Chip: Windows 2000 Professional Resource Kit / W2KPRK.iso / apps / perl / ActivePerl.exe / data.z / perlwin32faq7.html < prev    next >
Encoding:
Extensible Markup Language  |  1999-10-15  |  17.4 KB  |  483 lines

  1. <?xml version="1.0"?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  3.     "http://www.w3.org/TR/xhtml1/DTD/transitional.dtd">
  4. <html xmlns="http://www.w3.org/TR/xhtml1">
  5.   <head>
  6.     <title>perlwin32faq7 - Web Programming</title>
  7.     <link rev="made" href="mailto:bjepson@debian.ids.net" />
  8.     <meta name="GENERATOR" charset="iso-8859-1" />
  9.     <link rel="STYLESHEET" href="../win32prk.css" type="text/css"
  10.     media="screen" />
  11.   </head>
  12.  
  13.   <body bgcolor="#ffffff">
  14.     <!-- beginning of leaf header-->
  15.  
  16.     <table width="100%">
  17.       <tr>
  18.         <td bgcolor="000000" width="70" height="31"><a href= 
  19.         "http://www.activestate.com/"><img src="ASbutton.gif" alt= 
  20.         "ActiveState Home Page" border="0" width="68" height=
  21.         "30" /></a></td>
  22.  
  23.         <td width="10" bgcolor="#ffffff"> </td>
  24.  
  25.         <td valign="middle" bgcolor="#cc0066"><font face=
  26.         "sans-serif" size="+1" color="#ff99cc">   
  27.         Win32 FAQ</font></td>
  28.       </tr>
  29.     </table>
  30.     <!-- end of leaf content--><!-- INDEX BEGIN -->
  31.  
  32.     <ul>
  33.       <li><a href="#NAME">NAME</a></li>
  34.  
  35.       <li>
  36.         <a href="#DESCRIPTION">DESCRIPTION</a> 
  37.  
  38.         <ul>
  39.           <li><a href="#What_is_HTTP_and_how_do_I_get_m">What is
  40.           HTTP, and how do I get more information on it?</a></li>
  41.  
  42.           <li><a href="#What_is_CGI_and_how_do_I_get_mo">What is
  43.           CGI, and how do I get more information on it?</a></li>
  44.  
  45.           <li><a href="#How_do_I_return_a_graphics_file_">How do I
  46.           return a graphics file from a CGI script?</a></li>
  47.  
  48.           <li><a href="#My_CGI_scripts_don_t_seem_to_run">My CGI
  49.           scripts don't seem to run right under PerlIS.</a></li>
  50.  
  51.           <li><a href="#How_does_my_script_know_if_it_s_">How does
  52.           my script know if it's running under Perl for Win32
  53.           or</a></li>
  54.  
  55.           <li><a href="#How_does_my_script_know_if_it_s_">How does
  56.           my script know if it's running under Perl for Win32
  57.           or</a></li>
  58.  
  59.           <li><a href="#What_CGI_modules_run_with_Perl_f">What CGI
  60.           modules run with Perl for Win32?</a></li>
  61.  
  62.           <li><a href="#How_do_I_use_redirection_in_my_s">How do I
  63.           use redirection in my script?</a></li>
  64.  
  65.           <li><a href="#What_are_cookies_and_how_do_I_us">What are
  66.           cookies and how do I use them?</a></li>
  67.  
  68.           <li><a href="#How_do_I_get_the_e_mail_address_">How do I
  69.           get the e-mail address of the user?</a></li>
  70.  
  71.           <li><a href="#I_need_a_CGI_script_to_do_a_cert">I need a
  72.           CGI script to do a certain task. Has anyone done it
  73.           before?</a></li>
  74.  
  75.           <li><a href="#How_do_I_test_my_CGI_programs_th">How do I
  76.           test my CGI programs that take arguments without
  77.           running</a></li>
  78.  
  79.           <li><a href="#The_Content_Type_header_field_I">The
  80.           Content-Type: header field I output from my CGI script
  81.           shows up</a></li>
  82.  
  83.           <li><a href="#When_I_try_to_run_a_CGI_script_f">When I
  84.           try to run a CGI script from my browser, it tries
  85.           to</a></li>
  86.  
  87.           <li><a href="#What_s_Perl_for_WSAPI_">What's Perl for
  88.           WSAPI?</a></li>
  89.         </ul>
  90.       </li>
  91.  
  92.       <li><a href="#AUTHOR_AND_COPYRIGHT">AUTHOR AND
  93.       COPYRIGHT</a></li>
  94.     </ul>
  95.     <!-- INDEX END -->
  96.     <hr />
  97.     <br />
  98.  
  99.     <h1><a name="NAME">NAME</a></h1>
  100.  
  101.     <p>perlwin32faq7 - Web Programming <font size="-1">(CGI</font>
  102.     and PerlIS)</p>
  103.     <br />
  104.     <hr />
  105.  
  106.     <h1><a name="DESCRIPTION">DESCRIPTION</a></h1>
  107.  
  108.     <p>Web development with Perl for Win32</p>
  109.     <br />
  110.     <hr />
  111.  
  112.     <h2><a name="What_is_HTTP_and_how_do_I_get_m">What is HTTP, and
  113.     how do I get more information on it?</a></h2>
  114.  
  115.     <p> <font size="-1">HTTP</font> (HyperText Transfer Protocol)
  116.     is the protocol that web browsers use to talk to web servers.
  117.     The official specification for the <font size="-1">HTTP</font>
  118.     standard is available on the <font size="-1">W3</font>
  119.     Consortium web server at:</p>
  120.     <br />
  121. <pre>
  122.     <a href=
  123. "http://www.w3.org/pub/WWW/Protocols/">http://www.w3.org/pub/WWW/Protocols/</a>
  124. </pre>
  125.  
  126.     <p>Some more readable introductions can be found on the Yahoo
  127.     <font size="-1">HTTP</font> page at:</p>
  128.     <br />
  129. <pre>
  130.     <a href= 
  131. "http://www.yahoo.com/Computers_and_Internet/Internet/World_Wide_Web/HTTP/">http://www.yahoo.com/Computers_and_Internet/Internet/World_Wide_Web/HTTP/</a>
  132. </pre>
  133.     <br />
  134.     <hr />
  135.  
  136.     <h2><a name="What_is_CGI_and_how_do_I_get_mo">What is CGI, and
  137.     how do I get more information on it?</a></h2>
  138.  
  139.     <p> <font size="-1">CGI</font> (Common Gateway Interface) is a
  140.     protocol used by web servers to run server programs. Scripts
  141.     that support the <font size="-1">CGI</font> protocol are
  142.     sometimes called <font size="-1">``CGI</font> scripts''; this
  143.     leads to the unfortunate misperception that <font size="-1">
  144.     CGI</font> is a language of its own.</p>
  145.  
  146.     <p>The classic information on <font size="-1">CGI</font> is
  147.     available on the <font size="-1">NCSA</font> server at:</p>
  148.     <br />
  149. <pre>
  150.     <a href=
  151. "http://hoohoo.ncsa.uiuc.edu/cgi/">http://hoohoo.ncsa.uiuc.edu/cgi/</a>
  152. </pre>
  153.  
  154.     <p>If you haven't read this, read it now. If it doesn't click
  155.     for you, you can check the Yahoo <font size="-1">CGI</font>
  156.     page at:</p>
  157.     <br />
  158. <pre>
  159.     <a href= 
  160. "http://www.yahoo.com/Computers_and_Internet/Internet/World_Wide_Web/CGI___Common_Gateway_Interface/">http://www.yahoo.com/Computers_and_Internet/Internet/World_Wide_Web/CGI___Common_Gateway_Interface/</a>
  161. </pre>
  162.  
  163.     <p>(All one <font size="-1">URL)</font></p>
  164.  
  165.     <p>Try also the following <font size="-1">URL</font> for <font
  166.     size="-1">CGI</font> programming in Perl:</p>
  167.     <br />
  168. <pre>
  169.     <a href= 
  170. "http://www.perl.com/CPAN-local/doc/FAQs/cgi/perl-cgi-faq.html">http://www.perl.com/CPAN-local/doc/FAQs/cgi/perl-cgi-faq.html</a>
  171. </pre>
  172.  
  173.     <p>If you still don't get it, try one of the
  174.     ``get-rich-quick-by-writing-CGI-scripts'' books at your local
  175.     bookstores.</p>
  176.     <br />
  177.     <hr />
  178.  
  179.     <h2><a name="How_do_I_return_a_graphics_file_">How do I return
  180.     a graphics file from a CGI script?</a></h2>
  181.  
  182.     <p>One of the big differences between <font size="-1">
  183.     UNIX</font> and Win32 platforms is that on Win32 there's a
  184.     difference between text or <font size="-1">ASCII</font> files
  185.     and binary file. To return a graphics file, you need to specify
  186.     that the file is a binary file, and that the standard output
  187.     stream should accept binary data. Try something like this:</p>
  188.     <br />
  189. <pre>
  190.     $MY_FILE_NAME = 'Penelope.jpg';
  191.     $CHUNK_SIZE = 4096;
  192.     
  193.     open( MY_FILE, "<$MY_FILE_NAME" )
  194.         or die( "Can't open $MY_FILE_NAME: $!\n" );
  195.     
  196.     print "Content-type: image/jpeg\r\n";
  197.     print "\r\n";
  198.     
  199.     binmode( MY_FILE ); # These are crucial!
  200.     binmode( STDOUT );
  201.     
  202.     while ( $cb = read( MY_FILE, $data, $CHUNK_SIZE ) ) {
  203.         print $data;
  204.     }
  205.     
  206.     close( MY_FILE );
  207. </pre>
  208.     <br />
  209.     <hr />
  210.  
  211.     <h2><a name="My_CGI_scripts_don_t_seem_to_run">My CGI scripts
  212.     don't seem to run right under PerlIS.</a></h2>
  213.  
  214.     <p>Earlier versions of Perl for <font size="-1">ISAPI</font>
  215.     did not output headers correctly, and this caused Perl for
  216.     <font size="-1">ISAPI</font> scripts to misbehave. You could
  217.     work around this in earlier versions of PerlIS by sending the
  218.     headers at the beginning of your script like this:</p>
  219.     <br />
  220. <pre>
  221.     print <<"END";
  222.     HTTP/1.0 200 OK
  223.     Content-Type: text/html
  224.     
  225.     END
  226. </pre>
  227.  
  228.     <p>This is no longer necessary with the current version of Perl
  229.     for <font size="-1">ISAPI,</font> as Perl for <font size="-1">
  230.     ISAPI</font> sends the correct headers. The Registry value that
  231.     governs this is <code>EnableCGIHeader</code>, a <font size=
  232.     "-1">REG_DWORD</font> value that is set to 1 by default. This
  233.     value is stored under the Registry key
  234.     HKEY_LOCAL_MACHINE\SOFTWARE\ActiveState\PerlIS. If you need to
  235.     turn off the automatic generation of the header, set this value
  236.     to 0.</p>
  237.     <br />
  238.     <hr />
  239.  
  240.     <h2><a name="How_does_my_script_know_if_it_s_">How does my
  241.     script know if it's running under Perl for Win32 or Perl for
  242.     ISAPI?</a></h2>
  243.  
  244.     <p>Perl for <font size="-1">ISAPI</font> sets an environment
  245.     variable, <code>PERLXS</code>, for scripts that are executed
  246.     under Perl for <font size="-1">ISAPI.</font></p>
  247.     <br />
  248. <pre>
  249.     my $running_under_perlis = $ENV{PERLXS} eq 'PerlIS';
  250. </pre>
  251.     <br />
  252.     <hr />
  253.  
  254.     <h2><a name="How_does_my_script_know_if_it_s_">How does my
  255.     script know if it's running under Perl for Win32 or Perl for
  256.     WebSite?</a></h2>
  257.  
  258.     <p>Perl for WebSite sets an environment variable, <code>
  259.     PERLXS</code>, for scripts that are executed under Perl for
  260.     WebSite.</p>
  261.     <br />
  262. <pre>
  263.     my $running_under_perlws = $ENV{PERLXS} eq 'PerlWS';
  264. </pre>
  265.     <br />
  266.     <hr />
  267.  
  268.     <h2><a name="What_CGI_modules_run_with_Perl_f">What CGI modules
  269.     run with Perl for Win32?</a></h2>
  270.  
  271.     <p>The <em>CGI.pm</em> module works with Perl for Win32. It's
  272.     included with Perl for Win32, and you can use it in your
  273.     program with the statement <code>use CGI</code>. Complete
  274.     documentation for the <font size="-1">CGI</font> module is
  275.     embedded in the module, and can be read with <code>perldoc
  276.     CGI</code>. Further documentation is available at:</p>
  277.     <br />
  278. <pre>
  279.     <a href= 
  280. "http://www-genome.wi.mit.edu/ftp/pub/software/WWW/cgi_docs.html">http://www-genome.wi.mit.edu/ftp/pub/software/WWW/cgi_docs.html</a> 
  281. </pre>
  282.     <br />
  283.     <hr />
  284.  
  285.     <h2><a name="How_do_I_use_redirection_in_my_s">How do I use
  286.     redirection in my script?</a></h2>
  287.  
  288.     <p>You can use redirection to point the client browser to
  289.     another file or script, usually in a way that's invisible to
  290.     the user. The <font size="-1">CGI</font> module provides the
  291.     <code>redirect()</code> function for this purpose. The
  292.     following code redirects the client browser to <a href= 
  293.     "http://www.perl.com,">http://www.perl.com,</a> and works with
  294.     both <font size="-1">CGI</font> or PerlIS:</p>
  295.     <br />
  296. <pre>
  297.     use CGI qw(:standard);
  298.     print redirect('<a href=
  299. "http://www.perl.com">http://www.perl.com</a>');
  300. </pre>
  301.     <br />
  302.     <hr />
  303.  
  304.     <h2><a name="What_are_cookies_and_how_do_I_us">What are cookies
  305.     and how do I use them?</a></h2>
  306.  
  307.     <p>Cookies are packets of data that a server can give to a
  308.     client, such as a browser, to maintain information even after
  309.     the browser has left this site. This can be used to to identify
  310.     the client in future interactions or to store user preferences.
  311.     Cookies were first defined by Netscape, and the definition of
  312.     cookies can be found at:</p>
  313.     <br />
  314. <pre>
  315.     <a href=
  316. "http://home.netscape.com/newsref/std/cookie_spec.html">http://home.netscape.com/newsref/std/cookie_spec.html</a> 
  317. </pre>
  318.  
  319.     <p>The <font size="-1">CGI</font> module provides support for
  320.     managing cookies. See <a href=
  321.     "#What_CGI_modules_run_with_Perl_f">What CGI modules run with
  322.     Perl for Win32?</a> for information about the <font size="-1">
  323.     CGI</font> module, including the location of online
  324.     documentation.</p>
  325.     <br />
  326.     <hr />
  327.  
  328.     <h2><a name="How_do_I_get_the_e_mail_address_">How do I get the
  329.     e-mail address of the user?</a></h2>
  330.  
  331.     <p>It's not, in general, possible to get the e-mail address of
  332.     a user without asking them for it. If you need an e-mail
  333.     address, provide the user with a form to fill in an
  334.     address.</p>
  335.  
  336.     <p>The <font size="-1">HTTP</font> specification (see <a href= 
  337.     "#What_is_HTTP_and_how_do_I_get_m">What is HTTP, and how do I
  338.     get more information on it?</a>) defines an <font size="-1">
  339.     HTTP</font> request header field, <code>From:</code>, that can
  340.     contain the e-mail address of the user making the connection.
  341.     Your script can retrieve it using the environment variable
  342.     <font size="-1">HTTP_FROM.</font> However, it's rarely provided
  343.     and can't really be counted on.</p>
  344.  
  345.     <p>There are all kinds of sneaky ways to determine where
  346.     requests come from -- like checking the <font size="-1">
  347.     IP</font> address that was used to connect -- but you can't
  348.     figure out the e-mail address of the user this way.</p>
  349.  
  350.     <p>Generally, it's considered a violation of the user's privacy
  351.     to get their e-mail address without their knowledge. Perhaps
  352.     that's why it's so difficult.</p>
  353.     <br />
  354.     <hr />
  355.  
  356.     <h2><a name="I_need_a_CGI_script_to_do_a_cert">I need a CGI
  357.     script to do a certain task. Has anyone done it
  358.     before?</a></h2>
  359.  
  360.     <p>There are several <font size="-1">CGI</font> script archives
  361.     on the web. You may not be able to find exactly the script you
  362.     want, but you will probably find something that can be adapted
  363.     to your needs.</p>
  364.  
  365.     <p>A well known script page is:</p>
  366.  
  367.     <ul>
  368.       <li>
  369.         <p>Selena Sol's Script Archive: <a href= 
  370.         "http://www.extropia.com/Scripts/">
  371.         http://www.extropia.com/Scripts/</a></p>
  372.       </li>
  373.     </ul>
  374.  
  375.     <p>Note that most script archives have UNIX-targeted scripts,
  376.     and you may have to make some adaptations to them to make them
  377.     run (see <a href= 
  378.     "perlwin32faq5.html#How_do_I_make_a_UNIX_based_scrip">How do I
  379.     make a UNIX-based script work?</a>).</p>
  380.  
  381.     <p>This is one of the most common questions on the
  382.     Perl-Win32-Users list. Occasionally it's phrased more like
  383.     <font size="-1">``I</font> have a program to write. Here are
  384.     the specs. Please e-mail it to me by Friday.'' Hopefully the
  385.     reader can understand why these messages are usually answered
  386.     rudely, if they are answered at all.</p>
  387.     <br />
  388.     <hr />
  389.  
  390.     <h2><a name="How_do_I_test_my_CGI_programs_th">How do I test my
  391.     CGI programs that take arguments without running them through
  392.     the web server?</a></h2>
  393.  
  394.     <p>The hard way to test your <font size="-1">CGI</font>
  395.     programs from the command line is to set up environment
  396.     variables and a standard input stream just like a web server
  397.     would (see the <font size="-1">CGI</font> 1.1 specification for
  398.     more details on this -- see <a href= 
  399.     "#What_is_CGI_and_how_do_I_get_mo">What is CGI, and how do I
  400.     get more information on it?</a>).</p>
  401.  
  402.     <p>The easy way is to use <code>CGI.pm</code>, the Perl module
  403.     for <font size="-1">CGI</font> programming. It provides an easy
  404.     mechanism to run <font size="-1">CGI</font> programs, with
  405.     arguments, from the command line. See <a href= 
  406.     "#What_CGI_modules_run_with_Perl_f">What CGI modules run with
  407.     Perl for Win32?</a>.</p>
  408.     <br />
  409.     <hr />
  410.  
  411.     <h2><a name="The_Content_Type_header_field_I">The Content-Type:
  412.     header field I output from my CGI script shows up in the web
  413.     browser.</a></h2>
  414.  
  415.     <p>You are using an older version of Perl for <font size="-1">
  416.     ISAPI</font> as your Perl interpreter on an <font size="-1">
  417.     IIS</font> web server, but it's not outputting an <font size=
  418.     "-1">HTTP</font> status line. You should be able to fix this by
  419.     installing the most recent version of Perl and PerlIS. See <a
  420.     href="#My_CGI_scripts_don_t_seem_to_run">My CGI scripts don't
  421.     seem to run right under Perl for ISAPI.</a> for more
  422.     details.</p>
  423.     <br />
  424.     <hr />
  425.  
  426.     <h2><a name="When_I_try_to_run_a_CGI_script_f">When I try to
  427.     run a CGI script from my browser, it tries to download a file
  428.     of type "application/x-perl" instead.</a></h2>
  429.  
  430.     <p>Your web server has been misconfigured. It doesn't know that
  431.     it should execute your Perl program, so it's just returning it
  432.     to the browser. See <a href="perlwin32faq6.html#">the
  433.     perlwin32faq6 manpage</a> for details on how to configure your
  434.     particular web server to know that Perl scripts should be
  435.     executed.</p>
  436.     <br />
  437.     <hr />
  438.  
  439.     <h2><a name="What_s_Perl_for_WSAPI_">What's Perl for
  440.     WSAPI?</a></h2>
  441.  
  442.     <p>Perl for <font size="-1">WSAPI,</font> or PerlWS for short,
  443.     offers the same functionality as Perl for <font size="-1">
  444.     ISAPI</font> to users of O'Reilly's WebSite Professional web
  445.     server.</p>
  446.     <br />
  447.     <hr />
  448.  
  449.     <h1><a name="AUTHOR_AND_COPYRIGHT">AUTHOR AND
  450.     COPYRIGHT</a></h1>
  451.  
  452.     <p>This <font size="-1">FAQ</font> was originally assembled and
  453.     maintained by Evangelo Prodromou. <a href= 
  454.     "mailto:evangelo@endcontsw.com">evangelo@endcontsw.com.</a> It
  455.     has been revised and updated by Brian Jepson of O'Reilly and
  456.     Associates, and David Grove and David Dmytryshyn of
  457.     ActiveState.</p>
  458.  
  459.     <p>This <font size="-1">FAQ</font> is in the public domain. If
  460.     you use it, however, please ensure that you give credit to the
  461.     original authors.</p>
  462.  
  463.     <p><!-- beginning of leaf footer--></p>
  464.  
  465.     <table width="100%">
  466.       <tr>
  467.         <td bgcolor="000000" width="70" height="31"><a href= 
  468.         "http://www.activestate.com/"><img src="ASbutton.gif" alt= 
  469.         "ActiveState Home Page" border="0" width="68" height=
  470.         "30" /></a></td>
  471.  
  472.         <td width="10"> </td>
  473.  
  474.         <td valign="middle" bgcolor="#cc0066"><font face=
  475.         "sans-serif" size="+1" color="#ff99cc">   
  476.         Win32 FAQ</font></td>
  477.       </tr>
  478.     </table>
  479.     <!-- end  of leaf footer-->
  480.   </body>
  481. </html>
  482.  
  483.