tcl.html
来自「perl教程」· HTML 代码 · 共 508 行 · 第 1/2 页
HTML
508 行
<?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>Tcl - Tcl extension module for Perl</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__',1);</script>
<h1><a>Tcl - Tcl extension module for Perl</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>
<ul>
<li><a href="#methods_in_class_tcl">Methods in class Tcl</a></li>
<li><a href="#linking_perl_and_tcl_variables">Linking Perl and Tcl variables</a></li>
<li><a href="#accessing_perl_from_within_tcl">Accessing Perl from within Tcl</a></li>
<li><a href="#moving_tcl_tk_around_with_tcl_pm">Moving Tcl/Tk around with Tcl.pm</a></li>
<ul>
<li><a href="#first_method">First method</a></li>
<li><a href="#second_method">Second method</a></li>
<li><a href="#third_method">Third method</a></li>
</ul>
</ul>
<li><a href="#authors">AUTHORS</a></li>
<li><a href="#copyright">COPYRIGHT</a></li>
</ul>
<!-- INDEX END -->
<hr />
<p>
</p>
<h1><a name="name">NAME</a></h1>
<p>Tcl - Tcl extension module for Perl</p>
<p>
</p>
<hr />
<h1><a name="synopsis">SYNOPSIS</a></h1>
<pre>
<span class="keyword">use</span> <span class="variable">Tcl</span><span class="operator">;</span>
</pre>
<pre>
<span class="variable">$interp</span> <span class="operator">=</span> <span class="variable">new</span> <span class="variable">Tcl</span><span class="operator">;</span>
<span class="variable">$interp</span><span class="operator">-></span><span class="variable">Eval</span><span class="operator">(</span><span class="string">'puts "Hello world"'</span><span class="operator">);</span>
</pre>
<p>
</p>
<hr />
<h1><a name="description">DESCRIPTION</a></h1>
<p>The Tcl extension module gives access to the Tcl library with
functionality and interface similar to the C functions of Tcl.
In other words, you can</p>
<dl>
<dt><strong><a name="item_create_tcl_interpreters">create Tcl interpreters</a></strong>
<dd>
<p>The Tcl interpreters so created are Perl objects whose destructors
delete the interpreters cleanly when appropriate.</p>
</dd>
</li>
<dt><strong><a name="item_execute_tcl_code_in_an_interpreter">execute Tcl code in an interpreter</a></strong>
<dd>
<p>The code can come from strings, files or Perl filehandles.</p>
</dd>
</li>
<dt><strong><a name="item_bind_in_new_tcl_procedures">bind in new Tcl procedures</a></strong>
<dd>
<p>The new procedures can be either C code (with addresses presumably
obtained using <em>dl_open</em> and <em>dl_find_symbol</em>) or Perl subroutines
(by name, reference or as anonymous subs). The (optional) deleteProc
callback in the latter case is another perl subroutine which is called
when the command is explicitly deleted by name or else when the
destructor for the interpreter object is explicitly or implicitly called.</p>
</dd>
</li>
<dt><strong><a name="item_manipulate_the_result_field_of_a_tcl_interpreter">Manipulate the result field of a Tcl interpreter</a></strong>
<dt><strong><a name="item_set_and_get_values_of_variables_in_a_tcl_interpret">Set and get values of variables in a Tcl interpreter</a></strong>
<dt><strong><a name="item_tie_perl_variables_to_variables_in_a_tcl_interpret">Tie perl variables to variables in a Tcl interpreter</a></strong>
<dd>
<p>The variables can be either scalars or hashes.</p>
</dd>
</li>
</dl>
<p>
</p>
<h2><a name="methods_in_class_tcl">Methods in class Tcl</a></h2>
<p>To create a new Tcl interpreter, use</p>
<pre>
<span class="variable">$i</span> <span class="operator">=</span> <span class="variable">new</span> <span class="variable">Tcl</span><span class="operator">;</span>
</pre>
<p>The following methods and routines can then be used on the Perl object
returned (the object argument omitted in each case).</p>
<dl>
<dt><strong><a name="item_init">Init ()</a></strong>
<dd>
<p>Invoke <em>Tcl_Init</em> on the interpeter.</p>
</dd>
</li>
<dt><strong><a name="item_eval">Eval (STRING, FLAGS)</a></strong>
<dd>
<p>Evaluate script STRING in the interpreter. If the script returns
successfully (TCL_OK) then the Perl return value corresponds to Tcl
interpreter's result otherwise a <em>die</em> exception is raised with the $@
variable corresponding to Tcl's interpreter result object. In each case,
<em>corresponds</em> means that if the method is called in scalar context then
the string result is returned but if the method is called in list context
then the result is split as a Tcl list and returned as a Perl list.
The FLAGS field is optional and can be a bitwise OR of the constants
Tcl::EVAL_GLOBAL or Tcl::EVAL_DIRECT.</p>
</dd>
</li>
<dt><strong><a name="item_globaleval">GlobalEval (STRING)</a></strong>
<dd>
<p>REMOVED. Evalulate script STRING at global level.
Call <em>Eval</em>(STRING, Tcl::EVAL_GLOBAL) instead.</p>
</dd>
</li>
<dt><strong><a name="item_evalfile">EvalFile (FILENAME)</a></strong>
<dd>
<p>Evaluate the contents of the file with name FILENAME. Otherwise, the
same as <em>Eval</em>() above.</p>
</dd>
</li>
<dt><strong><a name="item_evalfilehandle">EvalFileHandle (FILEHANDLE)</a></strong>
<dd>
<p>Evaluate the contents of the Perl filehandle FILEHANDLE. Otherwise, the
same as <em>Eval</em>() above. Useful when using the filehandle DATA to tack
on a Tcl script following an __END__ token.</p>
</dd>
</li>
<dt><strong><a name="item_call">call (PROC, ARG, ...)</a></strong>
<dd>
<p>Looks up procedure PROC in the interpreter and invokes it using Tcl's eval
semantics that does command tracing and will use the ::unknown (AUTOLOAD)
mechanism. The arguments (ARG, ...) are not passed through the Tcl parser.
For example, spaces embedded in any ARG will not cause it to be split into
two Tcl arguments before being passed to PROC.</p>
</dd>
<dd>
<p>Before invoking procedure PROC special processing is performed on ARG list:</p>
</dd>
<dd>
<p>1. All subroutine references within ARG will be substituted with Tcl name
which is responsible to invoke this subroutine. This Tcl name will be
created using CreateCommand subroutine (see below).</p>
</dd>
<dd>
<p>2. All references to scalars will be substituted with names of Tcl variables
transformed appropriately.</p>
</dd>
<dd>
<p>These first two items allows to write and expect it to work properly such
code as:</p>
</dd>
<dd>
<pre>
<span class="keyword">my</span> <span class="variable">$r</span> <span class="operator">=</span> <span class="string">'aaaa'</span><span class="operator">;</span>
<span class="variable">button</span><span class="operator">(</span><span class="string">".d"</span><span class="operator">,</span> <span class="string">-textvariable</span> <span class="operator">=></span> <span class="operator">\</span><span class="variable">$r</span><span class="operator">,</span> <span class="string">-command</span><span class="operator">=></span><span class="keyword">sub</span><span class="variable"> </span><span class="operator">{</span><span class="variable">$r</span><span class="operator">++});</span>
</pre>
</dd>
<dd>
<p>3. As a special case, there is a mechanism to deal with Tk's special event
variables (they are mentioned as '%x', '%y' and so on throughout Tcl).
When creating a subroutine reference that uses such variables, you must
declare the desired variables using Tcl::Ev as the first argument to the
subroutine. Example:</p>
</dd>
<dd>
<pre>
<span class="keyword">sub</span><span class="variable"> textPaste </span><span class="operator">{</span>
<span class="keyword">my</span> <span class="operator">(</span><span class="variable">$x</span><span class="operator">,</span><span class="variable">$y</span><span class="operator">,</span><span class="variable">$w</span><span class="operator">)</span> <span class="operator">=</span> <span class="variable">@_</span><span class="operator">;</span>
<span class="variable">widget</span><span class="operator">(</span><span class="variable">$w</span><span class="operator">)-></span><span class="variable">insert</span><span class="operator">(</span><span class="string">"\@$x,$y"</span><span class="operator">,</span> <span class="variable">$interp</span><span class="operator">-></span><span class="variable">Eval</span><span class="operator">(</span><span class="string">'selection get'</span><span class="operator">));</span>
<span class="operator">}</span>
<span class="variable">$widget</span><span class="operator">-></span><span class="keyword">bind</span><span class="operator">(</span><span class="string">'<2>'</span><span class="operator">,</span> <span class="operator">[</span><span class="operator">\&</span><span class="variable">textPaste</span><span class="operator">,</span> <span class="variable">Tcl::Ev</span><span class="operator">(</span><span class="string">'%x'</span><span class="operator">,</span> <span class="string">'%y'</span><span class="operator">),</span> <span class="variable">$widget</span><span class="operator">]</span> <span class="operator">);</span>
</pre>
</dd>
</li>
<dt><strong><a name="item_return_ref">return_ref (NAME)</a></strong>
<dd>
<p>returns a reference corresponding to NAME, which was associated during
previously called <a href="#item_call"><code>$int->call(...)</code></a> preprocessing. As a typical
example this could be variable associated with a widget.</p>
</dd>
</li>
<dt><strong><a name="item_delete_ref">delete_ref (NAME)</a></strong>
<dd>
<p>deletes and returns a reference corresponding to NAME, which was associated
during previously called <a href="#item_call"><code>$int->call(...)</code></a> preprocessing.</p>
</dd>
</li>
<dt><strong><a name="item_icall">icall (PROC, ARG, ...)</a></strong>
<dd>
<p>Looks up procedure PROC in the interpreter and invokes it using Tcl's eval
semantics that does command tracing and will use the ::unknown (AUTOLOAD)
mechanism. The arguments (ARG, ...) are not passed through the Tcl parser.
For example, spaces embedded in any ARG will not cause it to be split into
two Tcl arguments before being passed to PROC.</p>
</dd>
<dd>
<p>This is the lower-level procedure that the 'call' method uses. Arguments
are converted efficiently from Perl SVs to Tcl_Objs. A Perl AV array
becomes a Tcl_ListObj, an SvIV becomes a Tcl_IntObj, etc. The reverse
conversion is done to the result.</p>
</dd>
</li>
<dt><strong><a name="item_invoke">invoke (PROC, ARG, ...)</a></strong>
<dd>
<p>Looks up procedure PROC in the interpreter and invokes it directly with
arguments (ARG, ...) without passing through the Tcl parser. For example,
spaces embedded in any ARG will not cause it to be split into two Tcl
arguments before being passed to PROC. This differs from icall/call in
that it directly invokes the command name without allowing for command
tracing or making use of Tcl's unknown (AUTOLOAD) mechanism. If the
command does not already exist in the interpreter, and error will be
thrown.</p>
</dd>
<dd>
<p>Arguments are converted efficiently from Perl SVs to Tcl_Objs. A Perl AV
array becomes a Tcl_ListObj, an SvIV becomes a Tcl_IntObj, etc. The
reverse conversion is done to the result.</p>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?