<LI><A HREF="#my cgi script runs from the command line but not the browser. (500 server error)">My CGI script runs from the command line but not the browser. (500 Server Error)</A></LI>
<LI><A HREF="#how can i get better error messages from a cgi program">How can I get better error messages from a CGI program?</A></LI>
<LI><A HREF="#how do i remove html from a string">How do I remove HTML from a string?</A></LI>
<LI><A HREF="#how do i extract urls">How do I extract URLs?</A></LI>
<LI><A HREF="#how do i download a file from the user's machine how do i open a file on another machine">How do I download a file from the user's machine? How do I open a file on another machine?</A></LI>
<LI><A HREF="#how do i make a popup menu in html">How do I make a pop-up menu in HTML?</A></LI>
<LI><A HREF="#how do i fetch an html file">How do I fetch an HTML file?</A></LI>
<LI><A HREF="#how do i automate an html form submission">How do I automate an HTML form submission?</A></LI>
<LI><A HREF="#how do i decode or create those %encodings on the web">How do I decode or create those %-encodings on the web?</A></LI>
<LI><A HREF="#how do i redirect to another page">How do I redirect to another page?</A></LI>
<LI><A HREF="#how do i put a password on my web pages">How do I put a password on my web pages?</A></LI>
<LI><A HREF="#how do i edit my .htpasswd and .htgroup files with perl">How do I edit my .htpasswd and .htgroup files with Perl?</A></LI>
<LI><A HREF="#how do i make sure users can't enter values into a form that cause my cgi script to do bad things">How do I make sure users can't enter values into a form that cause my CGI script to do bad things?</A></LI>
<LI><A HREF="#how do i parse a mail header">How do I parse a mail header?</A></LI>
<LI><A HREF="#how do i decode a cgi form">How do I decode a CGI form?</A></LI>
<LI><A HREF="#how do i check a valid mail address">How do I check a valid mail address?</A></LI>
<LI><A HREF="#how do i decode a mime/base64 string">How do I decode a MIME/BASE64 string?</A></LI>
<LI><A HREF="#how do i return the user's mail address">How do I return the user's mail address?</A></LI>
<LI><A HREF="#how do i send mail">How do I send mail?</A></LI>
<LI><A HREF="#how do i read mail">How do I read mail?</A></LI>
<LI><A HREF="#how do i find out my hostname/domainname/ip address">How do I find out my hostname/domainname/IP address?</A></LI>
<LI><A HREF="#how do i fetch a news article or the active newsgroups">How do I fetch a news article or the active newsgroups?</A></LI>
<LI><A HREF="#how do i fetch/put an ftp file">How do I fetch/put an FTP file?</A></LI>
<LI><A HREF="#how can i do rpc in perl">How can I do RPC in Perl?</A></LI>
</UL>
<LI><A HREF="#author and copyright">AUTHOR AND COPYRIGHT</A></LI>
<P>This section deals with questions related to networking, the internet,
and a few on the web.</P>
<P>
<H2><A NAME="my cgi script runs from the command line but not the browser. (500 server error)">My CGI script runs from the command line but not the browser. (500 Server Error)</A></H2>
<P>If you can demonstrate that you've read the following FAQs and that
your problem isn't something simple that can be easily answered, you'll
probably receive a courteous and useful reply to your question if you
post it on comp.infosystems.www.authoring.cgi (if it's something to do
with HTTP, HTML, or the CGI protocols). Questions that appear to be Perl
questions but are really CGI ones that are posted to comp.lang.perl.misc
<H2><A NAME="how can i get better error messages from a cgi program">How can I get better error messages from a CGI program?</A></H2>
<P>Use the CGI::Carp module. It replaces <A HREF="../../lib/Pod/perlfunc.html#item_warn"><CODE>warn</CODE></A> and <A HREF="../../lib/Pod/perlfunc.html#item_die"><CODE>die</CODE></A>, plus the
normal Carp modules <CODE>carp</CODE>, <CODE>croak</CODE>, and <CODE>confess</CODE> functions with
more verbose and safer versions. It still sends them to the normal
server error log.</P>
<PRE>
use CGI::Carp;
warn "This is a complaint";
die "But this one is serious";</PRE>
<P>The following use of CGI::Carp also redirects errors to a file of your choice,
placed in a BEGIN block to catch compile-time warnings as well:</P>
<PRE>
BEGIN {
use CGI::Carp qw(carpout);
open(LOG, ">>/var/local/cgi-logs/mycgi-log")
or die "Unable to append to mycgi-log: $!\n";
carpout(*LOG);
}</PRE>
<P>You can even arrange for fatal errors to go back to the client browser,
which is nice for your own debugging, but might confuse the end user.</P>
<PRE>
use CGI::Carp qw(fatalsToBrowser);
die "Bad error here";</PRE>
<P>Even if the error happens before you get the HTTP header out, the module
will try to take care of this to avoid the dreaded server 500 errors.
Normal warnings still go out to the server error log (or wherever
you've sent them with <CODE>carpout</CODE>) with the application name and date
stamp prepended.</P>
<P>
<H2><A NAME="how do i remove html from a string">How do I remove HTML from a string?</A></H2>
<P>The most correct way (albeit not the fastest) is to use HTML::Parser
from CPAN. Another mostly correct
way is to use HTML::FormatText which not only removes HTML but also
attempts to do a little simple formatting of the resulting plain text.</P>
<P>Many folks attempt a simple-minded regular expression approach, like
<CODE>s/<.*?>//g</CODE>, but that fails in many cases because the tags
may continue over line breaks, they may contain quoted angle-brackets,
or HTML comment may be present. Plus folks forget to convert
entities, like <CODE><</CODE> for example.</P>
<P>Here's one ``simple-minded'' approach, that works for most files:</P>
<PRE>
#!/usr/bin/perl -p0777
s/<(?:[^>'"]*|(['"]).*?\1)*>//gs</PRE>
<P>If you want a more complete solution, see the 3-stage striphtml
<H2><A NAME="how do i download a file from the user's machine how do i open a file on another machine">How do I download a file from the user's machine? How do I open a file on another machine?</A></H2>
<P>In the context of an HTML form, you can use what's known as
<STRONG>multipart/form-data</STRONG> encoding. The CGI.pm module (available from
CPAN) supports this in the <CODE>start_multipart_form()</CODE> method, which isn't
the same as the <CODE>startform()</CODE> method.</P>
<P>
<H2><A NAME="how do i make a popup menu in html">How do I make a pop-up menu in HTML?</A></H2>
<P>Use the <STRONG><SELECT</STRONG> >> and <STRONG><OPTION</STRONG> >> tags. The CGI.pm
module (available from CPAN) supports this widget, as well as many
others, including some that it cleverly synthesizes on its own.</P>
<P>
<H2><A NAME="how do i fetch an html file">How do I fetch an HTML file?</A></H2>
<P>One approach, if you have the lynx text-based HTML browser installed
on your system, is this:</P>
<PRE>
$html_code = `lynx -source $url`;
$text_data = `lynx -dump $url`;</PRE>
<P>The libwww-perl (LWP) modules from CPAN provide a more powerful way
to do this. They don't require lynx, but like lynx, can still work
<P>To be correct to the spec, each of those virtual newlines should really be
physical <CODE>"\015\012"</CODE> sequences by the time you hit the client browser.
Except for NPH scripts, though, that local newline should get translated
by your server into standard form, so you shouldn't have a problem
here, even if you are stuck on MacOS. Everybody else probably won't
even notice.</P>
<P>
<H2><A NAME="how do i put a password on my web pages">How do I put a password on my web pages?</A></H2>
<P>That depends. You'll need to read the documentation for your web
server, or perhaps check some of the other FAQs referenced above.</P>
<P>
<H2><A NAME="how do i edit my .htpasswd and .htgroup files with perl">How do I edit my .htpasswd and .htgroup files with Perl?</A></H2>
<P>The HTTPD::UserAdmin and HTTPD::GroupAdmin modules provide a
consistent OO interface to these files, regardless of how they're
stored. Databases may be text, dbm, Berkley DB or any database with a
DBI compatible driver. HTTPD::UserAdmin supports files used by the
`Basic' and `Digest' authentication schemes. Here's an example:</P>
<PRE>
use HTTPD::UserAdmin ();
HTTPD::UserAdmin
->new(DB => "/foo/.htpasswd")
->add($username => $password);</PRE>
<P>
<H2><A NAME="how do i make sure users can't enter values into a form that cause my cgi script to do bad things">How do I make sure users can't enter values into a form that cause my CGI script to do bad things?</A></H2>
<P>Read the CGI security FAQ, at
<A HREF="http://www-genome.wi.mit.edu/WWW/faqs/www-security-faq.html,">http://www-genome.wi.mit.edu/WWW/faqs/www-security-faq.html,</A> and the
<P>In brief: use tainting (see <A HREF="../../lib/Pod/perlsec.html">the perlsec manpage</A>), which makes sure that data
from outside your script (eg, CGI parameters) are never used in
<A HREF="../../lib/Pod/perlfunc.html#item_eval"><CODE>eval</CODE></A> or <A HREF="../../lib/Pod/perlfunc.html#item_system"><CODE>system</CODE></A> calls. In addition to tainting, never use the
single-argument form of <A HREF="../../lib/Pod/perlfunc.html#item_system"><CODE>system()</CODE></A> or exec(). Instead, supply the
command and arguments as a list, which prevents shell globbing.</P>
<P>
<H2><A NAME="how do i parse a mail header">How do I parse a mail header?</A></H2>
<P>For a quick-and-dirty solution, try this solution derived
from page 222 of the 2nd edition of ``Programming Perl'':</P>