perlfaq9.html

来自「perl教程」· HTML 代码 · 共 717 行 · 第 1/4 页

HTML
717 行
字号
your web server.  The configuration is different for different sorts
of web servers---apache does it differently from iPlanet which does
it differently from IIS.  Check your web server documentation for
the details for your particular server.</p>
<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, Berkeley DB or any database with
a DBI compatible driver.  HTTPD::UserAdmin supports files used by the
&quot;Basic&quot; and &quot;Digest&quot; authentication schemes.  Here's an example:</p>
<pre>
    <span class="keyword">use</span> <span class="variable">HTTPD::UserAdmin</span> <span class="operator">();</span>
    <span class="variable">HTTPD::UserAdmin</span>
          <span class="operator">-&gt;</span><span class="variable">new</span><span class="operator">(</span><span class="string">DB</span> <span class="operator">=&gt;</span> <span class="string">"/foo/.htpasswd"</span><span class="operator">)</span>
          <span class="operator">-&gt;</span><span class="variable">add</span><span class="operator">(</span><span class="variable">$username</span> <span class="operator">=&gt;</span> <span class="variable">$password</span><span class="operator">);</span>
</pre>
<p>
</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>See the security references listed in the CGI Meta FAQ</p>
<pre>
        <a href="http://www.perl.org/CGI_MetaFAQ.html">http://www.perl.org/CGI_MetaFAQ.html</a></pre>
<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 <a href="../../lib/Pod/perlfunc.html#split">split in the perlfunc manpage</a>:</p>
<pre>
    <span class="variable">$/</span> <span class="operator">=</span> <span class="string">''</span><span class="operator">;</span>
    <span class="variable">$header</span> <span class="operator">=</span> <span class="operator">&lt;</span><span class="variable">MSG</span><span class="operator">&gt;;</span>
    <span class="variable">$header</span> <span class="operator">=~</span> <span class="regex">s/\n\s+/ /g</span><span class="operator">;</span>      <span class="comment"># merge continuation lines</span>
    <span class="variable">%head</span> <span class="operator">=</span> <span class="operator">(</span> <span class="variable">UNIX_FROM_LINE</span><span class="operator">,</span> <span class="keyword">split</span> <span class="regex">/^([-\w]+):\s*/m</span><span class="operator">,</span> <span class="variable">$header</span> <span class="operator">);</span>
</pre>
<p>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).</p>
<p>
</p>
<h2><a name="how_do_i_decode_a_cgi_form">How do I decode a CGI form?</a></h2>
<p>(contributed by brian d foy)</p>
<p>Use the CGI.pm module that comes with Perl.  It's quick,
it's easy, and it actually does quite a bit of work to
ensure things happen correctly.  It handles GET, POST, and
HEAD requests, multipart forms, multivalued fields, query
string and message body combinations, and many other things
you probably don't want to think about.</p>
<p>It doesn't get much easier: the CGI module automatically
parses the input and makes each value available through the
<code>param()</code> function.</p>
<pre>
        <span class="keyword">use</span> <span class="variable">CGI</span> <span class="string">qw(:standard)</span><span class="operator">;</span>
</pre>
<pre>
        <span class="keyword">my</span> <span class="variable">$total</span> <span class="operator">=</span> <span class="variable">param</span><span class="operator">(</span> <span class="string">'price'</span> <span class="operator">)</span> <span class="operator">+</span> <span class="variable">param</span><span class="operator">(</span> <span class="string">'shipping'</span> <span class="operator">);</span>
</pre>
<pre>
        <span class="keyword">my</span> <span class="variable">@items</span> <span class="operator">=</span> <span class="variable">param</span><span class="operator">(</span> <span class="string">'item'</span> <span class="operator">);</span> <span class="comment"># multiple values, same field name</span>
</pre>
<p>If you want an object-oriented approach, CGI.pm can do that too.</p>
<pre>
        <span class="keyword">use</span> <span class="variable">CGI</span><span class="operator">;</span>
</pre>
<pre>
        <span class="keyword">my</span> <span class="variable">$cgi</span> <span class="operator">=</span> <span class="variable">CGI</span><span class="operator">-&gt;</span><span class="variable">new</span><span class="operator">();</span>
</pre>
<pre>
        <span class="keyword">my</span> <span class="variable">$total</span> <span class="operator">=</span> <span class="variable">$cgi</span><span class="operator">-&gt;</span><span class="variable">param</span><span class="operator">(</span> <span class="string">'price'</span> <span class="operator">)</span> <span class="operator">+</span> <span class="variable">$cgi</span><span class="operator">-&gt;</span><span class="variable">param</span><span class="operator">(</span> <span class="string">'shipping'</span> <span class="operator">);</span>
</pre>
<pre>
        <span class="keyword">my</span> <span class="variable">@items</span> <span class="operator">=</span> <span class="variable">$cgi</span><span class="operator">-&gt;</span><span class="variable">param</span><span class="operator">(</span> <span class="string">'item'</span> <span class="operator">);</span>
</pre>
<p>You might also try CGI::Minimal which is a lightweight version
of the same thing.  Other CGI::* modules on CPAN might work better
for you, too.</p>
<p>Many people try to write their own decoder (or copy one from
another program) and then run into one of the many &quot;gotchas&quot;
of the task.  It's much easier and less hassle to use CGI.pm.</p>
<p>
</p>
<h2><a name="how_do_i_check_a_valid_mail_address">How do I check a valid mail address?</a></h2>
<p>You can't, at least, not in real time.  Bummer, eh?</p>
<p>Without sending mail to the address and seeing whether there's a human
on the other end to answer you, you cannot determine whether a mail
address is valid.  Even if you apply the mail 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.</p>
<p>You can use the Email::Valid or RFC::RFC822::Address which check
the format of the address, although they cannot actually tell you
if it is a deliverable address (i.e. that mail to the address
will not bounce).  Modules like Mail::CheckUser and Mail::EXPN
try to interact with the domain name system or particular
mail servers to learn even more, but their methods do not
work everywhere---especially for security conscious administrators.</p>
<p>Many are tempted to try to eliminate many frequently-invalid
mail addresses with a simple regex, such as
<code>/^[\w.-]+\@(?:[\w-]+\.)+\w+$/</code>.  It's a very bad idea.  However,
this also throws out many valid ones, and says nothing about
potential deliverability, so it is not suggested.  Instead, see
<a href="http://www.cpan.org/authors/Tom_Christiansen/scripts/ckaddr.gz">http://www.cpan.org/authors/Tom_Christiansen/scripts/ckaddr.gz</a> ,
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
hostname given can be looked up in the DNS MX records.  It's not fast,
but it works for what it tries to do.</p>
<p>Our best advice for verifying a person's mail address is to have them
enter their address twice, just as you normally do to change a password.
This usually weeds out typos.  If both versions match, send
mail to that address with a personal message that looks somewhat like:</p>
<pre>
    Dear someuser@host.com,</pre>
<pre>
    <span class="variable">Please</span> <span class="variable">confirm</span> <span class="variable">the</span> <span class="variable">mail</span> <span class="variable">address</span> <span class="variable">you</span> <span class="variable">gave</span> <span class="variable">us</span> <span class="variable">Wed</span> <span class="variable">May</span>  <span class="number">6</span> <span class="number">09</span><span class="operator">:</span><span class="number">38</span><span class="operator">:</span><span class="number">41</span>
    <span class="variable">MDT</span> <span class="number">1998</span> <span class="variable">by</span> <span class="variable">replying</span> <span class="variable">to</span> <span class="variable">this</span> <span class="variable">message</span><span class="operator">.</span>  <span class="variable">Include</span> <span class="variable">the</span> <span class="variable">string</span>
    <span class="string">"Rumpelstiltskin"</span> <span class="variable">in</span> <span class="variable">that</span> <span class="variable">reply</span><span class="operator">,</span> <span class="variable">but</span> <span class="variable">spelled</span> <span class="variable">in</span> <span class="keyword">reverse</span><span class="operator">;</span> <span class="variable">that</span> <span class="variable">is</span><span class="operator">,</span>
    <span class="variable">start</span> <span class="variable">with</span> <span class="string">"Nik..."</span><span class="operator">.</span>  <span class="variable">Once</span> <span class="variable">this</span> <span class="variable">is</span> <span class="variable">done</span><span class="operator">,</span> <span class="variable">your</span> <span class="variable">confirmed</span> <span class="variable">address</span> <span class="variable">will</span>
    <span class="variable">be</span> <span class="variable">entered</span> <span class="variable">into</span> <span class="keyword">our</span> <span class="variable">records</span><span class="operator">.</span>
</pre>
<p>If you get the message back and they've followed your directions,
you can be reasonably assured that it's real.</p>
<p>A related strategy that's less open to forgery is to give them a PIN
(personal ID number).  Record the address and PIN (best that it be a
random one) for later processing.  In the mail you send, ask them to
include the PIN in their reply.  But if it bounces, or the message is
included via a &quot;vacation&quot; script, it'll be there anyway.  So it's
best to ask them to mail back a slight alteration of the PIN, such as
with the characters reversed, one added or subtracted to each digit, etc.</p>
<p>
</p>
<h2><a name="how_do_i_decode_a_mime_base64_string">How do I decode a MIME/BASE64 string?</a></h2>
<p>The MIME-Base64 package (available from CPAN) handles this as well as
the MIME/QP encoding.  Decoding BASE64 becomes as simple as:</p>
<pre>
    <span class="keyword">use</span> <span class="variable">MIME::Base64</span><span class="operator">;</span>
    <span class="variable">$decoded</span> <span class="operator">=</span> <span class="variable">decode_base64</span><span class="operator">(</span><span class="variable">$encoded</span><span class="operator">);</span>
</pre>
<p>The MIME-Tools package (available from CPAN) supports extraction with
decoding of BASE64 encoded attachments and content directly from email
messages.</p>
<p>If the string to decode is short (less than 84 bytes long)
a more direct approach is to use the <a href="../../lib/Pod/perlfunc.html#item_unpack"><code>unpack()</code></a> function's &quot;u&quot;
format after minor transliterations:</p>
<pre>
    <span class="regex">tr#A-Za-z0-9+/##cd</span><span class="operator">;</span>                   <span class="comment"># remove non-base64 chars</span>
    <span class="regex">tr#A-Za-z0-9+/# -_#</span><span class="operator">;</span>                  <span class="comment"># convert to uuencoded format</span>
    <span class="variable">$len</span> <span class="operator">=</span> <span class="keyword">pack</span><span class="operator">(</span><span class="string">"c"</span><span class="operator">,</span> <span class="number">32</span> <span class="operator">+</span> <span class="number">0</span><span class="operator">.</span><span class="number">75</span><span class="variable">*length</span><span class="operator">);</span>   <span class="comment"># compute length byte</span>
    <span class="keyword">print</span> <span class="keyword">unpack</span><span class="operator">(</span><span class="string">"u"</span><span class="operator">,</span> <span class="variable">$len</span> <span class="operator">.</span> <span class="variable">$_</span><span class="operator">);</span>         <span class="comment"># uudecode and print</span>
</pre>
<p>
</p>
<h2><a name="how_do_i_return_the_user_s_mail_address">How do I return the user's mail address?</a></h2>
<p>On systems that support getpwuid, the $&lt; variable, and the
Sys::Hostname module (which is part of the standard perl distribution),
you can probably try using something like this:</p>
<pre>
    <span class="keyword">use</span> <span class="variable">Sys::Hostname</span><span class="operator">;</span>
    <span class="variable">$address</span> <span class="operator">=</span> <span class="keyword">sprintf</span><span class="operator">(</span><span class="string">'%s@%s'</span><span class="operator">,</span> <span class="keyword">scalar</span> <span class="keyword">getpwuid</span><span class="operator">(</span><span class="variable">$&lt;</span><span class="operator">),</span> <span class="variable">hostname</span><span class="operator">);</span>
</pre>
<p>Company policies on mail address can mean that this generates addresses
that the company's mail system will not accept, so you should ask for
users' mail addresses when this matters.  Furthermore, not all systems
on which Perl runs are so forthcoming with this information as is Unix.</p>
<p>The Mail::Util module from CPAN (part of the MailTools package) provides a
<code>mailaddress()</code> 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.</p>
<p>
</p>
<h2><a name="how_do_i_send_mail">How do I send mail?</a></h2>
<p>Use the <code>sendmail</code> program directly:</p>
<pre>
    <span class="keyword">open</span><span class="operator">(</span><span class="variable">SENDMAIL</span><span class="operator">,</span> <span class="string">"|/usr/lib/sendmail -oi -t -odq"</span><span class="operator">)</span>
                        <span class="keyword">or</span> <span class="keyword">die</span> <span class="string">"Can't fork for sendmail: $!\n"</span><span class="operator">;</span>
    <span class="keyword">print</span> <span class="variable">SENDMAIL</span> <span class="operator">&lt;&lt;</span><span class="default">"EOF"</span><span class="operator">;</span><span class="string">
    From: User Originating Mail &lt;me\@host&gt;
    To: Final Destination &lt;you\@otherhost&gt;
    Subject: A relevant subject line

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?