perlfaq8.html

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

HTML
800 行
字号
<h2><a name="how_do_i_ask_the_user_for_a_password">How do I ask the user for a password?</a></h2>
<p>(This question has nothing to do with the web.  See a different
FAQ for that.)</p>
<p>There's an example of this in <a href="../../lib/Pod/perlfunc.html#crypt">crypt in the perlfunc manpage</a>).  First, you put the
terminal into &quot;no echo&quot; mode, then just read the password normally.
You may do this with an old-style <a href="../../lib/Pod/perlfunc.html#item_ioctl"><code>ioctl()</code></a> function, POSIX terminal
control (see <a href="../../lib/POSIX.html">the POSIX manpage</a> or its documentation the Camel Book), or a call
to the <strong>stty</strong> program, with varying degrees of portability.</p>
<p>You can also do this for most systems using the Term::ReadKey module
from CPAN, which is easier to use and in theory more portable.</p>
<pre>
    <span class="keyword">use</span> <span class="variable">Term::ReadKey</span><span class="operator">;</span>
</pre>
<pre>
    <span class="variable">ReadMode</span><span class="operator">(</span><span class="string">'noecho'</span><span class="operator">);</span>
    <span class="variable">$password</span> <span class="operator">=</span> <span class="variable">ReadLine</span><span class="operator">(</span><span class="number">0</span><span class="operator">);</span>
</pre>
<p>
</p>
<h2><a name="how_do_i_read_and_write_the_serial_port">How do I read and write the serial port?</a></h2>
<p>This depends on which operating system your program is running on.  In
the case of Unix, the serial ports will be accessible through files in
/dev; on other systems, device names will doubtless differ.
Several problem areas common to all device interaction are the
following:</p>
<dl>
<dt><strong><a name="item_lockfiles">lockfiles</a></strong>

<dd>
<p>Your system may use lockfiles to control multiple access.  Make sure
you follow the correct protocol.  Unpredictable behavior can result
from multiple processes reading from one device.</p>
</dd>
</li>
<dt><strong><a name="item_open_mode">open mode</a></strong>

<dd>
<p>If you expect to use both read and write operations on the device,
you'll have to open it for update (see <a href="../../lib/Pod/perlfunc.html#open">open in the perlfunc manpage</a> for
details).  You may wish to open it without running the risk of
blocking by using <a href="../../lib/Pod/perlfunc.html#item_sysopen"><code>sysopen()</code></a> and <code>O_RDWR|O_NDELAY|O_NOCTTY</code> from the
Fcntl module (part of the standard perl distribution).  See
<a href="../../lib/Pod/perlfunc.html#sysopen">sysopen in the perlfunc manpage</a> for more on this approach.</p>
</dd>
</li>
<dt><strong><a name="item_end_of_line">end of line</a></strong>

<dd>
<p>Some devices will be expecting a &quot;\r&quot; at the end of each line rather
than a &quot;\n&quot;.  In some ports of perl, &quot;\r&quot; and &quot;\n&quot; are different from
their usual (Unix) ASCII values of &quot;\012&quot; and &quot;\015&quot;.  You may have to
give the numeric values you want directly, using octal (&quot;\015&quot;), hex
(&quot;0x0D&quot;), or as a control-character specification (&quot;\cM&quot;).</p>
</dd>
<dd>
<pre>
    <span class="keyword">print</span> <span class="variable">DEV</span> <span class="string">"atv1\012"</span><span class="operator">;</span>       <span class="comment"># wrong, for some devices</span>
    <span class="keyword">print</span> <span class="variable">DEV</span> <span class="string">"atv1\015"</span><span class="operator">;</span>       <span class="comment"># right, for some devices</span>
</pre>
</dd>
<dd>
<p>Even though with normal text files a &quot;\n&quot; will do the trick, there is
still no unified scheme for terminating a line that is portable
between Unix, DOS/Win, and Macintosh, except to terminate <em>ALL</em> line
ends with &quot;\015\012&quot;, and strip what you don't need from the output.
This applies especially to socket I/O and autoflushing, discussed
next.</p>
</dd>
</li>
<dt><strong><a name="item_flushing_output">flushing output</a></strong>

<dd>
<p>If you expect characters to get to your device when you <a href="../../lib/Pod/perlfunc.html#item_print"><code>print()</code></a> them,
you'll want to autoflush that filehandle.  You can use <a href="../../lib/Pod/perlfunc.html#item_select"><code>select()</code></a>
and the <a href="../../lib/Pod/perlvar.html#item___"><code>$|</code></a> variable to control autoflushing (see <a href="../../lib/Pod/perlvar.html#_e_verbar_">$&verbar; in the perlvar manpage</a>
and <a href="../../lib/Pod/perlfunc.html#select">select in the perlfunc manpage</a>, or <a href="../../lib/Pod/perlfaq5.html">the perlfaq5 manpage</a>, &quot;How do I flush/unbuffer an
output filehandle?  Why must I do this?&quot;):</p>
</dd>
<dd>
<pre>
    <span class="variable">$oldh</span> <span class="operator">=</span> <span class="keyword">select</span><span class="operator">(</span><span class="variable">DEV</span><span class="operator">);</span>
    <span class="variable">$|</span> <span class="operator">=</span> <span class="number">1</span><span class="operator">;</span>
    <span class="keyword">select</span><span class="operator">(</span><span class="variable">$oldh</span><span class="operator">);</span>
</pre>
</dd>
<dd>
<p>You'll also see code that does this without a temporary variable, as in</p>
</dd>
<dd>
<pre>
    <span class="keyword">select</span><span class="operator">((</span><span class="keyword">select</span><span class="operator">(</span><span class="variable">DEV</span><span class="operator">),</span> <span class="variable">$|</span> <span class="operator">=</span> <span class="number">1</span><span class="operator">)</span><span class="operator">[</span><span class="number">0</span><span class="operator">]</span><span class="operator">);</span>
</pre>
</dd>
<dd>
<p>Or if you don't mind pulling in a few thousand lines
of code just because you're afraid of a little $| variable:</p>
</dd>
<dd>
<pre>
    <span class="keyword">use</span> <span class="variable">IO::Handle</span><span class="operator">;</span>
    <span class="variable">DEV</span><span class="operator">-&gt;</span><span class="variable">autoflush</span><span class="operator">(</span><span class="number">1</span><span class="operator">);</span>
</pre>
</dd>
<dd>
<p>As mentioned in the previous item, this still doesn't work when using
socket I/O between Unix and Macintosh.  You'll need to hard code your
line terminators, in that case.</p>
</dd>
</li>
<dt><strong><a name="item_non_2dblocking_input">non-blocking input</a></strong>

<dd>
<p>If you are doing a blocking <a href="../../lib/Pod/perlfunc.html#item_read"><code>read()</code></a> or sysread(), you'll have to
arrange for an alarm handler to provide a timeout (see
<a href="../../lib/Pod/perlfunc.html#alarm">alarm in the perlfunc manpage</a>).  If you have a non-blocking open, you'll likely
have a non-blocking read, which means you may have to use a 4-arg
<a href="../../lib/Pod/perlfunc.html#item_select"><code>select()</code></a> to determine whether I/O is ready on that device (see
<a href="../../lib/Pod/perlfunc.html#select">select in the perlfunc manpage</a>.</p>
</dd>
</li>
</dl>
<p>While trying to read from his caller-id box, the notorious Jamie Zawinski
&lt;<a href="mailto:jwz@netscape.com">jwz@netscape.com</a>&gt;, after much gnashing of teeth and fighting with sysread,
sysopen, POSIX's tcgetattr business, and various other functions that
go bump in the night, finally came up with this:</p>
<pre>
    <span class="keyword">sub</span><span class="variable"> open_modem </span><span class="operator">{</span>
        <span class="keyword">use</span> <span class="variable">IPC::Open2</span><span class="operator">;</span>
        <span class="keyword">my</span> <span class="variable">$stty</span> <span class="operator">=</span> <span class="string">`/bin/stty -g`</span><span class="operator">;</span>
        <span class="variable">open2</span><span class="operator">(</span> <span class="operator">\</span><span class="variable">*MODEM_IN</span><span class="operator">,</span> <span class="operator">\</span><span class="variable">*MODEM_OUT</span><span class="operator">,</span> <span class="string">"cu -l$modem_device -s2400 2&gt;&amp;1"</span><span class="operator">);</span>
        <span class="comment"># starting cu hoses /dev/tty's stty settings, even when it has</span>
        <span class="comment"># been opened on a pipe...</span>
        <span class="keyword">system</span><span class="operator">(</span><span class="string">"/bin/stty $stty"</span><span class="operator">);</span>
        <span class="variable">$_</span> <span class="operator">=</span> <span class="operator">&lt;</span><span class="variable">MODEM_IN</span><span class="operator">&gt;;</span>
        <span class="keyword">chomp</span><span class="operator">;</span>
        <span class="keyword">if</span> <span class="operator">(</span> <span class="operator">!</span><span class="regex">m/^Connected/</span> <span class="operator">)</span> <span class="operator">{</span>
            <span class="keyword">print</span> <span class="variable">STDERR</span> <span class="string">"$0: cu printed `$_' instead of `Connected'\n"</span><span class="operator">;</span>
        <span class="operator">}</span>
    <span class="operator">}</span>
</pre>
<p>
</p>
<h2><a name="how_do_i_decode_encrypted_password_files">How do I decode encrypted password files?</a></h2>
<p>You spend lots and lots of money on dedicated hardware, but this is
bound to get you talked about.</p>
<p>Seriously, you can't if they are Unix password files--the Unix
password system employs one-way encryption.  It's more like hashing than
encryption.  The best you can check is whether something else hashes to
the same string.  You can't turn a hash back into the original string.
Programs like Crack
can forcibly (and intelligently) try to guess passwords, but don't
(can't) guarantee quick success.</p>
<p>If you're worried about users selecting bad passwords, you should
proactively check when they try to change their password (by modifying
passwd(1), for example).</p>
<p>
</p>
<h2><a name="how_do_i_start_a_process_in_the_background">How do I start a process in the background?</a></h2>
<p>Several modules can start other processes that do not block
your Perl program.  You can use IPC::Open3, Parallel::Jobs,

⌨️ 快捷键说明

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