📄 language.tcl.html
字号:
encountered, which means that it can span multiple lines. Newline orsemicolon characters do not terminate the current command in suchcases. <SPANCLASS="PROPERTY">description</SPAN> properties usually make use of this:</P><TABLEBORDER="5"BGCOLOR="#E0E0F0"WIDTH="70%"><TR><TD><PRECLASS="PROGRAMLISTING">cdl_package CYGPKG_ERROR { description " This package contains the common list of error and status codes. It is held centrally to allow packages to interchange error codes and status codes in a common way, rather than each package having its own conventions for error/status reporting. The error codes are modelled on the POSIX style naming e.g. EINVAL etc. This package also provides the standard strerror() function to convert error codes to textual representation." …}</PRE></TD></TR></TABLE><P>The <SPANCLASS="APPLICATION">Tcl</SPAN> interpreter supports much the same forms of backslashsubstitution as other common programming languages. Some backslashsequences such as <TTCLASS="LITERAL">\n</TT> will be replaced by theappropriate character. The sequence <TTCLASS="LITERAL">\\</TT> will bereplaced by a single backslash. A backslash at the very end of a linewill cause that backslash, the newline character, and any white spaceat the start of the next line to be replaced by a single space. Hencethe following two Tcl commands are equivalent:</P><TABLEBORDER="5"BGCOLOR="#E0E0F0"WIDTH="70%"><TR><TD><PRECLASS="PROGRAMLISTING">puts "Hello\nworld\n"puts \"Helloworld"</PRE></TD></TR></TABLE><P>If a <SPANCLASS="PROPERTY">description</SPAN> string needs to contain quote marks or otherspecial characters then backslash escapes can be used. In addition toquote and backslash characters, the Tcl interpreter treats squarebrackets, the <TTCLASS="LITERAL">$</TT> character, and braces specially.Square brackets are used for command substitution, for example:</P><TABLEBORDER="5"BGCOLOR="#E0E0F0"WIDTH="70%"><TR><TD><PRECLASS="SCREEN">puts "The answer is [expr 6 * 9]"</PRE></TD></TR></TABLE><P>When the Tcl interpreter encounters the square brackets it will treatthe contents as another command that should be executed first, and theresult of executing that is used when continuing to process thescript. In this case the Tcl interpreter will execute the command<TTCLASS="LITERAL">expr 6 * 9</TT>, yielding a result of 42<ANAME="AEN1270"HREF="#FTN.AEN1270">[1]</A>and then theTcl interpreter will execute <TTCLASS="LITERAL">puts "The answer is 42"</TT>.It should be noted that the interpreter performs only one levelof substitution: if the result of performing command substitutionperforms further special characters such as square brackets then thesewill not be treated specially.</P><P>Command substitution will not prove useful for many <SPANCLASS="APPLICATION">CDL</SPAN> scripts,except for e.g. a <SPANCLASS="PROPERTY">define_proc</SPAN> property which involves a fragment of<SPANCLASS="APPLICATION">Tcl</SPAN> code. Potentially there are some interesting uses, for exampleto internationalize <SPANCLASS="PROPERTY">display</SPAN> strings. However care does have to betaken to avoid unexpected command substitution, for example if anoption description involves square brackets then typically these wouldrequire backslash-escapes.</P><P>The <TTCLASS="LITERAL">$</TT> character is used in Tcl scripts to performvariable substitution:</P><TABLEBORDER="5"BGCOLOR="#E0E0F0"WIDTH="70%"><TR><TD><PRECLASS="PROGRAMLISTING">set x [expr 6 * 9]puts "The answer is $x"</PRE></TD></TR></TABLE><P>Variable substitution, like command substitution, is unlikely toprove useful for many <SPANCLASS="APPLICATION">CDL</SPAN> scripts except in the context of<SPANCLASS="APPLICATION">Tcl</SPAN> fragments. If it is necessary to have a <TTCLASS="LITERAL">$</TT>character then a backslash escape may have to be used.</P><P>Braces are used to collect a sequence of characters into a singleargument, just like quotes. The difference is that variable, commandand backslash substitution do not occur inside braces (with thesole exception of backslash substitution at the end of a line).Therefore given a line in a <SPANCLASS="APPLICATION">CDL</SPAN> script such as:</P><TABLEBORDER="5"BGCOLOR="#E0E0F0"WIDTH="70%"><TR><TD><PRECLASS="PROGRAMLISTING">default_value {"RAM"}</PRE></TD></TR></TABLE><P>The braces are stripped off by the <SPANCLASS="APPLICATION">Tcl</SPAN> interpreter, leaving<TTCLASS="LITERAL">"RAM"</TT> which will be handled as a string constant bythe expression parsing code. The same effect could be achieved usingone of the following:</P><TABLEBORDER="5"BGCOLOR="#E0E0F0"WIDTH="70%"><TR><TD><PRECLASS="PROGRAMLISTING">default_value \"RAM\"default_value "\"RAM\""</PRE></TD></TR></TABLE><P>Generally the use of braces is less confusing. At this stage it isworth noting that the basic format of <SPANCLASS="APPLICATION">CDL</SPAN> data makes use ofbraces:</P><TABLEBORDER="5"BGCOLOR="#E0E0F0"WIDTH="70%"><TR><TD><PRECLASS="PROGRAMLISTING">cdl_option <name> { …};</PRE></TD></TR></TABLE><P>The <TTCLASS="LITERAL">cdl_option</TT> command is passed two arguments, a name and a body,where the body consists of everything inside the braces but not thebraces themselves. This body can then be executed in a recursiveinvocation of the <SPANCLASS="APPLICATION">Tcl</SPAN> interpreter. If a <SPANCLASS="APPLICATION">CDL</SPAN> script containsmismatched braces then the interpreter is likely to get ratherconfused and the resulting diagnostics may be difficult to understand. </P><P>Comments in Tcl scripts are introduced by a hash character<TTCLASS="LITERAL">#</TT>. However, a hash character only introduces acomment if it occurs where a command is expected. Consider thefollowing:</P><TABLEBORDER="5"BGCOLOR="#E0E0F0"WIDTH="70%"><TR><TD><PRECLASS="PROGRAMLISTING"># This is a commentputs "Hello" # world</PRE></TD></TR></TABLE><P>The first line is a valid comment, since the hash character occursright at the start where a command name is expected. The second linedoes not contain a comment. Instead it is an attempt to invoke the<TTCLASS="LITERAL">puts</TT> command with three arguments:<TTCLASS="LITERAL">Hello</TT>, <TTCLASS="LITERAL">#</TT> and<TTCLASS="LITERAL">world</TT>. These are not valid arguments for the<TTCLASS="LITERAL">puts</TT> command so an error will be raised.If the second line was rewritten as:</P><TABLEBORDER="5"BGCOLOR="#E0E0F0"WIDTH="70%"><TR><TD><PRECLASS="PROGRAMLISTING">puts "Hello"; # world</PRE></TD></TR></TABLE><P>then this is a valid Tcl script. The semicolon identifies the end ofthe current command, so the hash character occurs at a point where thenext command would start and hence it is interpreted as the start of acomment.</P><P>This handling of comments can lead to subtle behavior. Consider thefollowing:</P><TABLEBORDER="5"BGCOLOR="#E0E0F0"WIDTH="70%"><TR><TD><PRECLASS="PROGRAMLISTING">cdl_option WHATEVER {# This is a comment } default_value 0 …}</PRE></TD></TR></TABLE><P>Consider the way the Tcl interpreter processes this. The command nameand the first argument do not pose any special difficulties. Theopening brace is interpreted as the start of the next argument, whichcontinues until a closing brace is encountered. In this case theclosing brace occurs on the second line, so the second argument passedto <TTCLASS="LITERAL">cdl_option</TT> is<TTCLASS="LITERAL">\n # This is a comment</TT>. This second argument is processed in a recursiveinvocation of the Tcl interpreter and does not contain any commands,just a comment. Top-level script processing then resumes, and the nextcommand that is encountered is <TTCLASS="LITERAL">default_value</TT>. Sincethe parser is not currently processing a configuration option this isan error. Later on the Tcl interpreter would encounter a closing braceby itself, which is also an error.</P><P>For component writers who need more information about <SPANCLASS="APPLICATION">Tcl</SPAN>,especially about the language rather than the syntax, variousresources are available. A reasonable starting point is the<AHREF="http://www.tcl.tk/scripting/"TARGET="_top">Scriptics developerweb site</A>.</P></DIV><H3CLASS="FOOTNOTES">Notes</H3><TABLEBORDER="0"CLASS="FOOTNOTES"WIDTH="100%"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="5%"><ANAME="FTN.AEN1270"HREF="language.tcl.html#AEN1270">[1]</A></TD><TDALIGN="LEFT"VALIGN="TOP"WIDTH="95%"><P>It is possible that some versions of the Tcl interpreter will insteadproduce a result of 54 when asked to multiply six by nine. Appropriate<AHREF="http://www.douglasadams.com/creations/hhgg.html"TARGET="_top">referencedocumentation</A> should be consulted for more information on why42 is in fact the correct answer.</P></TD></TR></TABLE><DIVCLASS="NAVFOOTER"><HRALIGN="LEFT"WIDTH="100%"><TABLESUMMARY="Footer navigation table"WIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top"><AHREF="language.naming.html"ACCESSKEY="P">Prev</A></TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="cdl-guide.html"ACCESSKEY="H">Home</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top"><AHREF="language.values.html"ACCESSKEY="N">Next</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">Option Naming Convention</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="language.html"ACCESSKEY="U">Up</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top">Values and Expressions</TD></TR></TABLE></DIV></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -