⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 x-tclsumm2.html

📁 vxworks相关论文
💻 HTML
📖 第 1 页 / 共 3 页
字号:
<pre class="CodeLeft"><b><a name="84228"></b><tt class="output">11</tt><b></a></b></pre></td><td width="10">&nbsp;</td></tr><tr valign="top"><td colspan=1 rowspan=1><pre class="CodeLeft"><b><a name="84230">expr 0xff00 &amp; 0xf00</a></b></pre></td><td width="10">&nbsp;</td><td colspan=1 rowspan=1><pre class="CodeLeft"><b><a name="84232"></b><tt class="output">3840</tt><b></a></b></pre></td><td width="10">&nbsp;</td></tr><tr><td colspan="20"><hr class="tablerule"></td></tr><tr valign="middle"><td colspan="20"></td></tr></table></p></p></dl></dl><font face="Helvetica, sans-serif" class="sans"><h4 class="H3"><i><a name="84237">B.2.6  &nbsp;&nbsp;I/O, Files, and Formatting</a></i></h4></font><dl class="margin"><dl class="margin"><dd><p class="Body"><a name="84239"> </a>Tcl includes many commands for working with files and for formatted I/O. Tcl also has many facilities for interrogating file directories and attributes. The following examples illustrate some of the possibilities:<p class="table"><h4 class="EntityTitle"><a name="84242"><font face="Helvetica, sans-serif" size="-1" class="sans">Table B-7:&nbsp;&nbsp;Files and Formatting in Tcl</font></a></h4><table border="0" cellpadding="0" cellspacing="0"><tr><td colspan="20"><hr class="tablerule"></td></tr><tr valign="middle"><th rowspan="1" colspan="1"><div class="CellHeading"><b><a name="84246"> </a><font face="Helvetica, sans-serif" size="-1" class="sans">Tcl Expression</font></b></div></th><td width="10">&nbsp;</td><th rowspan="1" colspan="1"><div class="CellHeading"><b><a name="84248"> </a><font face="Helvetica, sans-serif" size="-1" class="sans">Description</font></b></div></th><td width="10">&nbsp;</td></tr><tr><td colspan="20"><hr class="tablerule2"></td></tr><tr valign="top"><td colspan=1 rowspan=1><pre class="CodeLeft"><b><a name="84254">set myfile [open myfile.out w]</a></b></pre></td><td width="10">&nbsp;</td><td colspan=1 rowspan=1><div class="CellBody"><a name="84256"> </a>Open a file for writing.</div></td><td width="10">&nbsp;</td></tr><tr valign="top"><td colspan=1 rowspan=1><pre class="CodeLeft"><b><a name="84258">puts $myfile [format "%s %d\n" \ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"you are number" [expr 3+3]]</a></b></pre></td><td width="10">&nbsp;</td><td colspan=1 rowspan=1><div class="CellBody"><a name="84260"> </a>Format a string and write it to file.</div></td><td width="10">&nbsp;</td></tr><tr valign="top"><td colspan=1 rowspan=1><pre class="CodeLeft"><b><a name="84262">close $myfile</a></b></pre></td><td width="10">&nbsp;</td><td colspan=1 rowspan=1><div class="CellBody"><a name="84264"> </a>Close the file.</div></td><td width="10">&nbsp;</td></tr><tr valign="top"><td colspan=1 rowspan=1><pre class="CodeLeft"><b><a name="84266">file exists myfile.out</a></b></pre></td><td width="10">&nbsp;</td><td colspan=1 rowspan=1><pre class="CodeLeft"><b><a name="84268"></b><tt class="output">1</tt><b></a></b></pre></td><td width="10">&nbsp;</td></tr><tr valign="top"><td colspan=1 rowspan=1><pre class="CodeLeft"><b><a name="84270">file writable myfile.out</a></b></pre></td><td width="10">&nbsp;</td><td colspan=1 rowspan=1><pre class="CodeLeft"><b><a name="84272"></b><tt class="output">1</tt><b></a></b></pre></td><td width="10">&nbsp;</td></tr><tr valign="top"><td colspan=1 rowspan=1><pre class="CodeLeft"><b><a name="84274">file executable myfile.out</a></b></pre></td><td width="10">&nbsp;</td><td colspan=1 rowspan=1><pre class="CodeLeft"><b><a name="84276"></b><tt class="output">0</tt><b></a></b></pre></td><td width="10">&nbsp;</td></tr><tr valign="top"><td colspan=1 rowspan=1><pre class="CodeLeft"><b><a name="84278">glob *.o</a></b></pre></td><td width="10">&nbsp;</td><td colspan=1 rowspan=1><pre class="CodeLeft"><b><a name="84281"></b><tt class="output">testCall.o foo.o bar.o</tt><b></a></b></pre></td><td width="10">&nbsp;</td></tr><tr><td colspan="20"><hr class="tablerule"></td></tr><tr valign="middle"><td colspan="20"></td></tr></table></p></p></dl></dl><font face="Helvetica, sans-serif" class="sans"><h4 class="H3"><i><a name="84286">B.2.7  &nbsp;&nbsp;Procedures</a></i></h4></font><dl class="margin"><dl class="margin"><dd><p class="Body"><a name="84288"> </a>Procedure definition in Tcl is straightforward, and resembles many other languages. The command <b class="tclProc">proc</b> builds a procedure from its arguments, which give the procedure name, a list of its arguments, and a sequence of statements for the procedure body. In the body, the <b class="tclProc">return</b> command specifies the result of the procedure. For example, the following defines a procedure to compute the square of a number:</p><dl class="margin"><dd><pre class="Code2"><b><a name="84289">proc square {i} {     return [expr $i * $i] }</a></b></pre></dl><dd><p class="Body"><a name="84290"> </a>If a procedure's argument list ends with the word <b class="keyword">args</b>, the result is a procedure that can be called with any number of arguments. All trailing arguments are captured in a list <b class="keyword">$args</b>. For example, the following procedure calculates the sum of all its arguments:</p><dl class="margin"><dd><pre class="Code2"><b><a name="84291">proc sum {args} {     set accum 0     foreach item $args {         incr accum $item     }     return $accum }</a></b></pre></dl><dd><p class="Body"><a name="84292"> </a>Defined Tcl procedures are called by name, and can be used just like any other Tcl command. The following examples illustrate some possibilities:<p class="table"><h4 class="EntityTitle"><a name="84295"><font face="Helvetica, sans-serif" size="-1" class="sans">Table B-8:&nbsp;&nbsp;Calling a Tcl Procedure</font></a></h4><table border="0" cellpadding="0" cellspacing="0"><tr><td colspan="20"><hr class="tablerule"></td></tr><tr valign="middle"><th rowspan="1" colspan="1"><div class="CellHeading"><b><a name="84299"> </a><font face="Helvetica, sans-serif" size="-1" class="sans">Tcl Expression</font></b></div></th><td width="10">&nbsp;</td><th rowspan="1" colspan="1"><div class="CellHeading"><b><a name="84301"> </a><font face="Helvetica, sans-serif" size="-1" class="sans">Result</font></b></div></th><td width="10">&nbsp;</td></tr><tr><td colspan="20"><hr class="tablerule2"></td></tr><tr valign="top"><td colspan=1 rowspan=1><pre class="CodeLeft"><b><a name="84307">square 4</a></b></pre></td><td width="10">&nbsp;</td><td colspan=1 rowspan=1><pre class="CodeLeft"><b><a name="84309"></b><tt class="output">16</tt><b></a></b></pre></td><td width="10">&nbsp;</td></tr><tr valign="top"><td colspan=1 rowspan=1><pre class="CodeLeft"><b><a name="84311">square [sum 1 2 3]</a></b></pre></td><td width="10">&nbsp;</td><td colspan=1 rowspan=1><pre class="CodeLeft"><b><a name="84313"></b><tt class="output">36</tt><b></a></b></pre></td><td width="10">&nbsp;</td></tr><tr valign="top"><td colspan=1 rowspan=1><pre class="CodeLeft"><b><a name="84315">set x "squ"</a></b></pre></td><td width="10">&nbsp;</td><td colspan=1 rowspan=1><pre class="CodeLeft"><b><a name="84317"></b><tt class="output">squ</tt><b></a></b></pre></td><td width="10">&nbsp;</td></tr><tr valign="top"><td colspan=1 rowspan=1><pre class="CodeLeft"><b><a name="84319">set y "are"</a></b></pre></td><td width="10">&nbsp;</td><td colspan=1 rowspan=1><pre class="CodeLeft"><b><a name="84321"></b><tt class="output">are</tt><b></a></b></pre></td><td width="10">&nbsp;</td></tr><tr valign="top"><td colspan=1 rowspan=1><pre class="CodeLeft"><b><a name="84323">$x$y 4</a></b></pre></td><td width="10">&nbsp;</td><td colspan=1 rowspan=1><pre class="CodeLeft"><b><a name="84325"></b><tt class="output">16</tt><b></a></b></pre></td><td width="10">&nbsp;</td></tr><tr><td colspan="20"><hr class="tablerule"></td></tr><tr valign="middle"><td colspan="20"></td></tr></table></p></p><dd><p class="Body"><a name="84330"> </a>The technique illustrated by the last example--constructing a procedure name "on the fly"--is used extensively in the Tornado tools to group a set of related procedures. The effect is similar to what can be achieved with function pointers in C. </p><dd><p class="Body"><a name="84331"> </a>For example, in Tornado tools, events are represented in Tcl as structured strings.  The first element of the string is the name of the event. Tcl scripts that handle events can search for the appropriate procedure to handle a particular event by mapping the event name to a procedure name, and calling that procedure if it exists. The following Tcl script demonstrates this approach:</p><dl class="margin"><dd><pre class="Code2"><b><a name="84332">proc shEventDispatch {event} {           set handlerProc "[lindex $event 0]_Handler"           if {[info procs $handlerProc] != ""} {              $handlerProc $event          } {              #event has no handler--do nothing.          } }</a></b></pre></dl></dl></dl><font face="Helvetica, sans-serif" class="sans"><h4 class="H3"><i><a name="84334">B.2.8  &nbsp;&nbsp;Control Structures</a></i></h4></font><dl class="margin"><dl class="margin"><dd><p class="Body"><a name="84336"> </a>Tcl provides all the popular control structures: conditionals (<b class="keyword">if</b>), loops (<b class="keyword">while</b>, <b class="keyword">for</b>, and <b class="keyword">foreach</b>), case statements (<b class="keyword">switch</b>), and explicit variable-scope control (<b class="keyword">global</b>, <b class="keyword">upvar</b>, and <b class="keyword">uplevel</b> variable declarations). By using these facilities, you can even define your own control structures. While there is nothing mysterious about these facilities, more detailed descriptions are beyond the scope of this summary.  For detailed information, see the books cited in the introduction to <a href="x-tclsumm2.html#83939"><i class="title">B.2&nbsp;A Taste of Tcl</i></a>.</p></dl></dl><font face="Helvetica, sans-serif" class="sans"><h4 class="H3"><i><a name="84341">B.2.9  &nbsp;&nbsp;Tcl Error Handling</a></i></h4></font><dl class="margin"><dl class="margin"><dd><p class="Body"><a name="84343"> </a><b></b>Every Tcl procedure, whether built-in or script, normally returns a string.  Tcl procedures may signal an error instead:  in a defined procedure, this is done with the <b class="tclProc">error</b> command.  This starts a process called <i class="term">unwinding</i>.  When a procedure signals an error, it passes to its caller a string containing information about the error.  Control is passed to the calling procedure.  If that procedure did not provide for this possibility by using the Tcl <b class="tclProc">catch</b> command, control is passed to its caller in turn.  This recursive unwinding continues until the top level, the Tcl interpreter, is reached.</p><dd><p class="Body"><a name="84345"> </a>As control is passed along, any procedure can catch the error and take one of two actions: signal another error and provide error information, or work around the error and return as usual, ending the unwinding process.</p><dd><p class="Body"><a name="84346"> </a>At each unwinding step, the Tcl interpreter adds a description of the current execution context to the Tcl variable <b class="symbol_lc">errorInfo</b>.  After unwinding ends, you can display <b class="symbol_lc">errorInfo</b> to trace error information.  Another variable, <b class="symbol_lc">errorCode</b>, may contain diagnostic information, such as an operating system dependent error code returned by a system call.</p></dl></dl><font face="Helvetica, sans-serif" class="sans"><h4 class="H3"><i><a name="84347">B.2.10  &nbsp;&nbsp;Integrating Tcl and C Applications</a></i></h4></font><dl class="margin"><dl class="margin"><dd><p class="Body"><a name="84348"> </a>Tcl is designed to integrate with C applications. The Tcl interpreter itself is distributed as a library, ready to link with other applications. The core of the Tcl integration strategy is to allow each application to add its own commands to the Tcl language. This is accomplished primarily through the subroutine <b class="routine"><i class="routine">Tcl_CreateCommand</i></b><b>(&nbsp;)</b> in the Tcl interpreter library, which associates a new Tcl command name and a pointer to an application-specific routine. For more details, consult the Tcl books cited in the introduction to <a href="x-tclsumm2.html#83939"><i class="title">B.2&nbsp;A Taste of Tcl</i></a>. <b></b></p></dl></dl><a name="foot"><hr></a><p class="navbar" align="right"><a href="index.html"><img border="0" alt="[Contents]" src="icons/contents.gif"></a><a href="GuideIX.html"><img border="0" alt="[Index]" src="icons/index.gif"></a><a href="x-tclsumm.html"><img border="0" alt="[Top]" src="icons/top.gif"></a><a href="x-tclsumm1.html"><img border="0" alt="[Prev]" src="icons/prev.gif"></a><a href="x-tclsumm3.html"><img border="0" alt="[Next]" src="icons/next.gif"></a></p></body></html><!---by WRS Documentation (), Wind River Systems, Inc.    conversion tool:  Quadralay WebWorks Publisher 4.0.11    template:         CSS Template, Jan 1998 - Jefro --->

⌨️ 快捷键说明

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