readline.html

来自「perl教程」· HTML 代码 · 共 239 行

HTML
239
字号
<?xml version="1.0" ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<!-- saved from url=(0017)http://localhost/ -->
<script language="JavaScript" src="../../displayToc.js"></script>
<script language="JavaScript" src="../../tocParas.js"></script>
<script language="JavaScript" src="../../tocTab.js"></script>
<link rel="stylesheet" type="text/css" href="../../scineplex.css">
<title>Term::ReadLine - Perl interface to various C&lt;readline&gt; packages. If no real package is found, substitutes stubs instead of basic functions.</title>
<link rel="stylesheet" href="../../Active.css" type="text/css" />
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<link rev="made" href="mailto:" />
</head>

<body>

<script>writelinks('__top__',2);</script>
<h1><a>Term::ReadLine - Perl interface to various C&lt;readline&gt; packages. If no real package is found, substitutes stubs instead of basic functions.</a></h1>
<p><a name="__index__"></a></p>

<!-- INDEX BEGIN -->

<ul>

	<li><a href="#name">NAME</a></li>
	<li><a href="#synopsis">SYNOPSIS</a></li>
	<li><a href="#description">DESCRIPTION</a></li>
	<li><a href="#minimal_set_of_supported_functions">Minimal set of supported functions</a></li>
	<li><a href="#additional_supported_functions">Additional supported functions</a></li>
	<li><a href="#exports">EXPORTS</a></li>
	<li><a href="#environment">ENVIRONMENT</a></li>
	<li><a href="#caveats">CAVEATS</a></li>
</ul>
<!-- INDEX END -->

<hr />
<p>
</p>
<h1><a name="name">NAME</a></h1>
<p>Term::ReadLine - Perl interface to various <a href="#item_readline"><code>readline</code></a> packages.
If no real package is found, substitutes stubs instead of basic functions.</p>
<p>
</p>
<hr />
<h1><a name="synopsis">SYNOPSIS</a></h1>
<pre>
  <span class="keyword">use</span> <span class="variable">Term::ReadLine</span><span class="operator">;</span>
  <span class="keyword">my</span> <span class="variable">$term</span> <span class="operator">=</span> <span class="variable">new</span> <span class="variable">Term::ReadLine</span> <span class="string">'Simple Perl calc'</span><span class="operator">;</span>
  <span class="keyword">my</span> <span class="variable">$prompt</span> <span class="operator">=</span> <span class="string">"Enter your arithmetic expression: "</span><span class="operator">;</span>
  <span class="keyword">my</span> <span class="variable">$OUT</span> <span class="operator">=</span> <span class="variable">$term</span><span class="operator">-&gt;</span><span class="variable">OUT</span> <span class="operator">||</span> <span class="operator">\</span><span class="variable">*STDOUT</span><span class="operator">;</span>
  <span class="keyword">while</span> <span class="operator">(</span> <span class="keyword">defined</span> <span class="operator">(</span><span class="variable">$_</span> <span class="operator">=</span> <span class="variable">$term</span><span class="operator">-&gt;</span><span class="keyword">readline</span><span class="operator">(</span><span class="variable">$prompt</span><span class="operator">))</span> <span class="operator">)</span> <span class="operator">{</span>
    <span class="keyword">my</span> <span class="variable">$res</span> <span class="operator">=</span> <span class="keyword">eval</span><span class="operator">(</span><span class="variable">$_</span><span class="operator">);</span>
    <span class="keyword">warn</span> <span class="variable">$@</span> <span class="keyword">if</span> <span class="variable">$@</span><span class="operator">;</span>
    <span class="keyword">print</span> <span class="variable">$OUT</span> <span class="variable">$res</span><span class="operator">,</span> <span class="string">"\n"</span> <span class="keyword">unless</span> <span class="variable">$@</span><span class="operator">;</span>
    <span class="variable">$term</span><span class="operator">-&gt;</span><span class="variable">addhistory</span><span class="operator">(</span><span class="variable">$_</span><span class="operator">)</span> <span class="keyword">if</span> <span class="regex">/\S/</span><span class="operator">;</span>
  <span class="operator">}</span>
</pre>
<p>
</p>
<hr />
<h1><a name="description">DESCRIPTION</a></h1>
<p>This package is just a front end to some other packages. It's a stub to
set up a common interface to the various ReadLine implementations found on
CPAN (under the <code>Term::ReadLine::*</code> namespace).</p>
<p>
</p>
<hr />
<h1><a name="minimal_set_of_supported_functions">Minimal set of supported functions</a></h1>
<p>All the supported functions should be called as methods, i.e., either as</p>
<pre>
  <span class="variable">$term</span> <span class="operator">=</span> <span class="variable">new</span> <span class="variable">Term::ReadLine</span> <span class="string">'name'</span><span class="operator">;</span>
</pre>
<p>or as</p>
<pre>
  <span class="variable">$term</span><span class="operator">-&gt;</span><span class="variable">addhistory</span><span class="operator">(</span><span class="string">'row'</span><span class="operator">);</span>
</pre>
<p>where $term is a return value of Term::ReadLine-&gt;new().</p>
<dl>
<dt><strong><a name="item_readline"><code>ReadLine</code></a></strong>

<dd>
<p>returns the actual package that executes the commands. Among possible
values are <code>Term::ReadLine::Gnu</code>, <code>Term::ReadLine::Perl</code>,
<code>Term::ReadLine::Stub</code>.</p>
</dd>
</li>
<dt><strong><a name="item_new"><code>new</code></a></strong>

<dd>
<p>returns the handle for subsequent calls to following
functions. Argument is the name of the application. Optionally can be
followed by two arguments for <code>IN</code> and <code>OUT</code> filehandles. These
arguments should be globs.</p>
</dd>
</li>
<dt><strong><a name="item_readline"><code>readline</code></a></strong>

<dd>
<p>gets an input line, <em>possibly</em> with actual <a href="#item_readline"><code>readline</code></a>
support. Trailing newline is removed. Returns <a href="../../lib/Pod/perlfunc.html#item_undef"><code>undef</code></a> on <code>EOF</code>.</p>
</dd>
</li>
<dt><strong><a name="item_addhistory"><code>addhistory</code></a></strong>

<dd>
<p>adds the line to the history of input, from where it can be used if
the actual <a href="#item_readline"><code>readline</code></a> is present.</p>
</dd>
</li>
<dt><strong><a name="item_in_2c_out"><code>IN</code>, <code>OUT</code></a></strong>

<dd>
<p>return the filehandles for input and output or <a href="../../lib/Pod/perlfunc.html#item_undef"><code>undef</code></a> if <a href="#item_readline"><code>readline</code></a>
input and output cannot be used for Perl.</p>
</dd>
</li>
<dt><strong><a name="item_minline"><code>MinLine</code></a></strong>

<dd>
<p>If argument is specified, it is an advice on minimal size of line to
be included into history.  <a href="../../lib/Pod/perlfunc.html#item_undef"><code>undef</code></a> means do not include anything into
history. Returns the old value.</p>
</dd>
</li>
<dt><strong><a name="item_findconsole"><code>findConsole</code></a></strong>

<dd>
<p>returns an array with two strings that give most appropriate names for
files for input and output using conventions <code>&quot;&lt;$in&quot;</code>, <code>&quot;&gt;out&quot;</code>.</p>
</dd>
</li>
<dt><strong><a name="item_attribs">Attribs</a></strong>

<dd>
<p>returns a reference to a hash which describes internal configuration
of the package. Names of keys in this hash conform to standard
conventions with the leading <code>rl_</code> stripped.</p>
</dd>
</li>
<dt><strong><a name="item_features"><code>Features</code></a></strong>

<dd>
<p>Returns a reference to a hash with keys being features present in
current implementation. Several optional features are used in the
minimal interface: <code>appname</code> should be present if the first argument
to <a href="#item_new"><code>new</code></a> is recognized, and <code>minline</code> should be present if
<a href="#item_minline"><code>MinLine</code></a> method is not dummy.  <code>autohistory</code> should be present if
lines are put into history automatically (maybe subject to
<a href="#item_minline"><code>MinLine</code></a>), and <a href="#item_addhistory"><code>addhistory</code></a> if <a href="#item_addhistory"><code>addhistory</code></a> method is not dummy.</p>
</dd>
<dd>
<p>If <a href="#item_features"><code>Features</code></a> method reports a feature <code>attribs</code> as present, the
method <a href="#item_attribs"><code>Attribs</code></a> is not dummy.</p>
</dd>
</li>
</dl>
<p>
</p>
<hr />
<h1><a name="additional_supported_functions">Additional supported functions</a></h1>
<p>Actually <code>Term::ReadLine</code> can use some other package, that will
support a richer set of commands.</p>
<p>All these commands are callable via method interface and have names
which conform to standard conventions with the leading <code>rl_</code> stripped.</p>
<p>The stub package included with the perl distribution allows some
additional methods:</p>
<dl>
<dt><strong><a name="item_tkrunning"><code>tkRunning</code></a></strong>

<dd>
<p>makes Tk event loop run when waiting for user input (i.e., during
<a href="#item_readline"><code>readline</code></a> method).</p>
</dd>
</li>
<dt><strong><a name="item_ornaments"><code>ornaments</code></a></strong>

<dd>
<p>makes the command line stand out by using termcap data.  The argument
to <a href="#item_ornaments"><code>ornaments</code></a> should be 0, 1, or a string of a form
<code>&quot;aa,bb,cc,dd&quot;</code>.  Four components of this string should be names of
<em>terminal capacities</em>, first two will be issued to make the prompt
standout, last two to make the input line standout.</p>
</dd>
</li>
<dt><strong><a name="item_newtty"><code>newTTY</code></a></strong>

<dd>
<p>takes two arguments which are input filehandle and output filehandle.
Switches to use these filehandles.</p>
</dd>
</li>
</dl>
<p>One can check whether the currently loaded ReadLine package supports
these methods by checking for corresponding <a href="#item_features"><code>Features</code></a>.</p>
<p>
</p>
<hr />
<h1><a name="exports">EXPORTS</a></h1>
<p>None</p>
<p>
</p>
<hr />
<h1><a name="environment">ENVIRONMENT</a></h1>
<p>The environment variable <code>PERL_RL</code> governs which ReadLine clone is
loaded. If the value is false, a dummy interface is used. If the value
is true, it should be tail of the name of the package to use, such as
<code>Perl</code> or <code>Gnu</code>.</p>
<p>As a special case, if the value of this variable is space-separated,
the tail might be used to disable the ornaments by setting the tail to
be <code>o=0</code> or <code>ornaments=0</code>.  The head should be as described above, say</p>
<p>If the variable is not set, or if the head of space-separated list is
empty, the best available package is loaded.</p>
<pre>
  export &quot;PERL_RL=Perl o=0&quot;     # Use Perl ReadLine without ornaments
  export &quot;PERL_RL= o=0&quot;         # Use best available ReadLine without ornaments</pre>
<p>(Note that processing of <code>PERL_RL</code> for ornaments is in the discretion of the 
particular used <code>Term::ReadLine::*</code> package).</p>
<p>
</p>
<hr />
<h1><a name="caveats">CAVEATS</a></h1>
<p>It seems that using Term::ReadLine from Emacs minibuffer doesn't work
quite right and one will get an error message like</p>
<pre>
    Cannot open /dev/tty for read at ...</pre>
<p>One possible workaround for this is to explicitly open /dev/tty like this</p>
<pre>
    <span class="keyword">open</span> <span class="operator">(</span><span class="variable">FH</span><span class="operator">,</span> <span class="string">"/dev/tty"</span> <span class="operator">)</span>
      <span class="keyword">or</span> <span class="keyword">eval</span> <span class="string">'sub Term::ReadLine::findConsole { ("&amp;STDIN", "&amp;STDERR") }'</span><span class="operator">;</span>
    <span class="keyword">die</span> <span class="variable">$@</span> <span class="keyword">if</span> <span class="variable">$@</span><span class="operator">;</span>
    <span class="keyword">close</span> <span class="operator">(</span><span class="variable">FH</span><span class="operator">);</span>
</pre>
<p>or you can try using the 4-argument form of Term::ReadLine-&gt;new().</p>

</body>

</html>

⌨️ 快捷键说明

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