home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Chip 2000 May
/
Chip_2000-05_cd1.bin
/
zkuste
/
Perl
/
ActivePerl-5.6.0.613.msi
/
䆊䌷䈹䈙䏵-䞅䞆䞀㡆䞃䄦䠥
/
_4e82bea3644a013377722515936aebd2
< prev
next >
Wrap
Text File
|
2000-03-22
|
14KB
|
259 lines
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<HEAD>
<TITLE>ActivePerl faq7 - Web Programming</TITLE>
<LINK rev="made" href="mailto:support@ActiveState.com">
<META name="GENERATOR" charset="iso-8859-1" content="Microsoft FrontPage 4.0">
<META name="ProgId" content="FrontPage.Editor.Document">
<LINK rel="STYLESHEET" href="../../Active.css" type="text/css" media="screen">
</HEAD>
<BODY bgcolor="#ffffff">
<TABLE border="0" cellpadding="0" cellspacing="0" width="100%">
<TR>
<TD class="block" valign="MIDDLE" width="100%" bgcolor="#cccccc"><STRONG>
<P class="block"> ActivePerl FAQ</P>
</STRONG></TD>
</TR>
</TABLE>
<UL>
<LI><A href="#NAME">NAME</A></LI>
<LI><A href="#DESCRIPTION">DESCRIPTION</A>
<UL>
<LI><A href="#What_is_HTTP_and_how_do_I_get_m">What is HTTP, and how do I get more information
on it?</A></LI>
<LI><A href="#What_is_CGI_and_how_do_I_get_mo">What is CGI, and how do I get more information
on it?</A></LI>
<LI><A href="#How_do_I_return_a_graphics_file_">How do I return a graphics file from a CGI
script?</A></LI>
<LI><A href="#My_CGI_scripts_don_t_seem_to_run">My CGI scripts don't seem to run right under
PerlIS.</A></LI>
<LI><A href="#How_does_my_script_know_if_it_s_">How does my script know if it's running under
ActivePerl or</A></LI>
<LI><A href="#What_CGI_modules_run_with_Perl_f">What CGI modules run with ActivePerl?</A></LI>
<LI><A href="#How_do_I_use_redirection_in_my_s">How do I use redirection in my script?</A></LI>
<LI><A href="#What_are_cookies_and_how_do_I_us">What are cookies and how do I use them?</A></LI>
<LI><A href="#How_do_I_get_the_e_mail_address_">How do I get the e-mail address of the user?</A></LI>
<LI><A href="#I_need_a_CGI_script_to_do_a_cert">I need a CGI script to do a certain task. Has
anyone done it before?</A></LI>
<LI><A href="#How_do_I_test_my_CGI_programs_th">How do I test my CGI programs that take
arguments without running</A></LI>
<LI><A href="#The_Content_Type_header_field_I">The Content-Type: header field I output from my
CGI script shows up</A></LI>
<LI><A href="#When_I_try_to_run_a_CGI_script_f">When I try to run a CGI script from my
browser, it tries to</A></LI>
<LI><A href="#What_s_Perl_for_WSAPI_">What's Perl for WSAPI?</A></LI>
</UL>
</LI>
<LI><A href="#AUTHOR_AND_COPYRIGHT">AUTHOR AND COPYRIGHT</A></LI>
</UL>
<HR>
<H1><A name="NAME">NAME</A></H1>
<P>ActivePerl faq7 - Web Programming (CGI and PerlIS)</P>
<HR>
<H1><A name="DESCRIPTION">DESCRIPTION</A></H1>
<P>Web development with ActivePerl</P>
<HR>
<H2><A name="What_is_HTTP_and_how_do_I_get_m">What is HTTP, and how do I get more information on it?</A></H2>
<P>HTTP (HyperText Transfer Protocol) is the protocol that web browsers use to talk to web servers.
The official specification for the HTTP standard is available on the W3 Consortium web server at:</P>
<PRE>
<A href="http://www.w3.org/pub/WWW/Protocols/">http://www.w3.org/pub/WWW/Protocols/</A>
</PRE>
<P>Some more readable introductions can be found on the Yahoo HTTP page at:</P>
<PRE>
<A href="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>
</PRE>
<HR>
<H2><A name="What_is_CGI_and_how_do_I_get_mo">What is CGI, and how do I get more information on it?</A></H2>
<P>CGI (Common Gateway Interface) is a protocol used by web servers to run server programs. Scripts
that support the CGI protocol are sometimes called ``CGI scripts''; this leads to the unfortunate
misperception that CGI is a language of its own.</P>
<P>The classic information on CGI is available on the NCSA server at:</P>
<PRE>
<A href="http://hoohoo.ncsa.uiuc.edu/cgi/">http://hoohoo.ncsa.uiuc.edu/cgi/</A>
</PRE>
<P>If you haven't read this, read it now. If it doesn't click for you, you can check the Yahoo CGI
page at:</P>
<PRE>
<A href="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>
</PRE>
<P>(All one URL)</P>
<P>Try also the following URL for CGI programming in Perl:</P>
<PRE>
<A href="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>
</PRE>
<P>If you still don't get it, try one of the ``get-rich-quick-by-writing-CGI-scripts'' books at your
local bookstores.</P>
<HR>
<H2><A name="How_do_I_return_a_graphics_file_">How do I return a graphics file from a CGI script?</A></H2>
<P>One of the big differences between UNIX and Win32 platforms is that on Win32 there's a difference
between text or ASCII files and binary file. To return a graphics file, you need to specify that the
file is a binary file, and that the standard output stream should accept binary data. Try something
like this:</P>
<PRE>
$MY_FILE_NAME = 'Penelope.jpg';
$CHUNK_SIZE = 4096;
open( MY_FILE, "<$MY_FILE_NAME" )
or die( "Can't open $MY_FILE_NAME: $!\n" );
print "Content-type: image/jpeg\r\n";
print "\r\n";
binmode( MY_FILE ); # These are crucial!
binmode( STDOUT );
while ( $cb = read( MY_FILE, $data, $CHUNK_SIZE ) ) {
print $data;
}
close( MY_FILE );
</PRE>
<HR>
<H2><A name="My_CGI_scripts_don_t_seem_to_run">My CGI scripts don't seem to run right under PerlIS.</A></H2>
<P>Earlier versions of Perl for ISAPI did not output headers correctly, and this caused Perl for
ISAPI scripts to misbehave. You could work around this in earlier versions of PerlIS by sending the
headers at the beginning of your script like this:</P>
<PRE>
print <<"END";
HTTP/1.0 200 OK
Content-Type: text/html
END
</PRE>
<P>This is no longer necessary with the current version of Perl for ISAPI, as Perl for ISAPI sends
the correct headers. The Registry value that governs this is <CODE>EnableCGIHeader</CODE>, a
REG_DWORD value that is set to 1 by default. This value is stored under the Registry key
HKEY_LOCAL_MACHINE\SOFTWARE\ActiveState\PerlIS. If you need to turn off the automatic generation of
the header, set this value to 0.</P>
<HR>
<H2><A name="How_does_my_script_know_if_it_s_">How does my script know if it's running under
ActivePerl or Perl for ISAPI?</A></H2>
<P>Perl for ISAPI sets an environment variable, <CODE>PERLXS</CODE>, for scripts that are executed
under Perl for ISAPI.</P>
<PRE>
my $running_under_perlis = $ENV{PERLXS} eq 'PerlIS';
</PRE>
<HR>
<H2><A name="How_does_my_script_know_if_it_s_">How does my script know if it's running under
ActivePerl or Perl for WebSite?</A></H2>
<P>Perl for WebSite sets an environment variable, <CODE>PERLXS</CODE>, for scripts that are executed
under Perl for WebSite.</P>
<PRE>
my $running_under_perlws = $ENV{PERLXS} eq 'PerlWS';
</PRE>
<HR>
<H2><A name="What_CGI_modules_run_with_Perl_f">What CGI modules run with ActivePerl?</A></H2>
<P>The <EM>CGI.pm</EM> module works with ActivePerl. It's included with ActivePerl, and you can use
it in your program with the statement <CODE>use CGI</CODE>. Complete documentation for the CGI
module is embedded in the module, and can be read with <CODE>perldoc CGI</CODE>. Further
documentation is available at:</P>
<PRE>
<A href="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>
</PRE>
<HR>
<H2><A name="How_do_I_use_redirection_in_my_s">How do I use redirection in my script?</A></H2>
<P>You can use redirection to point the client browser to another file or script, usually in a way
that's invisible to the user. The CGI module provides the <CODE>redirect()</CODE> function for this
purpose. The following code redirects the client browser to <A href="http://www.perl.com">http://www.perl.com</A>
and works with both CGI or PerlIS:</P>
<PRE>
use CGI qw(:standard);
print redirect('<A href="http://www.perl.com">http://www.perl.com</A>');
</PRE>
<HR>
<H2><A name="What_are_cookies_and_how_do_I_us">What are cookies and how do I use them?</A></H2>
<P>Cookies are packets of data that a server can give to a client, such as a browser, to maintain
information even after the browser has left this site. This can be used to to identify the client in
future interactions or to store user preferences. Cookies were first defined by Netscape, and the
definition of cookies can be found at:</P>
<PRE>
<A href="http://home.netscape.com/newsref/std/cookie_spec.html">http://home.netscape.com/newsref/std/cookie_spec.html</A>
</PRE>
<P>The CGI module provides support for managing cookies. See <A href="#What_CGI_modules_run_with_Perl_f">What
CGI modules run with ActivePerl?</A> for information about the CGI module, including the location of
online documentation.</P>
<HR>
<H2><A name="How_do_I_get_the_e_mail_address_">How do I get the e-mail address of the user?</A></H2>
<P>It's not, in general, possible to get the e-mail address of a user without asking them for it. If
you need an e-mail address, provide the user with a form to fill in an address.</P>
<P>The HTTP specification (see <A href="#What_is_HTTP_and_how_do_I_get_m">What is HTTP, and how do I
get more information on it?</A>) defines an HTTP request header field, <CODE>From:</CODE>, that can
contain the e-mail address of the user making the connection. Your script can retrieve it using the
environment variable HTTP_FROM. However, it's rarely provided and can't really be counted on.</P>
<P>There are all kinds of sneaky ways to determine where requests come from -- like checking the IP
address that was used to connect -- but you can't figure out the e-mail address of the user this
way.</P>
<P>Generally, it's considered a violation of the user's privacy to get their e-mail address without
their knowledge. Perhaps that's why it's so difficult.</P>
<HR>
<H2><A name="I_need_a_CGI_script_to_do_a_cert">I need a CGI script to do a certain task. Has anyone
done it before?</A></H2>
<P>There are several CGI script archives on the web. You may not be able to find exactly the script
you want, but you will probably find something that can be adapted to your needs.</P>
<P>Two of the most famous script pages are:</P>
<UL>
<LI>
<P>Selena Sol's Script Archive: <A href="http://www.extropia.com/Scripts/">http://www.extropia.com/scripts/</A></P>
</LI>
<LI>
<P>Matt's Script Archive: <A href="http://www.worldwidemart.com/scripts/">http://www.worldwidemart.com/scripts/</A></P>
</LI>
</UL>
<P>Note that most script archives have UNIX-targeted scripts, and you may have to make some
adaptations to them to make them run (see <A href="ActivePerl-Winfaq5.html#How_do_I_make_a_UNIX_based_scrip">How
do I make a UNIX-based script work?</A>).</P>
<P>This is one of the most common questions on the Perl-Win32-Users list. Occasionally it's phrased
more like ``I have a program to write. Here are the specs. Please e-mail it to me by Friday.''
Hopefully the reader can understand why these messages are usually answered rudely, if they are
answered at all.</P>
<HR>
<H2><A name="How_do_I_test_my_CGI_programs_th">How do I test my CGI programs that take arguments
without running them through the web server?</A></H2>
<P>The hard way to test your CGI programs from the command line is to set up environment variables
and a standard input stream just like a web server would (see the CGI 1.1 specification for more
details on this -- see <A href="#What_is_CGI_and_how_do_I_get_mo">What is CGI, and how do I get more
information on it?</A>).</P>
<P>The easy way is to use <CODE>CGI.pm</CODE>, the Perl module for CGI programming. It provides an
easy mechanism to run CGI programs, with arguments, from the command line. See <A href="#What_CGI_modules_run_with_Perl_f">What
CGI modules run with ActivePerl?</A>.</P>
<HR>
<H2><A name="The_Content_Type_header_field_I">The Content-Type: header field I output from my CGI
script shows up in the web browser.</A></H2>
<P>You are using an older version of Perl for ISAPI as your Perl interpreter on an IIS web server,
but it's not outputting an HTTP status line. You should be able to fix this by installing the most
recent version of Perl and PerlIS. See <A href="#My_CGI_scripts_don_t_seem_to_run">My CGI scripts
don't seem to run right under Perl for ISAPI.</A> for more details.</P>
<HR>
<H2><A name="When_I_try_to_run_a_CGI_script_f">When I try to run a CGI script from my browser, it
tries to download a file of type "application/x-perl" instead.</A></H2>
<P>Your web server has been misconfigured. It doesn't know that it should execute your Perl program,
so it's just returning it to the browser. See <A href="ActivePerl-Winfaq6.html#">the perlwin32faq6
manpage</A> for details on how to configure your particular web server to know that Perl scripts
should be executed.</P>
<HR>
<H2><A name="What_s_Perl_for_WSAPI_">What's Perl for WSAPI?</A></H2>
<P>Perl for WSAPI, or PerlWS for short, offers the same functionality as Perl for ISAPI to users of
O'Reilly's WebSite Professional web server.</P>
<HR>
<H1><A name="AUTHOR_AND_COPYRIGHT">AUTHOR AND COPYRIGHT</A></H1>
<P>This FAQ was originally assembled and maintained by Evangelo Prodromou. It has been revised and
updated by Brian Jepson of O'Reilly & Associates, David Grove, David Dmytryshyn and David Sparks
of ActiveState.</P>
<P>This FAQ is in the public domain. If you use it, however, please ensure that you give credit to
the original authors.</P>
<TABLE border="0" cellpadding="0" cellspacing="0" width="100%">
<TR>
<TD class="block" valign="MIDDLE" width="100%" bgcolor="#cccccc"><STRONG>
<P class="block"> ActivePerl FAQ</P>
</STRONG></TD>
</TR>
</TABLE>
</BODY>
</HTML>