=head2 How do I decode or create those %-encodings on the web?
-Here's an example of decoding:
- $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;
+If you are writing a CGI script, you should be using the CGI.pm module
+that comes with perl, or some other equivalent module. The CGI module
+automatically decodes queries for you, and provides an escape()
+function to handle encoding.
-Encoding is a bit harder, because you can't just blindly change
-all characters that are not letters, digits or underscores (C<\W>)
-into their hex escapes.
-It's important that characters with special meaning like C</> and C<?>
-I<not> be translated. Probably the easiest way to get this right is
-to avoid reinventing the wheel and just use the URI::Escape module,
-available from CPAN.
+
+The best source of detailed information on URI encoding is RFC 2396.
+Basically, the following substitutions do it:
+
+ s/([^\w()'*~!.-])/sprintf '%%%02x', $1/eg; # encode
+
+ s/%([A-Fa-f\d]{2})/chr hex $1/eg; # decode
+
+However, you should only apply them to individual URI components, not
+the entire URI, otherwise you'll lose information and generally mess
+things up. If that didn't explain it, don't worry. Just go read
+section 2 of the RFC, it's probably the best explanation there is.
+
+RFC 2396 also contains a lot of other useful information, including a
+regexp for breaking any arbitrary URI into components (Appendix B).
=head2 How do I redirect to another page?
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
+stored. Databases may be text, dbm, Berkeley 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:
use HTTPD::UserAdmin ();
Many are tempted to try to eliminate many frequently-invalid
mail addresses with a simple regex, such as
-C</^[\w.-]+\@([\w.-]\.)+\w+$/>. It's a very bad idea. However,
+C</^[\w.-]+\@(?:[\w-]+\.)+\w+$/>. It's a very bad idea. 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 ,
+potential deliverability, so it 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 mail to
(say, Bill Clinton or your postmaster), and then makes sure that the
=head2 How do I decode a MIME/BASE64 string?
-The MIME-tools package (available from CPAN) handles this and a lot
-more. Decoding BASE64 becomes as simple as:
+The MIME-Base64 package (available from CPAN) handles this as well as
+the MIME/QP encoding. Decoding BASE64 becomes as simple as:
- use MIME::base64;
+ use MIME::Base64;
$decoded = decode_base64($encoded);
-A more direct approach is to use the unpack() function's "u"
+The MIME-Tools package (available from CPAN) supports extraction with
+decoding of BASE64 encoded attachments and content directly from email
+messages.
+
+If the string to decode is short (less than 84 bytes long)
+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
are many reasons to use a mail transport agent like sendmail. These
include queueing, MX records, and security.
+=head2 How do I use MIME to make an attachment to a mail message?
+
+This answer is extracted directly from the MIME::Lite documentation.
+Create a multipart message (i.e., one with attachments).
+
+ use MIME::Lite;
+
+ ### Create a new multipart message:
+ $msg = MIME::Lite->new(
+ From =>'me@myhost.com',
+ To =>'you@yourhost.com',
+ Cc =>'some@other.com, some@more.com',
+ Subject =>'A message with 2 parts...',
+ Type =>'multipart/mixed'
+ );
+
+ ### Add parts (each "attach" has same arguments as "new"):
+ $msg->attach(Type =>'TEXT',
+ Data =>"Here's the GIF file you wanted"
+ );
+ $msg->attach(Type =>'image/gif',
+ Path =>'aaa000123.gif',
+ Filename =>'logo.gif'
+ );
+
+ $text = $msg->as_string;
+
+MIME::Lite also includes a method for sending these things.
+
+ $msg->send;
+
+This defaults to using L<sendmail(1)> but can be customized to use
+SMTP via L<Net::SMTP>.
+
=head2 How do I read mail?
While you could use the Mail::Folder module from CPAN (part of the