Seriously, 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 may not be so well received.
The useful FAQs are:
http://www.perl.com/perl/faq/idiots-guide.html http://www3.pair.com/webthing/docs/cgi/faqs/cgifaq.shtml http://www.perl.com/perl/faq/perl-cgi-faq.html http://www-genome.wi.mit.edu/WWW/faqs/www-security-faq.html http://www.boutell.com/faq/
Many folks attempt a simple-minded regular expression approach, like
s/<.*?>//g
, 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 <
for example.
Here's one ``simple-minded'' approach, that works for most files:
#!/usr/bin/perl -p0777 s/<(?:[^>'"]*|(['"]).*?\1)*>//gs
If you want a more complete solution, see the 3-stage striphtml program in http://www.perl.com/CPAN/authors/Tom_Christiansen/scripts/striphtml.gz .
#!/usr/bin/perl -n00 # qxurl - tchrist@perl.com print "$2\n" while m{ < \s* A \s+ HREF \s* = \s* (["']) (.*?) \1 \s* > }gsix;
This version does not adjust relative URLs, understand alternate bases, deal with HTML comments, or accept URLs themselves as arguments. It also runs about 100x faster than a more ``complete'' solution using the LWP suite of modules, such as the http://www.perl.com/CPAN/authors/Tom_Christiansen/scripts/xurl.gz program.
start_multipart_form
method, which isn't the same as the
startform
method.
$html_code = `lynx -source $url`; $text_data = `lynx -dump $url`;
$string = "http://altavista.digital.com/cgi-bin/query?pg=q&what=news&fmt=.&q=%2Bcgi-bin+%2Bperl.exe"; $string =~ s/%([a-fA-F0-9]{2})/chr(hex($1))/ge;
Encoding is a bit harder, because you can't just blindly change all the
non-alphanumunder character (\W
) into their hex escapes. It's important that characters with special
meaning like /
and ?
not be translated. Probably the easiest way to get this right is to avoid
reinventing the wheel and just use the URI::Escape module, which is part of
the libwww-perl package (LWP) available from CPAN.
Content-Type
as the headers of your reply, send back a Location:
header. Officially this should be a
URI:
header, so the CGI.pm module (available from CPAN) sends back both:
Location: http://www.domain.com/newpage URI: http://www.domain.com/newpage
Note that relative URLs in these headers can cause strange effects because of ``optimizations'' that servers do.
use HTTPD::UserAdmin (); HTTPD::UserAdmin ->new(DB => "/foo/.htpasswd") ->add($username => $password);
$/ = ''; $header = <MSG>; $header =~ s/\n\s+/ /g; # merge continuation lines %head = ( UNIX_FROM_LINE, split /^([-\w]+):\s*/m, $header );
That solution doesn't do well if, for example, you're trying to maintain all the Received lines. A more complete approach is to use the Mail::Header module from CPAN (part of the MailTools package).
$ENV{CONTENT_LENGTH}
and
$ENV{QUERY_STRING}
. It's true that this can work, but there are also a lot of versions of
this floating around that are quite simply broken!
Please do not be tempted to reinvent the wheel. Instead, use the CGI.pm or CGI_Lite.pm (available from CPAN), or if you're trapped in the module-free land of perl1 .. perl4, you might look into cgi-lib.pl (available from http://www.bio.cam.ac.uk/web/form.html).
Without sending mail to the address and seeing whether it bounces (and even then you face the halting problem), you cannot determine whether an email address is valid. Even if you apply the email header standard, you can have problems, because there are deliverable addresses that aren't RFC-822 (the mail header standard) compliant, and addresses that aren't deliverable which are compliant.
Many are tempted to try to eliminate many frequently-invalid email
addresses with a simple regexp, such as
/^[\w.-]+\@+\w+$/
. However, this also throws out many valid ones, and says nothing about
potential deliverability, so is not suggested. Instead, see http://www.perl.com/CPAN/authors/Tom_Christiansen/scripts/ckaddr.gz
, which actually checks against the full RFC spec (except for nested
comments), looks for addresses you may not wish to accept email to (say,
Bill Clinton or your postmaster), and then makes sure that the hostname
given can be looked up in DNS. It's not fast, but it works.
use MIME::base64; $decoded = decode_base64($encoded);
A more direct approach is to use the unpack function's ``u'' format after minor transliterations:
tr#A-Za-z0-9+/##cd; # remove non-base64 chars tr#A-Za-z0-9+/# -_#; # convert to uuencoded format $len = pack("c", 32 + 0.75*length); # compute length byte print unpack("u", $len . $_); # uudecode and print
use Sys::Hostname; $address = sprintf('%s@%s', getpwuid($<), hostname);
Company policies on email address can mean that this generates addresses that the company's email system will not accept, so you should ask for users' email addresses when this matters. Furthermore, not all systems on which Perl runs are so forthcoming with this information as is Unix.
The Mail::Util module from CPAN (part of the MailTools package) provides a
mailaddress
function that tries to guess the mail address of
the user. It makes a more intelligent guess than the code above, using
information given when the module was installed, but it could still be
incorrect. Again, the best way is often just to ask the user.
`hostname`
program. While sometimes expedient, this isn't very portable. It's one of
those tradeoffs of convenience versus portability.
The Sys::Hostname module (part of the standard perl distribution) will give you the hostname after which you can find out the IP address (assuming you have working DNS) with a gethostbyname call.
use Socket; use Sys::Hostname; my $host = hostname(); my $addr = inet_ntoa(scalar(gethostbyname($name)) || 'localhost');
Probably the simplest way to learn your DNS domain name is to grok it out of /etc/resolv.conf, at least under Unix. Of course, this assumes several things about your resolv.conf configuration, including that it exists.
(We still need a good DNS domain name-learning method for non-Unix systems.)
perl -MNews::NNTPClient -e 'print News::NNTPClient->new->list("newsgroups")'