Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

8232 lines (8135 sloc) 349.623 kb
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="application/xhtml+xml; charset=UTF-8" />
<meta name="generator" content="AsciiDoc 8.6.9" />
<title>Jim Tcl(n)</title>
<style type="text/css">
/* Shared CSS for AsciiDoc xhtml11 and html5 backends */
/* Default font. */
body {
font-family: Georgia,serif;
}
/* Title font. */
h1, h2, h3, h4, h5, h6,
div.title, caption.title,
thead, p.table.header,
#toctitle,
#author, #revnumber, #revdate, #revremark,
#footer {
font-family: Arial,Helvetica,sans-serif;
}
body {
margin: 1em 5% 1em 5%;
}
a {
color: blue;
text-decoration: underline;
}
a:visited {
color: fuchsia;
}
em {
font-style: italic;
color: navy;
}
strong {
font-weight: bold;
color: #083194;
}
h1, h2, h3, h4, h5, h6 {
color: #527bbd;
margin-top: 1.2em;
margin-bottom: 0.5em;
line-height: 1.3;
}
h1, h2, h3 {
border-bottom: 2px solid silver;
}
h2 {
padding-top: 0.5em;
}
h3 {
float: left;
}
h3 + * {
clear: left;
}
h5 {
font-size: 1.0em;
}
div.sectionbody {
margin-left: 0;
}
hr {
border: 1px solid silver;
}
p {
margin-top: 0.5em;
margin-bottom: 0.5em;
}
ul, ol, li > p {
margin-top: 0;
}
ul > li { color: #aaa; }
ul > li > * { color: black; }
.monospaced, code, pre {
font-family: "Courier New", Courier, monospace;
font-size: inherit;
color: navy;
padding: 0;
margin: 0;
}
pre {
white-space: pre-wrap;
}
#author {
color: #527bbd;
font-weight: bold;
font-size: 1.1em;
}
#email {
}
#revnumber, #revdate, #revremark {
}
#footer {
font-size: small;
border-top: 2px solid silver;
padding-top: 0.5em;
margin-top: 4.0em;
}
#footer-text {
float: left;
padding-bottom: 0.5em;
}
#footer-badges {
float: right;
padding-bottom: 0.5em;
}
#preamble {
margin-top: 1.5em;
margin-bottom: 1.5em;
}
div.imageblock, div.exampleblock, div.verseblock,
div.quoteblock, div.literalblock, div.listingblock, div.sidebarblock,
div.admonitionblock {
margin-top: 1.0em;
margin-bottom: 1.5em;
}
div.admonitionblock {
margin-top: 2.0em;
margin-bottom: 2.0em;
margin-right: 10%;
color: #606060;
}
div.content { /* Block element content. */
padding: 0;
}
/* Block element titles. */
div.title, caption.title {
color: #527bbd;
font-weight: bold;
text-align: left;
margin-top: 1.0em;
margin-bottom: 0.5em;
}
div.title + * {
margin-top: 0;
}
td div.title:first-child {
margin-top: 0.0em;
}
div.content div.title:first-child {
margin-top: 0.0em;
}
div.content + div.title {
margin-top: 0.0em;
}
div.sidebarblock > div.content {
background: #ffffee;
border: 1px solid #dddddd;
border-left: 4px solid #f0f0f0;
padding: 0.5em;
}
div.listingblock > div.content {
border: 1px solid #dddddd;
border-left: 5px solid #f0f0f0;
background: #f8f8f8;
padding: 0.5em;
}
div.quoteblock, div.verseblock {
padding-left: 1.0em;
margin-left: 1.0em;
margin-right: 10%;
border-left: 5px solid #f0f0f0;
color: #888;
}
div.quoteblock > div.attribution {
padding-top: 0.5em;
text-align: right;
}
div.verseblock > pre.content {
font-family: inherit;
font-size: inherit;
}
div.verseblock > div.attribution {
padding-top: 0.75em;
text-align: left;
}
/* DEPRECATED: Pre version 8.2.7 verse style literal block. */
div.verseblock + div.attribution {
text-align: left;
}
div.admonitionblock .icon {
vertical-align: top;
font-size: 1.1em;
font-weight: bold;
text-decoration: underline;
color: #527bbd;
padding-right: 0.5em;
}
div.admonitionblock td.content {
padding-left: 0.5em;
border-left: 3px solid #dddddd;
}
div.exampleblock > div.content {
border-left: 3px solid #dddddd;
padding-left: 0.5em;
}
div.imageblock div.content { padding-left: 0; }
span.image img { border-style: none; vertical-align: text-bottom; }
a.image:visited { color: white; }
dl {
margin-top: 0.8em;
margin-bottom: 0.8em;
}
dt {
margin-top: 0.5em;
margin-bottom: 0;
font-style: normal;
color: navy;
}
dd > *:first-child {
margin-top: 0.1em;
}
ul, ol {
list-style-position: outside;
}
ol.arabic {
list-style-type: decimal;
}
ol.loweralpha {
list-style-type: lower-alpha;
}
ol.upperalpha {
list-style-type: upper-alpha;
}
ol.lowerroman {
list-style-type: lower-roman;
}
ol.upperroman {
list-style-type: upper-roman;
}
div.compact ul, div.compact ol,
div.compact p, div.compact p,
div.compact div, div.compact div {
margin-top: 0.1em;
margin-bottom: 0.1em;
}
tfoot {
font-weight: bold;
}
td > div.verse {
white-space: pre;
}
div.hdlist {
margin-top: 0.8em;
margin-bottom: 0.8em;
}
div.hdlist tr {
padding-bottom: 15px;
}
dt.hdlist1.strong, td.hdlist1.strong {
font-weight: bold;
}
td.hdlist1 {
vertical-align: top;
font-style: normal;
padding-right: 0.8em;
color: navy;
}
td.hdlist2 {
vertical-align: top;
}
div.hdlist.compact tr {
margin: 0;
padding-bottom: 0;
}
.comment {
background: yellow;
}
.footnote, .footnoteref {
font-size: 0.8em;
}
span.footnote, span.footnoteref {
vertical-align: super;
}
#footnotes {
margin: 20px 0 20px 0;
padding: 7px 0 0 0;
}
#footnotes div.footnote {
margin: 0 0 5px 0;
}
#footnotes hr {
border: none;
border-top: 1px solid silver;
height: 1px;
text-align: left;
margin-left: 0;
width: 20%;
min-width: 100px;
}
div.colist td {
padding-right: 0.5em;
padding-bottom: 0.3em;
vertical-align: top;
}
div.colist td img {
margin-top: 0.3em;
}
@media print {
#footer-badges { display: none; }
}
#toc {
margin-bottom: 2.5em;
}
#toctitle {
color: #527bbd;
font-size: 1.1em;
font-weight: bold;
margin-top: 1.0em;
margin-bottom: 0.1em;
}
div.toclevel0, div.toclevel1, div.toclevel2, div.toclevel3, div.toclevel4 {
margin-top: 0;
margin-bottom: 0;
}
div.toclevel2 {
margin-left: 2em;
font-size: 0.9em;
}
div.toclevel3 {
margin-left: 4em;
font-size: 0.9em;
}
div.toclevel4 {
margin-left: 6em;
font-size: 0.9em;
}
span.aqua { color: aqua; }
span.black { color: black; }
span.blue { color: blue; }
span.fuchsia { color: fuchsia; }
span.gray { color: gray; }
span.green { color: green; }
span.lime { color: lime; }
span.maroon { color: maroon; }
span.navy { color: navy; }
span.olive { color: olive; }
span.purple { color: purple; }
span.red { color: red; }
span.silver { color: silver; }
span.teal { color: teal; }
span.white { color: white; }
span.yellow { color: yellow; }
span.aqua-background { background: aqua; }
span.black-background { background: black; }
span.blue-background { background: blue; }
span.fuchsia-background { background: fuchsia; }
span.gray-background { background: gray; }
span.green-background { background: green; }
span.lime-background { background: lime; }
span.maroon-background { background: maroon; }
span.navy-background { background: navy; }
span.olive-background { background: olive; }
span.purple-background { background: purple; }
span.red-background { background: red; }
span.silver-background { background: silver; }
span.teal-background { background: teal; }
span.white-background { background: white; }
span.yellow-background { background: yellow; }
span.big { font-size: 2em; }
span.small { font-size: 0.6em; }
span.underline { text-decoration: underline; }
span.overline { text-decoration: overline; }
span.line-through { text-decoration: line-through; }
div.unbreakable { page-break-inside: avoid; }
/*
* xhtml11 specific
*
* */
div.tableblock {
margin-top: 1.0em;
margin-bottom: 1.5em;
}
div.tableblock > table {
border: 3px solid #527bbd;
}
thead, p.table.header {
font-weight: bold;
color: #527bbd;
}
p.table {
margin-top: 0;
}
/* Because the table frame attribute is overriden by CSS in most browsers. */
div.tableblock > table[frame="void"] {
border-style: none;
}
div.tableblock > table[frame="hsides"] {
border-left-style: none;
border-right-style: none;
}
div.tableblock > table[frame="vsides"] {
border-top-style: none;
border-bottom-style: none;
}
/*
* html5 specific
*
* */
table.tableblock {
margin-top: 1.0em;
margin-bottom: 1.5em;
}
thead, p.tableblock.header {
font-weight: bold;
color: #527bbd;
}
p.tableblock {
margin-top: 0;
}
table.tableblock {
border-width: 3px;
border-spacing: 0px;
border-style: solid;
border-color: #527bbd;
border-collapse: collapse;
}
th.tableblock, td.tableblock {
border-width: 1px;
padding: 4px;
border-style: solid;
border-color: #527bbd;
}
table.tableblock.frame-topbot {
border-left-style: hidden;
border-right-style: hidden;
}
table.tableblock.frame-sides {
border-top-style: hidden;
border-bottom-style: hidden;
}
table.tableblock.frame-none {
border-style: hidden;
}
th.tableblock.halign-left, td.tableblock.halign-left {
text-align: left;
}
th.tableblock.halign-center, td.tableblock.halign-center {
text-align: center;
}
th.tableblock.halign-right, td.tableblock.halign-right {
text-align: right;
}
th.tableblock.valign-top, td.tableblock.valign-top {
vertical-align: top;
}
th.tableblock.valign-middle, td.tableblock.valign-middle {
vertical-align: middle;
}
th.tableblock.valign-bottom, td.tableblock.valign-bottom {
vertical-align: bottom;
}
/*
* manpage specific
*
* */
body.manpage h1 {
padding-top: 0.5em;
padding-bottom: 0.5em;
border-top: 2px solid silver;
border-bottom: 2px solid silver;
}
body.manpage h2 {
border-style: none;
}
body.manpage div.sectionbody {
margin-left: 3em;
}
@media print {
body.manpage div#toc { display: none; }
}
</style>
<script type="text/javascript">
/*<![CDATA[*/
var asciidoc = { // Namespace.
/////////////////////////////////////////////////////////////////////
// Table Of Contents generator
/////////////////////////////////////////////////////////////////////
/* Author: Mihai Bazon, September 2002
* http://students.infoiasi.ro/~mishoo
*
* Table Of Content generator
* Version: 0.4
*
* Feel free to use this script under the terms of the GNU General Public
* License, as long as you do not remove or alter this notice.
*/
/* modified by Troy D. Hanson, September 2006. License: GPL */
/* modified by Stuart Rackham, 2006, 2009. License: GPL */
// toclevels = 1..4.
toc: function (toclevels) {
function getText(el) {
var text = "";
for (var i = el.firstChild; i != null; i = i.nextSibling) {
if (i.nodeType == 3 /* Node.TEXT_NODE */) // IE doesn't speak constants.
text += i.data;
else if (i.firstChild != null)
text += getText(i);
}
return text;
}
function TocEntry(el, text, toclevel) {
this.element = el;
this.text = text;
this.toclevel = toclevel;
}
function tocEntries(el, toclevels) {
var result = new Array;
var re = new RegExp('[hH]([1-'+(toclevels+1)+'])');
// Function that scans the DOM tree for header elements (the DOM2
// nodeIterator API would be a better technique but not supported by all
// browsers).
var iterate = function (el) {
for (var i = el.firstChild; i != null; i = i.nextSibling) {
if (i.nodeType == 1 /* Node.ELEMENT_NODE */) {
var mo = re.exec(i.tagName);
if (mo && (i.getAttribute("class") || i.getAttribute("className")) != "float") {
result[result.length] = new TocEntry(i, getText(i), mo[1]-1);
}
iterate(i);
}
}
}
iterate(el);
return result;
}
var toc = document.getElementById("toc");
if (!toc) {
return;
}
// Delete existing TOC entries in case we're reloading the TOC.
var tocEntriesToRemove = [];
var i;
for (i = 0; i < toc.childNodes.length; i++) {
var entry = toc.childNodes[i];
if (entry.nodeName.toLowerCase() == 'div'
&& entry.getAttribute("class")
&& entry.getAttribute("class").match(/^toclevel/))
tocEntriesToRemove.push(entry);
}
for (i = 0; i < tocEntriesToRemove.length; i++) {
toc.removeChild(tocEntriesToRemove[i]);
}
// Rebuild TOC entries.
var entries = tocEntries(document.getElementById("content"), toclevels);
for (var i = 0; i < entries.length; ++i) {
var entry = entries[i];
if (entry.element.id == "")
entry.element.id = "_toc_" + i;
var a = document.createElement("a");
a.href = "#" + entry.element.id;
a.appendChild(document.createTextNode(entry.text));
var div = document.createElement("div");
div.appendChild(a);
div.className = "toclevel" + entry.toclevel;
toc.appendChild(div);
}
if (entries.length == 0)
toc.parentNode.removeChild(toc);
},
/////////////////////////////////////////////////////////////////////
// Footnotes generator
/////////////////////////////////////////////////////////////////////
/* Based on footnote generation code from:
* http://www.brandspankingnew.net/archive/2005/07/format_footnote.html
*/
footnotes: function () {
// Delete existing footnote entries in case we're reloading the footnodes.
var i;
var noteholder = document.getElementById("footnotes");
if (!noteholder) {
return;
}
var entriesToRemove = [];
for (i = 0; i < noteholder.childNodes.length; i++) {
var entry = noteholder.childNodes[i];
if (entry.nodeName.toLowerCase() == 'div' && entry.getAttribute("class") == "footnote")
entriesToRemove.push(entry);
}
for (i = 0; i < entriesToRemove.length; i++) {
noteholder.removeChild(entriesToRemove[i]);
}
// Rebuild footnote entries.
var cont = document.getElementById("content");
var spans = cont.getElementsByTagName("span");
var refs = {};
var n = 0;
for (i=0; i<spans.length; i++) {
if (spans[i].className == "footnote") {
n++;
var note = spans[i].getAttribute("data-note");
if (!note) {
// Use [\s\S] in place of . so multi-line matches work.
// Because JavaScript has no s (dotall) regex flag.
note = spans[i].innerHTML.match(/\s*\[([\s\S]*)]\s*/)[1];
spans[i].innerHTML =
"[<a id='_footnoteref_" + n + "' href='#_footnote_" + n +
"' title='View footnote' class='footnote'>" + n + "</a>]";
spans[i].setAttribute("data-note", note);
}
noteholder.innerHTML +=
"<div class='footnote' id='_footnote_" + n + "'>" +
"<a href='#_footnoteref_" + n + "' title='Return to text'>" +
n + "</a>. " + note + "</div>";
var id =spans[i].getAttribute("id");
if (id != null) refs["#"+id] = n;
}
}
if (n == 0)
noteholder.parentNode.removeChild(noteholder);
else {
// Process footnoterefs.
for (i=0; i<spans.length; i++) {
if (spans[i].className == "footnoteref") {
var href = spans[i].getElementsByTagName("a")[0].getAttribute("href");
href = href.match(/#.*/)[0]; // Because IE return full URL.
n = refs[href];
spans[i].innerHTML =
"[<a href='#_footnote_" + n +
"' title='View footnote' class='footnote'>" + n + "</a>]";
}
}
}
},
install: function(toclevels) {
var timerId;
function reinstall() {
asciidoc.footnotes();
if (toclevels) {
asciidoc.toc(toclevels);
}
}
function reinstallAndRemoveTimer() {
clearInterval(timerId);
reinstall();
}
timerId = setInterval(reinstall, 500);
if (document.addEventListener)
document.addEventListener("DOMContentLoaded", reinstallAndRemoveTimer, false);
else
window.onload = reinstallAndRemoveTimer;
}
}
asciidoc.install();
/*]]>*/
</script>
</head>
<body class="manpage">
<div id="header">
<h1>
Jim Tcl(n) Manual Page
</h1>
<h2>NAME</h2>
<div class="sectionbody">
<p>Jim Tcl v0.76 -
reference manual for the Jim Tcl scripting language
</p>
</div>
</div>
<div id="content">
<div class="sect1">
<h2 id="_synopsis">SYNOPSIS</h2>
<div class="sectionbody">
<div class="literalblock">
<div class="content">
<pre><code>cc &lt;source&gt; -ljim</code></pre>
</div></div>
<div class="paragraph"><p>or</p></div>
<div class="literalblock">
<div class="content">
<pre><code>jimsh [&lt;scriptfile&gt;]
jimsh -e '&lt;immediate-script&gt;'
jimsh --version</code></pre>
</div></div>
<div class="ulist"><div class="title">Quick Index</div><ul>
<li>
<p>
<a href="#CommandIndex">Command Reference</a>
</p>
</li>
<li>
<p>
<a href="#OperatorPrecedence">Operator Precedence</a>
</p>
</li>
<li>
<p>
<a href="#BuiltinVariables">Builtin Variables</a>
</p>
</li>
<li>
<p>
<a href="#BackslashSequences">Backslash Sequences</a>
</p>
</li>
</ul></div>
</div>
</div>
<div class="sect1">
<h2 id="_introduction">INTRODUCTION</h2>
<div class="sectionbody">
<div class="paragraph"><p>Jim Tcl is a small footprint reimplementation of the Tcl scripting language.
The core language engine is compatible with Tcl 8.5+, while implementing
a significant subset of the Tcl 8.6 command set, plus additional features
available only in Jim Tcl.</p></div>
<div class="paragraph"><p>Some notable differences with Tcl 8.5/8.6 are:</p></div>
<div class="olist arabic"><ol class="arabic">
<li>
<p>
Object-based I/O (aio), but with a Tcl-compatibility layer
</p>
</li>
<li>
<p>
I/O: Support for sockets and pipes including udp, unix domain sockets and IPv6
</p>
</li>
<li>
<p>
Integers are 64bit
</p>
</li>
<li>
<p>
Support for references (<a href="#_ref"><strong><code>ref</code></strong></a>/<a href="#_getref"><strong><code>getref</code></strong></a>/<a href="#_setref"><strong><code>setref</code></strong></a>) and garbage collection
</p>
</li>
<li>
<p>
Builtin dictionary type (<a href="#_dict"><strong><code>dict</code></strong></a>) with some limitations compared to Tcl 8.6
</p>
</li>
<li>
<p>
<a href="#_env"><strong><code>env</code></strong></a> command to access environment variables
</p>
</li>
<li>
<p>
Operating system features: <a href="#cmd_1"><strong><code>os.fork</code></strong></a>, <a href="#cmd_1"><strong><code>os.wait</code></strong></a>, <a href="#cmd_1"><strong><code>os.uptime</code></strong></a>, <a href="#_signal"><strong><code>signal</code></strong></a>, <a href="#_alarm"><strong><code>alarm</code></strong></a>, <a href="#_sleep"><strong><code>sleep</code></strong></a>
</p>
</li>
<li>
<p>
Much better error reporting. <a href="#_info"><strong><code>info</code></strong></a> <code>stacktrace</code> as a replacement for <em>$errorInfo</em>, <em>$errorCode</em>
</p>
</li>
<li>
<p>
Support for "static" variables in procedures
</p>
</li>
<li>
<p>
Threads and coroutines are not supported
</p>
</li>
<li>
<p>
Command and variable traces are not supported
</p>
</li>
<li>
<p>
Built-in command line editing
</p>
</li>
<li>
<p>
Expression shorthand syntax: <code>$(&#8230;)</code>
</p>
</li>
<li>
<p>
Modular build allows many features to be omitted or built as dynamic, loadable modules
</p>
</li>
<li>
<p>
Highly suitable for use in an embedded environment
</p>
</li>
<li>
<p>
Support for UDP, IPv6, Unix-Domain sockets in addition to TCP sockets
</p>
</li>
</ol></div>
</div>
</div>
<div class="sect1">
<h2 id="_recent_changes">RECENT CHANGES</h2>
<div class="sectionbody">
<div class="sect2">
<h3 id="_changes_between_0_75_and_0_76">Changes between 0.75 and 0.76</h3>
<div class="olist arabic"><ol class="arabic">
<li>
<p>
Add support for <a href="#_file"><strong><code>file</code></strong></a> <code>link</code>
</p>
</li>
<li>
<p>
<a href="#_glob"><strong><code>glob</code></strong></a> now supports the <em>--tails</em> option
</p>
</li>
<li>
<p>
Add support for <a href="#_string"><strong><code>string</code></strong></a> <code>cat</code>
</p>
</li>
<li>
<p>
Allow <a href="#_info"><strong><code>info</code></strong></a> <code>source</code> to add source info
</p>
</li>
</ol></div>
</div>
<div class="sect2">
<h3 id="_changes_between_0_74_and_0_75">Changes between 0.74 and 0.75</h3>
<div class="olist arabic"><ol class="arabic">
<li>
<p>
<a href="#_binary"><strong><code>binary</code></strong></a>, <a href="#cmd_3"><strong><code>pack</code></strong></a> and <a href="#cmd_3"><strong><code>unpack</code></strong></a> now support floating point
</p>
</li>
<li>
<p>
<a href="#_file"><strong><code>file</code></strong></a> <code>copy</code> <em>-force</em> handles source and target as the same file
</p>
</li>
<li>
<p>
<a href="#_format"><strong><code>format</code></strong></a> now supports <code>%b</code> for binary conversion
</p>
</li>
<li>
<p>
<a href="#_lsort"><strong><code>lsort</code></strong></a> now supports <em>-unique</em> and <em>-real</em>
</p>
</li>
<li>
<p>
Add support for half-close with <a href="#_aio"><strong><code>aio</code></strong></a> <code>close</code> ?r|w?
</p>
</li>
<li>
<p>
Add <a href="#_socket"><strong><code>socket</code></strong></a> <code>pair</code> for a bidirectional pipe
</p>
</li>
<li>
<p>
Add <em>--random-hash</em> to randomise hash tables for greater security
</p>
</li>
<li>
<p>
<a href="#_dict"><strong><code>dict</code></strong></a> now supports <em>for</em>, <em>values</em>, <em>incr</em>, <em>append</em>, <em>lappend</em>, <em>update</em>, <em>info</em> and <em>replace</em>
</p>
</li>
<li>
<p>
<a href="#_file"><strong><code>file</code></strong></a> <code>stat</code> no longer requires the variable name
</p>
</li>
</ol></div>
</div>
<div class="sect2">
<h3 id="_changes_between_0_73_and_0_74">Changes between 0.73 and 0.74</h3>
<div class="olist arabic"><ol class="arabic">
<li>
<p>
Numbers with leading zeros are treated as decimal, not octal
</p>
</li>
<li>
<p>
Add <a href="#_aio"><strong><code>aio</code></strong></a> <code>isatty</code>
</p>
</li>
<li>
<p>
Add LFS (64 bit) support for <a href="#_aio"><strong><code>aio</code></strong></a> <code>seek</code>, <a href="#_aio"><strong><code>aio</code></strong></a> <code>tell</code>, <a href="#_aio"><strong><code>aio</code></strong></a> <code>copyto</code>, <a href="#_file"><strong><code>file</code></strong></a> <code>copy</code>
</p>
</li>
<li>
<p>
<a href="#_string"><strong><code>string</code></strong></a> <code>compare</code> and <a href="#_string"><strong><code>string</code></strong></a> <code>equal</code> now support <em>-length</em>
</p>
</li>
<li>
<p>
<a href="#_glob"><strong><code>glob</code></strong></a> now supports <em>-directory</em>
</p>
</li>
</ol></div>
</div>
<div class="sect2">
<h3 id="_changes_between_0_72_and_0_73">Changes between 0.72 and 0.73</h3>
<div class="olist arabic"><ol class="arabic">
<li>
<p>
Built-in regexp now support non-capturing parentheses: (?:&#8230;)
</p>
</li>
<li>
<p>
Add <a href="#_string"><strong><code>string</code></strong></a> <code>replace</code>
</p>
</li>
<li>
<p>
Add <a href="#_string"><strong><code>string</code></strong></a> <code>totitle</code>
</p>
</li>
<li>
<p>
Add <a href="#_info"><strong><code>info</code></strong></a> <code>statics</code>
</p>
</li>
<li>
<p>
Add <code>build-jim-ext</code> for easy separate building of loadable modules (extensions)
</p>
</li>
<li>
<p>
<a href="#_local"><strong><code>local</code></strong></a> now works with any command, not just procs
</p>
</li>
<li>
<p>
Add <a href="#_info"><strong><code>info</code></strong></a> <code>alias</code> to access the target of an alias
</p>
</li>
<li>
<p>
UTF-8 encoding past the basic multilingual plane (BMP) is supported
</p>
</li>
<li>
<p>
Add <a href="#_tcl_prefix"><strong><code>tcl::prefix</code></strong></a>
</p>
</li>
<li>
<p>
Add <a href="#_history"><strong><code>history</code></strong></a>
</p>
</li>
<li>
<p>
Most extensions are now enabled by default
</p>
</li>
<li>
<p>
Add support for namespaces and the <a href="#_namespace"><strong><code>namespace</code></strong></a> command
</p>
</li>
<li>
<p>
Add <a href="#_apply"><strong><code>apply</code></strong></a>
</p>
</li>
</ol></div>
</div>
<div class="sect2">
<h3 id="_changes_between_0_71_and_0_72">Changes between 0.71 and 0.72</h3>
<div class="olist arabic"><ol class="arabic">
<li>
<p>
procs now allow <em>args</em> and optional parameters in any position
</p>
</li>
<li>
<p>
Add Tcl-compatible expr functions, <code>rand()</code>, <code>srand()</code> and <code>pow()</code>
</p>
</li>
<li>
<p>
Add support for the <em>-force</em> option to <a href="#_file"><strong><code>file</code></strong></a> <code>delete</code>
</p>
</li>
<li>
<p>
Better diagnostics when <a href="#_source"><strong><code>source</code></strong></a> fails to load a script with a missing quote or bracket
</p>
</li>
<li>
<p>
New <code>tcl_platform(pathSeparator)</code>
</p>
</li>
<li>
<p>
Add support settings the modification time with <a href="#_file"><strong><code>file</code></strong></a> <code>mtime</code>
</p>
</li>
<li>
<p>
<a href="#_exec"><strong><code>exec</code></strong></a> is now fully supported on win32 (mingw32)
</p>
</li>
<li>
<p>
<a href="#_file"><strong><code>file</code></strong></a> <code>join</code>, <a href="#_pwd"><strong><code>pwd</code></strong></a>, <a href="#_glob"><strong><code>glob</code></strong></a> etc. now work for mingw32
</p>
</li>
<li>
<p>
Line editing is now supported for the win32 console (mingw32)
</p>
</li>
<li>
<p>
Add <a href="#_aio"><strong><code>aio</code></strong></a> <code>listen</code> command
</p>
</li>
</ol></div>
</div>
<div class="sect2">
<h3 id="_changes_between_0_70_and_0_71">Changes between 0.70 and 0.71</h3>
<div class="olist arabic"><ol class="arabic">
<li>
<p>
Allow <em>args</em> to be renamed in procs
</p>
</li>
<li>
<p>
Add <code>$(&#8230;)</code> shorthand syntax for expressions
</p>
</li>
<li>
<p>
Add automatic reference variables in procs with <code>&amp;var</code> syntax
</p>
</li>
<li>
<p>
Support <code>jimsh --version</code>
</p>
</li>
<li>
<p>
Additional variables in <code>tcl_platform()</code>
</p>
</li>
<li>
<p>
<a href="#_local"><strong><code>local</code></strong></a> procs now push existing commands and <a href="#_upcall"><strong><code>upcall</code></strong></a> can call them
</p>
</li>
<li>
<p>
Add <a href="#_loop"><strong><code>loop</code></strong></a> command (TclX compatible)
</p>
</li>
<li>
<p>
Add <a href="#_aio"><strong><code>aio</code></strong></a> <code>buffering</code> command
</p>
</li>
<li>
<p>
<a href="#_info"><strong><code>info</code></strong></a> <code>complete</code> can now return the missing character
</p>
</li>
<li>
<p>
<a href="#_binary"><strong><code>binary</code></strong></a> <code>format</code> and <a href="#_binary"><strong><code>binary</code></strong></a> <code>scan</code> are now (optionally) supported
</p>
</li>
<li>
<p>
Add <a href="#_string"><strong><code>string</code></strong></a> <code>byterange</code>
</p>
</li>
<li>
<p>
Built-in regexp now support non-greedy repetition (*?, +?, ??)
</p>
</li>
</ol></div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_tcl_introduction">TCL INTRODUCTION</h2>
<div class="sectionbody">
<div class="paragraph"><p>Tcl stands for <em>tool command language</em> and is pronounced <em>tickle.</em>
It is actually two things: a language and a library.</p></div>
<div class="paragraph"><p>First, Tcl is a simple textual language, intended primarily for
issuing commands to interactive programs such as text editors,
debuggers, illustrators, and shells. It has a simple syntax and is also
programmable, so Tcl users can write command procedures to provide more
powerful commands than those in the built-in set.</p></div>
<div class="paragraph"><p>Second, Tcl is a library package that can be embedded in application
programs. The Tcl library consists of a parser for the Tcl language,
routines to implement the Tcl built-in commands, and procedures that
allow each application to extend Tcl with additional commands specific
to that application. The application program generates Tcl commands and
passes them to the Tcl parser for execution. Commands may be generated
by reading characters from an input source, or by associating command
strings with elements of the application&#8217;s user interface, such as menu
entries, buttons, or keystrokes.</p></div>
<div class="paragraph"><p>When the Tcl library receives commands it parses them into component
fields and executes built-in commands directly. For commands implemented
by the application, Tcl calls back to the application to execute the
commands. In many cases commands will invoke recursive invocations of the
Tcl interpreter by passing in additional strings to execute (procedures,
looping commands, and conditional commands all work in this way).</p></div>
<div class="paragraph"><p>An application program gains three advantages by using Tcl for its command
language. First, Tcl provides a standard syntax: once users know Tcl,
they will be able to issue commands easily to any Tcl-based application.
Second, Tcl provides programmability. All a Tcl application needs
to do is to implement a few application-specific low-level commands.
Tcl provides many utility commands plus a general programming interface
for building up complex command procedures. By using Tcl, applications
need not re-implement these features.</p></div>
<div class="paragraph"><p>Third, Tcl can be used as a common language for communicating between
applications. Inter-application communication is not built into the
Tcl core described here, but various add-on libraries, such as the Tk
toolkit, allow applications to issue commands to each other. This makes
it possible for applications to work together in much more powerful ways
than was previously possible.</p></div>
<div class="paragraph"><p>Fourth, Jim Tcl includes a command processor, <code>jimsh</code>, which can be
used to run standalone Tcl scripts, or to run Tcl commands interactively.</p></div>
<div class="paragraph"><p>This manual page focuses primarily on the Tcl language. It describes
the language syntax and the built-in commands that will be available
in any application based on Tcl. The individual library procedures are
described in more detail in separate manual pages, one per procedure.</p></div>
</div>
</div>
<div class="sect1">
<h2 id="_jimsh_command_interpreter">JIMSH COMMAND INTERPRETER</h2>
<div class="sectionbody">
<div class="paragraph"><p>A simple, but powerful command processor, <code>jimsh</code>, is part of Jim Tcl.
It may be invoked in interactive mode as:</p></div>
<div class="literalblock">
<div class="content">
<pre><code>jimsh</code></pre>
</div></div>
<div class="paragraph"><p>or to process the Tcl script in a file with:</p></div>
<div class="literalblock">
<div class="content">
<pre><code>jimsh filename</code></pre>
</div></div>
<div class="paragraph"><p>It may also be invoked to execute an immediate script with:</p></div>
<div class="literalblock">
<div class="content">
<pre><code>jimsh -e "script"</code></pre>
</div></div>
<div class="sect2">
<h3 id="_interactive_mode">Interactive Mode</h3>
<div class="paragraph"><p>Interactive mode reads Tcl commands from standard input, evaluates
those commands and prints the results.</p></div>
<div class="literalblock">
<div class="content">
<pre><code>$ jimsh
Welcome to Jim version 0.73, Copyright (c) 2005-8 Salvatore Sanfilippo
. info version
0.73
. lsort [info commands p*]
package parray pid popen proc puts pwd
. foreach i {a b c} {
{&gt; puts $i
{&gt; }
a
b
c
. bad
invalid command name "bad"
[error] . exit
$</code></pre>
</div></div>
<div class="paragraph"><p>If <code>jimsh</code> is configured with line editing (it is by default) and a VT-100-compatible
terminal is detected, Emacs-style line editing commands are available, including:
arrow keys, <code>^W</code> to erase a word, <code>^U</code> to erase the line, <code>^R</code> for reverse incremental search
in history. Additionally, the <code>h</code> command may be used to display the command history.</p></div>
<div class="paragraph"><p>Command line history is automatically saved and loaded from <code>~/.jim_history</code></p></div>
<div class="paragraph"><p>In interactive mode, <code>jimsh</code> automatically runs the script <code>~/.jimrc</code> at startup
if it exists.</p></div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_interpreters">INTERPRETERS</h2>
<div class="sectionbody">
<div class="paragraph"><p>The central data structure in Tcl is an interpreter (C type <em>Jim_Interp</em>).
An interpreter consists of a set of command bindings, a set of variable
values, and a few other miscellaneous pieces of state. Each Tcl command
is interpreted in the context of a particular interpreter.</p></div>
<div class="paragraph"><p>Some Tcl-based applications will maintain multiple interpreters
simultaneously, each associated with a different widget or portion of
the application. Interpreters are relatively lightweight structures.
They can be created and deleted quickly, so application programmers should
feel free to use multiple interpreters if that simplifies the application.</p></div>
</div>
</div>
<div class="sect1">
<h2 id="_data_types">DATA TYPES</h2>
<div class="sectionbody">
<div class="paragraph"><p>Tcl supports only one type of data: strings. All commands, all arguments
to commands, all command results, and all variable values are strings.</p></div>
<div class="paragraph"><p>Where commands require numeric arguments or return numeric results,
the arguments and results are passed as strings. Many commands expect
their string arguments to have certain formats, but this interpretation
is up to the individual commands. For example, arguments often contain
Tcl command strings, which may get executed as part of the commands.
The easiest way to understand the Tcl interpreter is to remember that
everything is just an operation on a string. In many cases Tcl constructs
will look similar to more structured constructs from other languages.
However, the Tcl constructs are not structured at all; they are just
strings of characters, and this gives them a different behaviour than
the structures they may look like.</p></div>
<div class="paragraph"><p>Although the exact interpretation of a Tcl string depends on who is doing
the interpretation, there are three common forms that strings take:
commands, expressions, and lists. The major sections below discuss
these three forms in more detail.</p></div>
</div>
</div>
<div class="sect1">
<h2 id="_basic_command_syntax">BASIC COMMAND SYNTAX</h2>
<div class="sectionbody">
<div class="paragraph"><p>The Tcl language has syntactic similarities to both the Unix shells
and Lisp. However, the interpretation of commands is different
in Tcl than in either of those other two systems.
A Tcl command string consists of one or more commands separated
by newline characters or semi-colons.
Each command consists of a collection of fields separated by
white space (spaces or tabs).
The first field must be the name of a command, and the
additional fields, if any, are arguments that will be passed to
that command. For example, the command:</p></div>
<div class="literalblock">
<div class="content">
<pre><code>set a 22</code></pre>
</div></div>
<div class="paragraph"><p>has three fields: the first, <a href="#_set"><strong><code>set</code></strong></a>, is the name of a Tcl command, and
the last two, <em>a</em> and <em>22</em>, will be passed as arguments to
the <a href="#_set"><strong><code>set</code></strong></a> command. The command name may refer either to a built-in
Tcl command, an application-specific command bound in with the library
procedure <em>Jim_CreateCommand</em>, or a command procedure defined with the
<a href="#_proc"><strong><code>proc</code></strong></a> built-in command.</p></div>
<div class="paragraph"><p>Arguments are passed literally as text strings. Individual commands may
interpret those strings in any fashion they wish. The <a href="#_set"><strong><code>set</code></strong></a> command,
for example, will treat its first argument as the name of a variable
and its second argument as a string value to assign to that variable.
For other commands arguments may be interpreted as integers, lists,
file names, or Tcl commands.</p></div>
<div class="paragraph"><p>Command names should normally be typed completely (e.g. no abbreviations).
However, if the Tcl interpreter cannot locate a command it invokes a
special command named <a href="#_unknown"><strong><code>unknown</code></strong></a> which attempts to find or create the
command.</p></div>
<div class="paragraph"><p>For example, at many sites <a href="#_unknown"><strong><code>unknown</code></strong></a> will search through library
directories for the desired command and create it as a Tcl procedure if
it is found. The <a href="#_unknown"><strong><code>unknown</code></strong></a> command often provides automatic completion
of abbreviated commands, but usually only for commands that were typed
interactively.</p></div>
<div class="paragraph"><p>It&#8217;s probably a bad idea to use abbreviations in command scripts and
other forms that will be re-used over time: changes to the command set
may cause abbreviations to become ambiguous, resulting in scripts that
no longer work.</p></div>
</div>
</div>
<div class="sect1">
<h2 id="_comments">COMMENTS</h2>
<div class="sectionbody">
<div class="paragraph"><p>If the first non-blank character in a command is <code>#</code>, then everything
from the <code>#</code> up through the next newline character is treated as
a comment and ignored. When comments are embedded inside nested
commands (e.g. fields enclosed in braces) they must have properly-matched
braces (this is necessary because when Tcl parses the top-level command
it doesn&#8217;t yet know that the nested field will be used as a command so
it cannot process the nested comment character as a comment).</p></div>
</div>
</div>
<div class="sect1">
<h2 id="_grouping_arguments_with_double_quotes">GROUPING ARGUMENTS WITH DOUBLE-QUOTES</h2>
<div class="sectionbody">
<div class="paragraph"><p>Normally each argument field ends at the next white space, but
double-quotes may be used to create arguments with embedded space.</p></div>
<div class="paragraph"><p>If an argument field begins with a double-quote, then the argument isn&#8217;t
terminated by white space (including newlines) or a semi-colon (see below
for information on semi-colons); instead it ends at the next double-quote
character. The double-quotes are not included in the resulting argument.
For example, the command</p></div>
<div class="literalblock">
<div class="content">
<pre><code>set a "This is a single argument"</code></pre>
</div></div>
<div class="paragraph"><p>will pass two arguments to <a href="#_set"><strong><code>set</code></strong></a>: <em>a</em> and <em>This is a single argument</em>.</p></div>
<div class="paragraph"><p>Within double-quotes, command substitutions, variable substitutions,
and backslash substitutions still occur, as described below. If the
first character of a command field is not a quote, then quotes receive
no special interpretation in the parsing of that field.</p></div>
</div>
</div>
<div class="sect1">
<h2 id="_grouping_arguments_with_braces">GROUPING ARGUMENTS WITH BRACES</h2>
<div class="sectionbody">
<div class="paragraph"><p>Curly braces may also be used for grouping arguments. They are similar
to quotes except for two differences. First, they nest; this makes them
easier to use for complicated arguments like nested Tcl command strings.
Second, the substitutions described below for commands, variables, and
backslashes do <strong>not</strong> occur in arguments enclosed in braces, so braces
can be used to prevent substitutions where they are undesirable.</p></div>
<div class="paragraph"><p>If an argument field begins with a left brace, then the argument ends
at the matching right brace. Tcl will strip off the outermost layer
of braces and pass the information between the braces to the command
without any further modification. For example, in the command</p></div>
<div class="literalblock">
<div class="content">
<pre><code>set a {xyz a {b c d}}</code></pre>
</div></div>
<div class="paragraph"><p>the <a href="#_set"><strong><code>set</code></strong></a> command will receive two arguments: <em>a</em>
and <em>xyz a {b c d}</em>.</p></div>
<div class="paragraph"><p>When braces or quotes are in effect, the matching brace or quote need
not be on the same line as the starting quote or brace; in this case
the newline will be included in the argument field along with any other
characters up to the matching brace or quote. For example, the <a href="#_eval"><strong><code>eval</code></strong></a>
command takes one argument, which is a command string; <a href="#_eval"><strong><code>eval</code></strong></a> invokes
the Tcl interpreter to execute the command string. The command</p></div>
<div class="literalblock">
<div class="content">
<pre><code>eval {
set a 22
set b 33
}</code></pre>
</div></div>
<div class="paragraph"><p>will assign the value <em>22</em> to <em>a</em> and <em>33</em> to <em>b</em>.</p></div>
<div class="paragraph"><p>If the first character of a command field is not a left
brace, then neither left nor right
braces in the field will be treated specially (except as part of
variable substitution; see below).</p></div>
</div>
</div>
<div class="sect1">
<h2 id="_command_substitution_with_brackets">COMMAND SUBSTITUTION WITH BRACKETS</h2>
<div class="sectionbody">
<div class="paragraph"><p>If an open bracket occurs in a field of a command, then command
substitution occurs (except for fields enclosed in braces). All of the
text up to the matching close bracket is treated as a Tcl command and
executed immediately. Then the result of that command is substituted
for the bracketed text. For example, consider the command</p></div>
<div class="literalblock">
<div class="content">
<pre><code>set a [set b]</code></pre>
</div></div>
<div class="paragraph"><p>When the <a href="#_set"><strong><code>set</code></strong></a> command has only a single argument, it is the name of a
variable and <a href="#_set"><strong><code>set</code></strong></a> returns the contents of that variable. In this case,
if variable <em>b</em> has the value <em>foo</em>, then the command above is equivalent
to the command</p></div>
<div class="literalblock">
<div class="content">
<pre><code>set a foo</code></pre>
</div></div>
<div class="paragraph"><p>Brackets can be used in more complex ways. For example, if the variable
<em>b</em> has the value <em>foo</em> and the variable <em>c</em> has the value <em>gorp</em>,
then the command</p></div>
<div class="literalblock">
<div class="content">
<pre><code>set a xyz[set b].[set c]</code></pre>
</div></div>
<div class="paragraph"><p>is equivalent to the command</p></div>
<div class="literalblock">
<div class="content">
<pre><code>set a xyzfoo.gorp</code></pre>
</div></div>
<div class="paragraph"><p>A bracketed command may contain multiple commands separated by newlines
or semi-colons in the usual fashion. In this case the value of the last
command is used for substitution. For example, the command</p></div>
<div class="literalblock">
<div class="content">
<pre><code>set a x[set b 22
expr $b+2]x</code></pre>
</div></div>
<div class="paragraph"><p>is equivalent to the command</p></div>
<div class="literalblock">
<div class="content">
<pre><code>set a x24x</code></pre>
</div></div>
<div class="paragraph"><p>If a field is enclosed in braces then the brackets and the characters
between them are not interpreted specially; they are passed through to
the argument verbatim.</p></div>
</div>
</div>
<div class="sect1">
<h2 id="_variable_substitution_with">VARIABLE SUBSTITUTION WITH $</h2>
<div class="sectionbody">
<div class="paragraph"><p>The dollar sign (<code>$</code>) may be used as a special shorthand form for
substituting variable values. If <code>$</code> appears in an argument that isn&#8217;t
enclosed in braces then variable substitution will occur. The characters
after the <code>$</code>, up to the first character that isn&#8217;t a number, letter,
or underscore, are taken as a variable name and the string value of that
variable is substituted for the name.</p></div>
<div class="paragraph"><p>For example, if variable <em>foo</em> has the value <em>test</em>, then the command</p></div>
<div class="literalblock">
<div class="content">
<pre><code>set a $foo.c</code></pre>
</div></div>
<div class="paragraph"><p>is equivalent to the command</p></div>
<div class="literalblock">
<div class="content">
<pre><code>set a test.c</code></pre>
</div></div>
<div class="paragraph"><p>There are two special forms for variable substitution. If the next
character after the name of the variable is an open parenthesis, then
the variable is assumed to be an array name, and all of the characters
between the open parenthesis and the next close parenthesis are taken as
an index into the array. Command substitutions and variable substitutions
are performed on the information between the parentheses before it is
used as an index.</p></div>
<div class="paragraph"><p>For example, if the variable <em>x</em> is an array with one element named
<em>first</em> and value <em>87</em> and another element named <em>14</em> and value <em>more</em>,
then the command</p></div>
<div class="literalblock">
<div class="content">
<pre><code>set a xyz$x(first)zyx</code></pre>
</div></div>
<div class="paragraph"><p>is equivalent to the command</p></div>
<div class="literalblock">
<div class="content">
<pre><code>set a xyz87zyx</code></pre>
</div></div>
<div class="paragraph"><p>If the variable <em>index</em> has the value <em>14</em>, then the command</p></div>
<div class="literalblock">
<div class="content">
<pre><code>set a xyz$x($index)zyx</code></pre>
</div></div>
<div class="paragraph"><p>is equivalent to the command</p></div>
<div class="literalblock">
<div class="content">
<pre><code>set a xyzmorezyx</code></pre>
</div></div>
<div class="paragraph"><p>For more information on arrays, see VARIABLES AND ARRAYS below.</p></div>
<div class="paragraph"><p>The second special form for variables occurs when the dollar sign is
followed by an open curly brace. In this case the variable name consists
of all the characters up to the next curly brace.</p></div>
<div class="paragraph"><p>Array references are not possible in this form: the name between braces
is assumed to refer to a scalar variable. For example, if variable
<em>foo</em> has the value <em>test</em>, then the command</p></div>
<div class="literalblock">
<div class="content">
<pre><code>set a abc${foo}bar</code></pre>
</div></div>
<div class="paragraph"><p>is equivalent to the command</p></div>
<div class="literalblock">
<div class="content">
<pre><code>set a abctestbar</code></pre>
</div></div>
<div class="paragraph"><p>Variable substitution does not occur in arguments that are enclosed in
braces: the dollar sign and variable name are passed through to the
argument verbatim.</p></div>
<div class="paragraph"><p>The dollar sign abbreviation is simply a shorthand form. <code>$a</code> is
completely equivalent to <code>[set a]</code>; it is provided as a convenience
to reduce typing.</p></div>
</div>
</div>
<div class="sect1">
<h2 id="_separating_commands_with_semi_colons">SEPARATING COMMANDS WITH SEMI-COLONS</h2>
<div class="sectionbody">
<div class="paragraph"><p>Normally, each command occupies one line (the command is terminated by a
newline character). However, semi-colon (<code>;</code>) is treated as a command
separator character; multiple commands may be placed on one line by
separating them with a semi-colon. Semi-colons are not treated as
command separators if they appear within curly braces or double-quotes.</p></div>
</div>
</div>
<div class="sect1">
<h2 id="_backslash_substitution">BACKSLASH SUBSTITUTION</h2>
<div class="sectionbody">
<div class="paragraph"><p>Backslashes may be used to insert non-printing characters into command
fields and also to insert special characters like braces and brackets
into fields without them being interpreted specially as described above.</p></div>
<div class="paragraph"><p>The backslash sequences understood by the Tcl interpreter are
listed below. In each case, the backslash
sequence is replaced by the given character:</p></div>
<div class="dlist" id="BackslashSequences"><dl>
<dt class="hdlist1">
<code>\b</code>
</dt>
<dd>
<p>
Backspace (0x8)
</p>
</dd>
<dt class="hdlist1">
<code>\f</code>
</dt>
<dd>
<p>
Form feed (0xc)
</p>
</dd>
<dt class="hdlist1">
<code>\n</code>
</dt>
<dd>
<p>
Newline (0xa)
</p>
</dd>
<dt class="hdlist1">
<code>\r</code>
</dt>
<dd>
<p>
Carriage-return (0xd).
</p>
</dd>
<dt class="hdlist1">
<code>\t</code>
</dt>
<dd>
<p>
Tab (0x9).
</p>
</dd>
<dt class="hdlist1">
<code>\v</code>
</dt>
<dd>
<p>
Vertical tab (0xb).
</p>
</dd>
<dt class="hdlist1">
<code>\{</code>
</dt>
<dd>
<p>
Left brace ({).
</p>
</dd>
<dt class="hdlist1">
<code>\}</code>
</dt>
<dd>
<p>
Right brace (}).
</p>
</dd>
<dt class="hdlist1">
<code>\[</code>
</dt>
<dd>
<p>
Open bracket ([).
</p>
</dd>
<dt class="hdlist1">
<code>\]</code>
</dt>
<dd>
<p>
Close bracket (]).
</p>
</dd>
<dt class="hdlist1">
<code>\$</code>
</dt>
<dd>
<p>
Dollar sign ($).
</p>
</dd>
<dt class="hdlist1">
<code>\&lt;space&gt;</code>
</dt>
<dd>
<p>
Space ( ): doesn&#8217;t terminate argument.
</p>
</dd>
<dt class="hdlist1">
<code>\;</code>
</dt>
<dd>
<p>
Semi-colon: doesn&#8217;t terminate command.
</p>
</dd>
<dt class="hdlist1">
<code>\"</code>
</dt>
<dd>
<p>
Double-quote.
</p>
</dd>
<dt class="hdlist1">
<code>\&lt;newline&gt;</code>
</dt>
<dd>
<p>
Nothing: this joins two lines together
into a single line. This backslash feature is unique in that
it will be applied even when the sequence occurs within braces.
</p>
</dd>
<dt class="hdlist1">
<code>\\</code>
</dt>
<dd>
<p>
Backslash (<em>\</em>).
</p>
</dd>
<dt class="hdlist1">
<code>\ddd</code>
</dt>
<dd>
<p>
The digits <code><em>ddd</em></code> (one, two, or three of them) give the octal value of
the character. Note that Jim supports null characters in strings.
</p>
</dd>
<dt class="hdlist1">
<code>\unnnn</code>
</dt>
<dt class="hdlist1">
<code>\u{nnn}</code>
</dt>
<dt class="hdlist1">
<code>\Unnnnnnnn</code>
</dt>
<dd>
<p>
The UTF-8 encoding of the unicode codepoint represented by the hex digits, <code><em>nnnn</em></code>, is inserted.
The <em>u</em> form allows for one to four hex digits.
The <em>U</em> form allows for one to eight hex digits.
The <em>u{nnn}</em> form allows for one to eight hex digits, but makes it easier to insert
characters UTF-8 characters which are followed by a hex digit.
</p>
</dd>
</dl></div>
<div class="paragraph"><p>For example, in the command</p></div>
<div class="literalblock">
<div class="content">
<pre><code>set a \{x\[\ yz\141</code></pre>
</div></div>
<div class="paragraph"><p>the second argument to <a href="#_set"><strong><code>set</code></strong></a> will be <code>{x[ yza</code>.</p></div>
<div class="paragraph"><p>If a backslash is followed by something other than one of the options
described above, then the backslash is transmitted to the argument
field without any special processing, and the Tcl scanner continues
normal processing with the next character. For example, in the
command</p></div>
<div class="literalblock">
<div class="content">
<pre><code>set \*a \\\{foo</code></pre>
</div></div>
<div class="paragraph"><p>The first argument to <a href="#_set"><strong><code>set</code></strong></a> will be <code>\*a</code> and the second
argument will be <code>\{foo</code>.</p></div>
<div class="paragraph"><p>If an argument is enclosed in braces, then backslash sequences inside
the argument are parsed but no substitution occurs (except for
backslash-newline): the backslash
sequence is passed through to the argument as is, without making
any special interpretation of the characters in the backslash sequence.
In particular, backslashed braces are not counted in locating the
matching right brace that terminates the argument.
For example, in the
command</p></div>
<div class="literalblock">
<div class="content">
<pre><code>set a {\{abc}</code></pre>
</div></div>
<div class="paragraph"><p>the second argument to <a href="#_set"><strong><code>set</code></strong></a> will be <code>\{abc</code>.</p></div>
<div class="paragraph"><p>This backslash mechanism is not sufficient to generate absolutely
any argument structure; it only covers the
most common cases. To produce particularly complicated arguments
it is probably easiest to use the <a href="#_format"><strong><code>format</code></strong></a> command along with
command substitution.</p></div>
</div>
</div>
<div class="sect1">
<h2 id="_string_and_list_index_specifications">STRING AND LIST INDEX SPECIFICATIONS</h2>
<div class="sectionbody">
<div class="paragraph"><p>Many string and list commands take one or more <em>index</em> parameters which
specify a position in the string relative to the start or end of the string/list.</p></div>
<div class="paragraph"><p>The index may be one of the following forms:</p></div>
<div class="dlist"><dl>
<dt class="hdlist1">
<code>integer</code>
</dt>
<dd>
<p>
A simple integer, where <em>0</em> refers to the first element of the string
or list.
</p>
</dd>
<dt class="hdlist1">
<code>integer+integer</code> or
</dt>
<dt class="hdlist1">
<code>integer-integer</code>
</dt>
<dd>
<p>
The sum or difference of the two integers. e.g. <code>2+3</code> refers to the 5th element.
This is useful when used with (e.g.) <code>$i+1</code> rather than the more verbose
<code>[expr {$i+1}]</code>
</p>
</dd>
<dt class="hdlist1">
<code>end</code>
</dt>
<dd>
<p>
The last element of the string or list.
</p>
</dd>
<dt class="hdlist1">
<code>end-integer</code>
</dt>
<dd>
<p>
The <em>nth-from-last</em> element of the string or list.
</p>
</dd>
</dl></div>
</div>
</div>
<div class="sect1">
<h2 id="_command_summary">COMMAND SUMMARY</h2>
<div class="sectionbody">
<div class="olist arabic"><ol class="arabic">
<li>
<p>
A command is just a string.
</p>
</li>
<li>
<p>
Within a string commands are separated by newlines or semi-colons
(unless the newline or semi-colon is within braces or brackets
or is backslashed).
</p>
</li>
<li>
<p>
A command consists of fields. The first field is the name of the command.
The other fields are strings that are passed to that command as arguments.
</p>
</li>
<li>
<p>
Fields are normally separated by white space.
</p>
</li>
<li>
<p>
Double-quotes allow white space and semi-colons to appear within
a single argument.
Command substitution, variable substitution, and backslash substitution
still occur inside quotes.
</p>
</li>
<li>
<p>
Braces defer interpretation of special characters.
If a field begins with a left brace, then it consists of everything
between the left brace and the matching right brace. The
braces themselves are not included in the argument.
No further processing is done on the information between the braces
except that backslash-newline sequences are eliminated.
</p>
</li>
<li>
<p>
If a field doesn&#8217;t begin with a brace then backslash,
variable, and command substitution are done on the field. Only a
single level of processing is done: the results of one substitution
are not scanned again for further substitutions or any other
special treatment. Substitution can
occur on any field of a command, including the command name
as well as the arguments.
</p>
</li>
<li>
<p>
If the first non-blank character of a command is a <code>#</code>, everything
from the <code>#</code> up through the next newline is treated as a comment
and ignored.
</p>
</li>
</ol></div>
</div>
</div>
<div class="sect1">
<h2 id="_expressions">EXPRESSIONS</h2>
<div class="sectionbody">
<div class="paragraph"><p>The second major interpretation applied to strings in Tcl is
as expressions. Several commands, such as <a href="#_expr"><strong><code>expr</code></strong></a>, <a href="#_for"><strong><code>for</code></strong></a>,
and <a href="#_if"><strong><code>if</code></strong></a>, treat one or more of their arguments as expressions
and call the Tcl expression processors (<em>Jim_ExprLong</em>,
<em>Jim_ExprBoolean</em>, etc.) to evaluate them.</p></div>
<div class="paragraph"><p>The operators permitted in Tcl expressions are a subset of
the operators permitted in C expressions, and they have the
same meaning and precedence as the corresponding C operators.
Expressions almost always yield numeric results
(integer or floating-point values).
For example, the expression</p></div>
<div class="literalblock">
<div class="content">
<pre><code>8.2 + 6</code></pre>
</div></div>
<div class="paragraph"><p>evaluates to 14.2.</p></div>
<div class="paragraph"><p>Tcl expressions differ from C expressions in the way that
operands are specified, and in that Tcl expressions support
non-numeric operands and string comparisons.</p></div>
<div class="paragraph"><p>A Tcl expression consists of a combination of operands, operators,
and parentheses.</p></div>
<div class="paragraph"><p>White space may be used between the operands and operators and
parentheses; it is ignored by the expression processor.
Where possible, operands are interpreted as integer values.</p></div>
<div class="paragraph"><p>Integer values may be specified in decimal (the normal case) or in
hexadecimal (if the first two characters of the operand are <em>0x</em>).
Note that Jim Tcl does <strong>not</strong> treat numbers with leading zeros as octal.</p></div>
<div class="paragraph"><p>If an operand does not have one of the integer formats given
above, then it is treated as a floating-point number if that is
possible. Floating-point numbers may be specified in any of the
ways accepted by an ANSI-compliant C compiler (except that the
<em>f</em>, <em>F</em>, <em>l</em>, and <em>L</em> suffixes will not be permitted in
most installations). For example, all of the
following are valid floating-point numbers: 2.1, 3., 6e4, 7.91e+16.</p></div>
<div class="paragraph"><p>If no numeric interpretation is possible, then an operand is left
as a string (and only a limited set of operators may be applied to
it).</p></div>
<div class="olist arabic"><ol class="arabic">
<li>
<p>
Operands may be specified in any of the following ways:
</p>
</li>
<li>
<p>
As a numeric value, either integer or floating-point.
</p>
</li>
<li>
<p>
As a Tcl variable, using standard <em>$</em> notation.
The variable&#8217;s value will be used as the operand.
</p>
</li>
<li>
<p>
As a string enclosed in double-quotes.
The expression parser will perform backslash, variable, and
command substitutions on the information between the quotes,
and use the resulting value as the operand
</p>
</li>
<li>
<p>
As a string enclosed in braces.
The characters between the open brace and matching close brace
will be used as the operand without any substitutions.
</p>
</li>
<li>
<p>
As a Tcl command enclosed in brackets.
The command will be executed and its result will be used as
the operand.
</p>
</li>
</ol></div>
<div class="paragraph"><p>Where substitutions occur above (e.g. inside quoted strings), they
are performed by the expression processor.
However, an additional layer of substitution may already have
been performed by the command parser before the expression
processor was called.</p></div>
<div class="paragraph"><p>As discussed below, it is usually best to enclose expressions
in braces to prevent the command parser from performing substitutions
on the contents.</p></div>
<div class="paragraph"><p>For some examples of simple expressions, suppose the variable <em>a</em> has
the value 3 and the variable <em>b</em> has the value 6. Then the expression
on the left side of each of the lines below will evaluate to the value
on the right side of the line:</p></div>
<div class="literalblock">
<div class="content">
<pre><code>$a + 3.1 6.1
2 + "$a.$b" 5.6
4*[llength "6 2"] 8
{word one} &lt; "word $a" 0</code></pre>
</div></div>
<div class="paragraph"><p>The valid operators are listed below, grouped in decreasing order
of precedence:</p></div>
<div class="dlist" id="OperatorPrecedence"><dl>
<dt class="hdlist1">
<code>int() double() round() abs(), rand(), srand()</code>
</dt>
<dd>
<p>
Unary functions (except rand() which takes no arguments)
</p>
<div class="ulist"><ul>
<li>
<p>
<code><em>int()</em></code> converts the numeric argument to an integer by truncating down.
</p>
</li>
<li>
<p>
<code><em>double()</em></code> converts the numeric argument to floating point.
</p>
</li>
<li>
<p>
<code><em>round()</em></code> converts the numeric argument to the closest integer value.
</p>
</li>
<li>
<p>
<code><em>abs()</em></code> takes the absolute value of the numeric argument.
</p>
</li>
<li>
<p>
<code><em>rand()</em></code> takes the absolute value of the numeric argument.
</p>
</li>
<li>
<p>
<code><em>rand()</em></code> returns a pseudo-random floating-point value in the range (0,1).
</p>
</li>
<li>
<p>
<code><em>srand()</em></code> takes an integer argument to (re)seed the random number generator. Returns the first random number from that seed.
</p>
</li>
</ul></div>
</dd>
<dt class="hdlist1">
<code>sin() cos() tan() asin() acos() atan() sinh() cosh() tanh() ceil() floor() exp() log() log10() sqrt()</code>
</dt>
<dd>
<p>
Unary math functions.
If Jim is compiled with math support, these functions are available.
</p>
</dd>
<dt class="hdlist1">
<code>- + ~ !</code>
</dt>
<dd>
<p>
Unary minus, unary plus, bit-wise NOT, logical NOT. None of these operands
may be applied to string operands, and bit-wise NOT may be
applied only to integers.
</p>
</dd>
<dt class="hdlist1">
<code>** pow(x,y)</code>
</dt>
<dd>
<p>
Power. e.g. <em>x<sup>y</sup></em>. If Jim is compiled with math support, supports doubles and
integers. Otherwise supports integers only. (Note that the math-function form
has the same highest precedence)
</p>
</dd>
<dt class="hdlist1">
<code>* / %</code>
</dt>
<dd>
<p>
Multiply, divide, remainder. None of these operands may be
applied to string operands, and remainder may be applied only
to integers.
</p>
</dd>
<dt class="hdlist1">
<code>+ -</code>
</dt>
<dd>
<p>
Add and subtract. Valid for any numeric operands.
</p>
</dd>
<dt class="hdlist1">
<code>&lt;&lt; &gt;&gt; &lt;&lt;&lt; &gt;&gt;&gt;</code>
</dt>
<dd>
<p>
Left and right shift, left and right rotate. Valid for integer operands only.
</p>
</dd>
<dt class="hdlist1">
<code>&lt; &gt; &lt;= &gt;=</code>
</dt>
<dd>
<p>
Boolean less, greater, less than or equal, and greater than or equal.
Each operator produces 1 if the condition is true, 0 otherwise.
These operators may be applied to strings as well as numeric operands,
in which case string comparison is used.
</p>
</dd>
<dt class="hdlist1">
<code>== !=</code>
</dt>
<dd>
<p>
Boolean equal and not equal. Each operator produces a zero/one result.
Valid for all operand types. <strong>Note</strong> that values will be converted to integers
if possible, then floating point types, and finally strings will be compared.
It is recommended that <em>eq</em> and <em>ne</em> should be used for string comparison.
</p>
</dd>
<dt class="hdlist1">
<code>eq ne</code>
</dt>
<dd>
<p>
String equal and not equal. Uses the string value directly without
attempting to convert to a number first.
</p>
</dd>
<dt class="hdlist1">
<code>in ni</code>
</dt>
<dd>
<p>
String in list and not in list. For <em>in</em>, result is 1 if the left operand (as a string)
is contained in the right operand (as a list), or 0 otherwise. The result for
<code>{$a ni $list}</code> is equivalent to <code>{!($a in $list)}</code>.
</p>
</dd>
<dt class="hdlist1">
<code>&amp;</code>
</dt>
<dd>
<p>
Bit-wise AND. Valid for integer operands only.
</p>
</dd>
<dt class="hdlist1">
<code>|</code>
</dt>
<dd>
<p>
Bit-wise OR. Valid for integer operands only.
</p>
</dd>
<dt class="hdlist1">
<code>^</code>
</dt>
<dd>
<p>
Bit-wise exclusive OR. Valid for integer operands only.
</p>
</dd>
<dt class="hdlist1">
<code>&amp;&amp;</code>
</dt>
<dd>
<p>
Logical AND. Produces a 1 result if both operands are non-zero, 0 otherwise.
Valid for numeric operands only (integers or floating-point).
</p>
</dd>
<dt class="hdlist1">
<code>||</code>
</dt>
<dd>
<p>
Logical OR. Produces a 0 result if both operands are zero, 1 otherwise.
Valid for numeric operands only (integers or floating-point).
</p>
</dd>
<dt class="hdlist1">
<code>x ? y : z</code>
</dt>
<dd>
<p>
If-then-else, as in C. If <code><em>x</em></code>
evaluates to non-zero, then the result is the value of <code><em>y</em></code>.
Otherwise the result is the value of <code><em>z</em></code>.
The <code><em>x</em></code> operand must have a numeric value, while <code><em>y</em></code> and <code><em>z</em></code> can
be of any type.
</p>
</dd>
</dl></div>
<div class="paragraph"><p>See the C manual for more details on the results
produced by each operator.
All of the binary operators group left-to-right within the same
precedence level. For example, the expression</p></div>
<div class="literalblock">
<div class="content">
<pre><code>4*2 &lt; 7</code></pre>
</div></div>
<div class="paragraph"><p>evaluates to 0.</p></div>
<div class="paragraph"><p>The <code>&amp;&amp;</code>, <code>||</code>, and <code>?:</code> operators have <em>lazy evaluation</em>, just as
in C, which means that operands are not evaluated if they are not
needed to determine the outcome. For example, in</p></div>
<div class="literalblock">
<div class="content">
<pre><code>$v ? [a] : [b]</code></pre>
</div></div>
<div class="paragraph"><p>only one of <code>[a]</code> or <code>[b]</code> will actually be evaluated,
depending on the value of <code>$v</code>.</p></div>
<div class="paragraph"><p>All internal computations involving integers are done with the C
type <em>long long</em> if available, or <em>long</em> otherwise, and all internal
computations involving floating-point are done with the C type
<em>double</em>.</p></div>
<div class="paragraph"><p>When converting a string to floating-point, exponent overflow is
detected and results in a Tcl error.
For conversion to integer from string, detection of overflow depends
on the behaviour of some routines in the local C library, so it should
be regarded as unreliable.
In any case, overflow and underflow are generally not detected
reliably for intermediate results.</p></div>
<div class="paragraph"><p>Conversion among internal representations for integer, floating-point,
and string operands is done automatically as needed.
For arithmetic computations, integers are used until some
floating-point number is introduced, after which floating-point is used.
For example,</p></div>
<div class="literalblock">
<div class="content">
<pre><code>5 / 4</code></pre>
</div></div>
<div class="paragraph"><p>yields the result 1, while</p></div>
<div class="literalblock">
<div class="content">
<pre><code>5 / 4.0
5 / ( [string length "abcd"] + 0.0 )</code></pre>
</div></div>
<div class="paragraph"><p>both yield the result 1.25.</p></div>
<div class="paragraph"><p>String values may be used as operands of the comparison operators,
although the expression evaluator tries to do comparisons as integer
or floating-point when it can.
If one of the operands of a comparison is a string and the other
has a numeric value, the numeric operand is converted back to
a string using the C <em>sprintf</em> format specifier
<em>%d</em> for integers and <em>%g</em> for floating-point values.
For example, the expressions</p></div>
<div class="literalblock">
<div class="content">
<pre><code>"0x03" &gt; "2"
"0y" &lt; "0x12"</code></pre>
</div></div>
<div class="paragraph"><p>both evaluate to 1. The first comparison is done using integer
comparison, and the second is done using string comparison after
the second operand is converted to the string <em>18</em>.</p></div>
<div class="paragraph"><p>In general it is safest to enclose an expression in braces when
entering it in a command: otherwise, if the expression contains
any white space then the Tcl interpreter will split it
among several arguments. For example, the command</p></div>
<div class="literalblock">
<div class="content">
<pre><code>expr $a + $b</code></pre>
</div></div>
<div class="paragraph"><p>results in three arguments being passed to <a href="#_expr"><strong><code>expr</code></strong></a>: <code>$a</code>,
+, and <code>$b</code>. In addition, if the expression isn&#8217;t in braces
then the Tcl interpreter will perform variable and command substitution
immediately (it will happen in the command parser rather than in
the expression parser). In many cases the expression is being
passed to a command that will evaluate the expression later (or
even many times if, for example, the expression is to be used to
decide when to exit a loop). Usually the desired goal is to re-do
the variable or command substitutions each time the expression is
evaluated, rather than once and for all at the beginning. For example,
the command</p></div>
<div class="literalblock">
<div class="content">
<pre><code>for {set i 1} $i&lt;=10 {incr i} {...} ** WRONG **</code></pre>
</div></div>
<div class="paragraph"><p>is probably intended to iterate over all values of <code>i</code> from 1 to 10.
After each iteration of the body of the loop, <a href="#_for"><strong><code>for</code></strong></a> will pass
its second argument to the expression evaluator to see whether or not
to continue processing. Unfortunately, in this case the value of <code>i</code>
in the second argument will be substituted once and for all when the
<a href="#_for"><strong><code>for</code></strong></a> command is parsed. If <code>i</code> was 0 before the <a href="#_for"><strong><code>for</code></strong></a>
command was invoked then the second argument of <a href="#_for"><strong><code>for</code></strong></a> will be <code>0&lt;=10</code>
which will always evaluate to 1, even though <code>i</code> eventually
becomes greater than 10. In the above case the loop will never
terminate. Instead, the expression should be placed in braces:</p></div>
<div class="literalblock">
<div class="content">
<pre><code>for {set i 1} {$i&lt;=10} {incr i} {...} ** RIGHT **</code></pre>
</div></div>
<div class="paragraph"><p>This causes the substitution of <em>i</em>
to be delayed; it will be re-done each time the expression is
evaluated, which is the desired result.</p></div>
</div>
</div>
<div class="sect1">
<h2 id="_lists">LISTS</h2>
<div class="sectionbody">
<div class="paragraph"><p>The third major way that strings are interpreted in Tcl is as lists.
A list is just a string with a list-like structure
consisting of fields separated by white space. For example, the
string</p></div>
<div class="literalblock">
<div class="content">
<pre><code>Al Sue Anne John</code></pre>
</div></div>
<div class="paragraph"><p>is a list with four elements or fields.
Lists have the same basic structure as command strings, except
that a newline character in a list is treated as a field separator
just like space or tab. Conventions for braces and quotes
and backslashes are the same for lists as for commands. For example,
the string</p></div>
<div class="literalblock">
<div class="content">
<pre><code>a b\ c {d e {f g h}}</code></pre>
</div></div>
<div class="paragraph"><p>is a list with three elements: <code>a</code>, <code>b c</code>, and <code>d e {f g h}</code>.</p></div>
<div class="paragraph"><p>Whenever an element is extracted from a list, the same rules about
braces and quotes and backslashes are applied as for commands. Thus in
the example above when the third element is extracted from the list,
the result is</p></div>
<div class="literalblock">
<div class="content">
<pre><code>d e {f g h}</code></pre>
</div></div>
<div class="paragraph"><p>(when the field was extracted, all that happened was to strip off
the outermost layer of braces). Command substitution and
variable substitution are never
made on a list (at least, not by the list-processing commands; the
list can always be passed to the Tcl interpreter for evaluation).</p></div>
<div class="paragraph"><p>The Tcl commands <a href="#_concat"><strong><code>concat</code></strong></a>, <a href="#_foreach"><strong><code>foreach</code></strong></a>, <a href="#_lappend"><strong><code>lappend</code></strong></a>, <a href="#_lindex"><strong><code>lindex</code></strong></a>, <a href="#_linsert"><strong><code>linsert</code></strong></a>,
<a href="#_list"><strong><code>list</code></strong></a>, <a href="#_llength"><strong><code>llength</code></strong></a>, <a href="#_lrange"><strong><code>lrange</code></strong></a>, <a href="#_lreplace"><strong><code>lreplace</code></strong></a>, <a href="#_lsearch"><strong><code>lsearch</code></strong></a>, and <a href="#_lsort"><strong><code>lsort</code></strong></a> allow
you to build lists, extract elements from them, search them, and perform
other list-related functions.</p></div>
<div class="paragraph"><p>Advanced list commands include <a href="#_lrepeat"><strong><code>lrepeat</code></strong></a>, <a href="#_lreverse"><strong><code>lreverse</code></strong></a>, <a href="#_lmap"><strong><code>lmap</code></strong></a>, <a href="#_lassign"><strong><code>lassign</code></strong></a>, <a href="#_lset"><strong><code>lset</code></strong></a>.</p></div>
</div>
</div>
<div class="sect1">
<h2 id="_list_expansion">LIST EXPANSION</h2>
<div class="sectionbody">
<div class="paragraph"><p>A new addition to Tcl 8.5 is the ability to expand a list into separate
arguments. Support for this feature is also available in Jim.</p></div>
<div class="paragraph"><p>Consider the following attempt to exec a list:</p></div>
<div class="literalblock">
<div class="content">
<pre><code>set cmd {ls -l}
exec $cmd</code></pre>
</div></div>
<div class="paragraph"><p>This will attempt to exec the a command named "ls -l", which will clearly not
work. Typically eval and concat are required to solve this problem, however
it can be solved much more easily with <code>{*}</code>.</p></div>
<div class="literalblock">
<div class="content">
<pre><code>exec {*}$cmd</code></pre>
</div></div>
<div class="paragraph"><p>This will expand the following argument into individual elements and then evaluate
the resulting command.</p></div>
<div class="paragraph"><p>Note that the official Tcl syntax is <code>{*}</code>, however <code>{expand}</code> is retained
for backward compatibility with experimental versions of this feature.</p></div>
</div>
</div>
<div class="sect1">
<h2 id="_regular_expressions">REGULAR EXPRESSIONS</h2>
<div class="sectionbody">
<div class="paragraph"><p>Tcl provides two commands that support string matching using regular
expressions, <a href="#_regexp"><strong><code>regexp</code></strong></a> and <a href="#_regsub"><strong><code>regsub</code></strong></a>, as well as <a href="#_switch"><strong><code>switch</code></strong></a> <code>-regexp</code> and
<a href="#_lsearch"><strong><code>lsearch</code></strong></a> <code>-regexp</code>.</p></div>
<div class="paragraph"><p>Regular expressions may be implemented one of two ways. Either using the system&#8217;s C library
POSIX regular expression support, or using the built-in regular expression engine.
The differences between these are described below.</p></div>
<div class="paragraph"><p><strong>NOTE</strong> Tcl 7.x and 8.x use perl-style Advanced Regular Expressions (<code>ARE</code>).</p></div>
<div class="sect2">
<h3 id="_posix_regular_expressions">POSIX Regular Expressions</h3>
<div class="paragraph"><p>If the system supports POSIX regular expressions, and UTF-8 support is not enabled,
this support will be used by default. The type of regular expressions supported are
Extended Regular Expressions (<code>ERE</code>) rather than Basic Regular Expressions (<code>BRE</code>).
See REG_EXTENDED in the documentation.</p></div>
<div class="paragraph"><p>Using the system-supported POSIX regular expressions will typically
make for the smallest code size, but some features such as UTF-8
and <code>\w</code>, <code>\d</code>, <code>\s</code> are not supported, and null characters
in strings are not supported.</p></div>
<div class="paragraph"><p>See regex(3) and regex(7) for full details.</p></div>
</div>
<div class="sect2">
<h3 id="_jim_built_in_regular_expressions">Jim built-in Regular Expressions</h3>
<div class="paragraph"><p>The Jim built-in regular expression engine may be selected with <code>./configure --with-jim-regexp</code>
or it will be selected automatically if UTF-8 support is enabled.</p></div>
<div class="paragraph"><p>This engine supports UTF-8 as well as some <code>ARE</code> features. The differences with both Tcl 7.x/8.x
and POSIX are highlighted below.</p></div>
<div class="olist arabic"><ol class="arabic">
<li>
<p>
UTF-8 strings and patterns are both supported
</p>
</li>
<li>
<p>
Supported character classes: <code>[:alnum:]</code>, <code>[:digit:]</code> and <code>[:space:]</code>
</p>
</li>
<li>
<p>
Supported shorthand character classes: <code>\w</code> = <code>[:alnum:]</code>, <code>\d</code> = <code>[:digit:],</code> <code>\s</code> = <code>[:space:]</code>
</p>
</li>
<li>
<p>
Character classes apply to ASCII characters only
</p>
</li>
<li>
<p>
Supported constraint escapes: <code>\m</code> = <code>\&lt;</code> = start of word, <code>\M</code> = <code>\&gt;</code> = end of word
</p>
</li>
<li>
<p>
Backslash escapes may be used within regular expressions, such as <code>\n</code> = newline, <code>\uNNNN</code> = unicode
</p>
</li>
<li>
<p>
Support for the <code>?</code> non-greedy quantifier. e.g. <code>*?</code>
</p>
</li>
<li>
<p>
Support for non-capuring parentheses <code>(?:&#8230;)</code>
</p>
</li>
<li>
<p>
Jim Tcl considers that both patterns and strings end at a null character (<code>\x00</code>)
</p>
</li>
</ol></div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_command_results">COMMAND RESULTS</h2>
<div class="sectionbody">
<div class="paragraph"><p>Each command produces two results: a code and a string. The
code indicates whether the command completed successfully or not,
and the string gives additional information. The valid codes are
defined in jim.h, and are:</p></div>
<div class="dlist"><dl>
<dt class="hdlist1">
<code>JIM_OK(0)</code>
</dt>
<dd>
<p>
This is the normal return code, and indicates that the command completed
successfully. The string gives the command&#8217;s return value.
</p>
</dd>
<dt class="hdlist1">
<code>JIM_ERR(1)</code>
</dt>
<dd>
<p>
Indicates that an error occurred; the string gives a message describing
the error.
</p>
</dd>
<dt class="hdlist1">
<code>JIM_RETURN(2)</code>
</dt>
<dd>
<p>
Indicates that the <a href="#_return"><strong><code>return</code></strong></a> command has been invoked, and that the
current procedure (or top-level command or <a href="#_source"><strong><code>source</code></strong></a> command)
should return immediately. The
string gives the return value for the procedure or command.
</p>
</dd>
<dt class="hdlist1">
<code>JIM_BREAK(3)</code>
</dt>
<dd>
<p>
Indicates that the <a href="#_break"><strong><code>break</code></strong></a> command has been invoked, so the
innermost loop should abort immediately. The string should always
be empty.
</p>
</dd>
<dt class="hdlist1">
<code>JIM_CONTINUE(4)</code>
</dt>
<dd>
<p>
Indicates that the <a href="#_continue"><strong><code>continue</code></strong></a> command has been invoked, so the
innermost loop should go on to the next iteration. The string
should always be empty.
</p>
</dd>
<dt class="hdlist1">
<code>JIM_SIGNAL(5)</code>
</dt>
<dd>
<p>
Indicates that a signal was caught while executing a commands.
The string contains the name of the signal caught.
See the <a href="#_signal"><strong><code>signal</code></strong></a> and <a href="#_catch"><strong><code>catch</code></strong></a> commands.
</p>
</dd>
<dt class="hdlist1">
<code>JIM_EXIT(6)</code>
</dt>
<dd>
<p>
Indicates that the command called the <a href="#_exit"><strong><code>exit</code></strong></a> command.
The string contains the exit code.
</p>
</dd>
</dl></div>
<div class="paragraph"><p>Tcl programmers do not normally need to think about return codes,
since <code>JIM_OK</code> is almost always returned. If anything else is returned
by a command, then the Tcl interpreter immediately stops processing
commands and returns to its caller. If there are several nested
invocations of the Tcl interpreter in progress, then each nested
command will usually return the error to its caller, until eventually
the error is reported to the top-level application code. The
application will then display the error message for the user.</p></div>
<div class="paragraph"><p>In a few cases, some commands will handle certain <a href="#_error"><strong><code>error</code></strong></a> conditions
themselves and not return them upwards. For example, the <a href="#_for"><strong><code>for</code></strong></a>
command checks for the <code>JIM_BREAK</code> code; if it occurs, then <a href="#_for"><strong><code>for</code></strong></a>
stops executing the body of the loop and returns <code>JIM_OK</code> to its
caller. The <a href="#_for"><strong><code>for</code></strong></a> command also handles <code>JIM_CONTINUE</code> codes and the
procedure interpreter handles <code>JIM_RETURN</code> codes. The <a href="#_catch"><strong><code>catch</code></strong></a>
command allows Tcl programs to catch errors and handle them without
aborting command interpretation any further.</p></div>
<div class="paragraph"><p>The <a href="#_info"><strong><code>info</code></strong></a> <code>returncodes</code> command may be used to programmatically map between
return codes and names.</p></div>
</div>
</div>
<div class="sect1">
<h2 id="_procedures">PROCEDURES</h2>
<div class="sectionbody">
<div class="paragraph"><p>Tcl allows you to extend the command interface by defining
procedures. A Tcl procedure can be invoked just like any other Tcl
command (it has a name and it receives one or more arguments).
The only difference is that its body isn&#8217;t a piece of C code linked
into the program; it is a string containing one or more other
Tcl commands.</p></div>
<div class="paragraph"><p>The <a href="#_proc"><strong><code>proc</code></strong></a> command is used to create a new Tcl command procedure:</p></div>
<div class="paragraph"><p><code><strong>proc</strong> <em>name arglist ?statics? body</em></code></p></div>
<div class="paragraph"><p>The new command is named <code><em>name</em></code>, and it replaces any existing command
there may have been by that name. Whenever the new command is
invoked, the contents of <code><em>body</em></code> will be executed by the Tcl
interpreter.</p></div>
<div class="paragraph"><p><code><em>arglist</em></code> specifies the formal arguments to the procedure.
It consists of a list, possibly empty, of the following
argument specifiers:</p></div>
<div class="dlist"><dl>
<dt class="hdlist1">
<code>name</code>
</dt>
<dd>
<p>
Required Argument - A simple argument name.
</p>
</dd>
<dt class="hdlist1">
<code>name default</code>
</dt>
<dd>
<p>
Optional Argument - A two-element list consisting of the
argument name, followed by the default value, which will
be used if the corresponding argument is not supplied.
</p>
</dd>
<dt class="hdlist1">
<code>&amp;name</code>
</dt>
<dd>
<p>
Reference Argument - The caller is expected to pass the name of
an existing variable. An implicit <a href="#_upvar"><strong><code>upvar</code></strong></a> <code>1 'origname' 'name'</code> is done
to make the variable available in the proc scope.
</p>
</dd>
<dt class="hdlist1">
<code><strong>args</strong></code>
</dt>
<dd>
<p>
Variable Argument - The special name <code><em>args</em></code>, which is
assigned all remaining arguments (including none) as a list. The
variable argument may only be specified once. Note that
the syntax <code>args newname</code> may be used to retain the special
behaviour of <code><em>args</em></code> with a different local name. In this case,
the variable is named <code><em>newname</em></code> rather than <code><em>args</em></code>.
</p>
</dd>
</dl></div>
<div class="paragraph"><p>When the command is invoked, a local variable will be created for each of
the formal arguments to the procedure; its value will be the value
of corresponding argument in the invoking command or the argument&#8217;s
default value.</p></div>
<div class="paragraph"><p>Arguments with default values need not be specified in a procedure
invocation. However, there must be enough actual arguments for all
required arguments, and there must not be any extra actual arguments
(unless the Variable Argument is specified).</p></div>
<div class="paragraph"><p>Actual arguments are assigned to formal arguments as in left-to-right
order with the following precedence.</p></div>
<div class="olist arabic"><ol class="arabic">
<li>
<p>
Required Arguments (including Reference Arguments)
</p>
</li>
<li>
<p>
Optional Arguments
</p>
</li>
<li>
<p>
Variable Argument
</p>
</li>
</ol></div>
<div class="paragraph"><p>The following example illustrates precedence. Assume a procedure declaration:</p></div>
<div class="literalblock">
<div class="content">
<pre><code>proc p {{a A} args b {c C} d} {...}</code></pre>
</div></div>
<div class="paragraph"><p>This procedure requires at least two arguments, but can accept an unlimited number.
The following table shows how various numbers of arguments are assigned.
Values marked as <code>-</code> are assigned the default value.</p></div>
<div class="tableblock">
<table rules="all"
width="40%"
frame="hsides"
cellspacing="0" cellpadding="4">
<col width="16%" />
<col width="16%" />
<col width="16%" />
<col width="16%" />
<col width="16%" />
<col width="16%" />
<thead>
<tr>
<th align="left" valign="top">Number of arguments</th>
<th align="left" valign="top">a</th>
<th align="left" valign="top">args</th>
<th align="left" valign="top">b</th>
<th align="left" valign="top">c</th>
<th align="left" valign="top">d</th>
</tr>
</thead>
<tbody>
<tr>
<td align="left" valign="top"><p class="table">2</p></td>
<td align="left" valign="top"><p class="table">-</p></td>
<td align="left" valign="top"><p class="table">-</p></td>
<td align="left" valign="top"><p class="table">1</p></td>
<td align="left" valign="top"><p class="table">-</p></td>
<td align="left" valign="top"><p class="table">2</p></td>
</tr>
<tr>
<td align="left" valign="top"><p class="table">3</p></td>
<td align="left" valign="top"><p class="table">1</p></td>
<td align="left" valign="top"><p class="table">-</p></td>
<td align="left" valign="top"><p class="table">2</p></td>
<td align="left" valign="top"><p class="table">-</p></td>
<td align="left" valign="top"><p class="table">3</p></td>
</tr>
<tr>
<td align="left" valign="top"><p class="table">4</p></td>
<td align="left" valign="top"><p class="table">1</p></td>
<td align="left" valign="top"><p class="table">-</p></td>
<td align="left" valign="top"><p class="table">2</p></td>
<td align="left" valign="top"><p class="table">3</p></td>
<td align="left" valign="top"><p class="table">4</p></td>
</tr>
<tr>
<td align="left" valign="top"><p class="table">5</p></td>
<td align="left" valign="top"><p class="table">1</p></td>
<td align="left" valign="top"><p class="table">2</p></td>
<td align="left" valign="top"><p class="table">3</p></td>
<td align="left" valign="top"><p class="table">4</p></td>
<td align="left" valign="top"><p class="table">5</p></td>
</tr>
<tr>
<td align="left" valign="top"><p class="table">6</p></td>
<td align="left" valign="top"><p class="table">1</p></td>
<td align="left" valign="top"><p class="table">2,3</p></td>
<td align="left" valign="top"><p class="table">4</p></td>
<td align="left" valign="top"><p class="table">5</p></td>
<td align="left" valign="top"><p class="table">6</p></td>
</tr>
</tbody>
</table>
</div>
<div class="paragraph"><p>When <code><em>body</em></code> is being executed, variable names normally refer to local
variables, which are created automatically when referenced and deleted
when the procedure returns. One local variable is automatically created
for each of the procedure&#8217;s arguments. Global variables can be
accessed by invoking the <a href="#_global"><strong><code>global</code></strong></a> command or via the <code>::</code> prefix.</p></div>
<div class="sect2">
<h3 id="_new_in_jim">New in Jim</h3>
<div class="paragraph"><p>In addition to procedure arguments, Jim procedures may declare static variables.
These variables scoped to the procedure and initialised at procedure definition.
Either from the static variable definition, or from the enclosing scope.</p></div>
<div class="paragraph"><p>Consider the following example:</p></div>
<div class="literalblock">
<div class="content">
<pre><code>jim&gt; set a 1
jim&gt; proc a {} {a {b 2}} {
set c 1
puts "$a $b $c"
incr a
incr b
incr c
}
jim&gt; a
1 2 1
jim&gt; a
2 3 1</code></pre>
</div></div>
<div class="paragraph"><p>The static variable <code><em>a</em></code> has no initialiser, so it is initialised from
the enclosing scope with the value 1. (Note that it is an error if there
is no variable with the same name in the enclosing scope). However <code><em>b</em></code>
has an initialiser, so it is initialised to 2.</p></div>
<div class="paragraph"><p>Unlike a local variable, the value of a static variable is retained across
invocations of the procedure.</p></div>
<div class="paragraph"><p>See the <a href="#_proc"><strong><code>proc</code></strong></a> command for information on how to define procedures
and what happens when they are invoked. See also NAMESPACES.</p></div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_variables_scalars_and_arrays">VARIABLES - SCALARS AND ARRAYS</h2>
<div class="sectionbody">
<div class="paragraph"><p>Tcl allows the definition of variables and the use of their values
either through <em>$</em>-style variable substitution, the <a href="#_set"><strong><code>set</code></strong></a>
command, or a few other mechanisms.</p></div>
<div class="paragraph"><p>Variables need not be declared: a new variable will automatically
be created each time a new variable name is used.</p></div>
<div class="paragraph"><p>Tcl supports two types of variables: scalars and arrays.
A scalar variable has a single value, whereas an array variable
can have any number of elements, each with a name (called
its <em>index</em>) and a value.</p></div>
<div class="paragraph"><p>Array indexes may be arbitrary strings; they need not be numeric.
Parentheses are used refer to array elements in Tcl commands.
For example, the command</p></div>
<div class="literalblock">
<div class="content">
<pre><code>set x(first) 44</code></pre>
</div></div>
<div class="paragraph"><p>will modify the element of <em>x</em> whose index is <em>first</em>
so that its new value is <em>44</em>.</p></div>
<div class="paragraph"><p>Two-dimensional arrays can be simulated in Tcl by using indexes
that contain multiple concatenated values.
For example, the commands</p></div>
<div class="literalblock">
<div class="content">
<pre><code>set a(2,3) 1
set a(3,6) 2</code></pre>
</div></div>
<div class="paragraph"><p>set the elements of <em>a</em> whose indexes are <em>2,3</em> and <em>3,6</em>.</p></div>
<div class="paragraph"><p>In general, array elements may be used anywhere in Tcl that scalar
variables may be used.</p></div>
<div class="paragraph"><p>If an array is defined with a particular name, then there may
not be a scalar variable with the same name.</p></div>
<div class="paragraph"><p>Similarly, if there is a scalar variable with a particular
name then it is not possible to make array references to the
variable.</p></div>
<div class="paragraph"><p>To convert a scalar variable to an array or vice versa, remove
the existing variable with the <a href="#_unset"><strong><code>unset</code></strong></a> command.</p></div>
<div class="paragraph"><p>The <a href="#_array"><strong><code>array</code></strong></a> command provides several features for dealing
with arrays, such as querying the names of all the elements of
the array and converting between an array and a list.</p></div>
<div class="paragraph"><p>Variables may be either global or local. If a variable
name is used when a procedure isn&#8217;t being executed, then it
automatically refers to a global variable. Variable names used
within a procedure normally refer to local variables associated with that
invocation of the procedure. Local variables are deleted whenever
a procedure exits. Either <a href="#_global"><strong><code>global</code></strong></a> command may be used to request
that a name refer to a global variable for the duration of the current
procedure (this is somewhat analogous to <em>extern</em> in C), or the variable
may be explicitly scoped with the <code>::</code> prefix. For example</p></div>
<div class="literalblock">
<div class="content">
<pre><code>set a 1
set b 2
proc p {} {
set c 3
global a</code></pre>
</div></div>
<div class="literalblock">
<div class="content">
<pre><code> puts "$a $::b $c"
}
p</code></pre>
</div></div>
<div class="paragraph"><p>will output:</p></div>
<div class="literalblock">
<div class="content">
<pre><code>1 2 3</code></pre>
</div></div>
</div>
</div>
<div class="sect1">
<h2 id="_arrays_as_lists_in_jim">ARRAYS AS LISTS IN JIM</h2>
<div class="sectionbody">
<div class="paragraph"><p>Unlike Tcl, Jim can automatically convert between a list (with an even
number of elements) and an array value. This is similar to the way Tcl
can convert between a string and a list.</p></div>
<div class="paragraph"><p>For example:</p></div>
<div class="literalblock">
<div class="content">
<pre><code>set a {1 one 2 two}
puts $a(2)</code></pre>
</div></div>
<div class="paragraph"><p>will output:</p></div>
<div class="literalblock">
<div class="content">
<pre><code>two</code></pre>
</div></div>
<div class="paragraph"><p>Thus <a href="#_array"><strong><code>array</code></strong></a> <code>set</code> is equivalent to <a href="#_set"><strong><code>set</code></strong></a> when the variable does not
exist or is empty.</p></div>
<div class="paragraph"><p>The reverse is also true where an array will be converted into
a list.</p></div>
<div class="literalblock">
<div class="content">
<pre><code>set a(1) one; set a(2) two
puts $a</code></pre>
</div></div>
<div class="paragraph"><p>will output:</p></div>
<div class="literalblock">
<div class="content">
<pre><code>1 one 2 two</code></pre>
</div></div>
</div>
</div>
<div class="sect1">
<h2 id="_dictionary_values">DICTIONARY VALUES</h2>
<div class="sectionbody">
<div class="paragraph"><p>Tcl 8.5 introduced the dict command, and Jim Tcl has added a version
of this command. Dictionaries provide efficient access to key-value
pairs, just like arrays, but dictionaries are pure values. This
means that you can pass them to a procedure just as a list or a
string. Tcl dictionaries are therefore much more like Tcl lists,
except that they represent a mapping from keys to values, rather
than an ordered sequence.</p></div>
<div class="paragraph"><p>You can nest dictionaries, so that the value for a particular key
consists of another dictionary. That way you can elegantly build
complicated data structures, such as hierarchical databases. You
can also combine dictionaries with other Tcl data structures. For
instance, you can build a list of dictionaries that themselves
contain lists.</p></div>
<div class="paragraph"><p>Dictionaries are values that contain an efficient, order-preserving
mapping from arbitrary keys to arbitrary values. Each key in the
dictionary maps to a single value. They have a textual format that
is exactly that of any list with an even number of elements, with
each mapping in the dictionary being represented as two items in
the list. When a command takes a dictionary and produces a new
dictionary based on it (either returning it or writing it back into
the variable that the starting dictionary was read from) the new
dictionary will have the same order of keys, modulo any deleted
keys and with new keys added on to the end. When a string is
interpreted as a dictionary and it would otherwise have duplicate
keys, only the last value for a particular key is used; the others
are ignored, meaning that, "apple banana" and "apple carrot apple
banana" are equivalent dictionaries (with different string
representations).</p></div>
<div class="paragraph"><p>Note that in Jim, arrays are implemented as dictionaries.
Thus automatic conversion between lists and dictionaries applies
as it does for arrays.</p></div>
<div class="literalblock">
<div class="content">
<pre><code>jim&gt; dict set a 1 one
1 one
jim&gt; dict set a 2 two
1 one 2 two
jim&gt; puts $a
1 one 2 two
jim&gt; puts $a(2)
two
jim&gt; dict set a 3 T three
1 one 2 two 3 {T three}</code></pre>
</div></div>
<div class="paragraph"><p>See the <a href="#_dict"><strong><code>dict</code></strong></a> command for more details.</p></div>
</div>
</div>
<div class="sect1">
<h2 id="_namespaces">NAMESPACES</h2>
<div class="sectionbody">
<div class="paragraph"><p>Tcl added namespaces as a mechanism avoiding name clashes, especially in applications
including a number of 3rd party components. While there is less need for namespaces
in Jim Tcl (which does not strive to support large applications), it is convenient to
provide a subset of the support for namespaces to easy porting code from Tcl.</p></div>
<div class="paragraph"><p>Jim Tcl currently supports "light-weight" namespaces which should be adequate for most
purposes. This feature is currently experimental. See README.namespaces for more information
and the documentation of the <a href="#_namespace"><strong><code>namespace</code></strong></a> command.</p></div>
</div>
</div>
<div class="sect1">
<h2 id="_garbage_collection_references_lambda">GARBAGE COLLECTION, REFERENCES, LAMBDA</h2>
<div class="sectionbody">
<div class="paragraph"><p>Unlike Tcl, Jim has some sophisticated support for functional programming.
These are described briefly below.</p></div>
<div class="paragraph"><p>More information may be found at <a href="http://wiki.tcl.tk/13847">http://wiki.tcl.tk/13847</a></p></div>
<div class="sect2">
<h3 id="_references">References</h3>
<div class="paragraph"><p>A reference can be thought of as holding a value with one level of indirection,
where the value may be garbage collected when unreferenced.
Consider the following example:</p></div>
<div class="literalblock">
<div class="content">
<pre><code>jim&gt; set r [ref "One String" test]
&lt;reference.&lt;test___&gt;.00000000000000000000&gt;
jim&gt; getref $r
One String</code></pre>
</div></div>
<div class="paragraph"><p>The operation <a href="#_ref"><strong><code>ref</code></strong></a> creates a references to the value specified by the
first argument. (The second argument is a "type" used for documentation purposes).</p></div>
<div class="paragraph"><p>The operation <a href="#_getref"><strong><code>getref</code></strong></a> is the dereferencing operation which retrieves the value
stored in the reference.</p></div>
<div class="literalblock">
<div class="content">
<pre><code>jim&gt; setref $r "New String"
New String
jim&gt; getref $r
New String</code></pre>
</div></div>
<div class="paragraph"><p>The operation <a href="#_setref"><strong><code>setref</code></strong></a> replaces the value stored by the reference. If the old value
is no longer accessible by any reference, it will eventually be automatically be garbage
collected.</p></div>
</div>
<div class="sect2">
<h3 id="_garbage_collection">Garbage Collection</h3>
<div class="paragraph"><p>Normally, all values in Tcl are passed by value. As such values are copied and released
automatically as necessary.</p></div>
<div class="paragraph"><p>With the introduction of references, it is possible to create values whose lifetime
transcend their scope. To support this, case, the Jim system will periodically identify
and discard objects which are no longer accessible by any reference.</p></div>
<div class="paragraph"><p>The <a href="#_collect"><strong><code>collect</code></strong></a> command may be used to force garbage collection. Consider a reference created
with a finalizer:</p></div>
<div class="literalblock">
<div class="content">
<pre><code>jim&gt; proc f {ref value} { puts "Finaliser called for $ref,$value" }
jim&gt; set r [ref "One String" test f]
&lt;reference.&lt;test___&gt;.00000000000
jim&gt; collect
0
jim&gt; set r ""
jim&gt; collect
Finaliser called for &lt;reference.&lt;test___&gt;.00000000000,One String
1</code></pre>
</div></div>
<div class="paragraph"><p>Note that once the reference, <em>r</em>, was modified so that it no longer
contained a reference to the value, the garbage collector discarded
the value (after calling the finalizer).</p></div>
<div class="paragraph"><p>The finalizer for a reference may be examined or changed with the <a href="#_finalize"><strong><code>finalize</code></strong></a> command</p></div>
<div class="literalblock">
<div class="content">
<pre><code>jim&gt; finalize $r
f
jim&gt; finalize $r newf
newf</code></pre>
</div></div>
</div>
<div class="sect2">
<h3 id="_lambda">Lambda</h3>
<div class="paragraph"><p>Jim provides a garbage collected lambda function. This is a procedure
which is able to create an anonymous procedure. Consider:</p></div>
<div class="literalblock">
<div class="content">
<pre><code>jim&gt; set f [lambda {a} {{x 0}} { incr x $a }]
jim&gt; $f 1
1
jim&gt; $f 2
3
jim&gt; set f ""</code></pre>
</div></div>
<div class="paragraph"><p>This create an anonymous procedure (with the name stored in <em>f</em>), with a static variable
which is incremented by the supplied value and the result returned.</p></div>
<div class="paragraph"><p>Once the procedure name is no longer accessible, it will automatically be deleted
when the garbage collector runs.</p></div>
<div class="paragraph"><p>The procedure may also be delete immediately by renaming it "". e.g.</p></div>
<div class="literalblock">
<div class="content">
<pre><code>jim&gt; rename $f ""</code></pre>
</div></div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_utf_8_and_unicode">UTF-8 AND UNICODE</h2>
<div class="sectionbody">
<div class="paragraph"><p>If Jim is built with UTF-8 support enabled (configure --enable-utf),
then most string-related commands become UTF-8 aware. These include,
but are not limited to, <a href="#_string"><strong><code>string</code></strong></a> <code>match</code>, <a href="#_split"><strong><code>split</code></strong></a>, <a href="#_glob"><strong><code>glob</code></strong></a>, <a href="#_scan"><strong><code>scan</code></strong></a> and
<a href="#_format"><strong><code>format</code></strong></a>.</p></div>
<div class="paragraph"><p>UTF-8 encoding has many advantages, but one of the complications is that
characters can take a variable number of bytes. Thus the addition of
<a href="#_string"><strong><code>string</code></strong></a> <code>bytelength</code> which returns the number of bytes in a string,
while <a href="#_string"><strong><code>string</code></strong></a> <code>length</code> returns the number of characters.</p></div>
<div class="paragraph"><p>If UTF-8 support is not enabled, all commands treat bytes as characters
and <a href="#_string"><strong><code>string</code></strong></a> <code>bytelength</code> returns the same value as <a href="#_string"><strong><code>string</code></strong></a> <code>length</code>.</p></div>
<div class="paragraph"><p>Note that even if UTF-8 support is not enabled, the <code>\uNNNN</code> and related syntax
is still available to embed UTF-8 sequences.</p></div>
<div class="paragraph"><p>Jim Tcl supports all currently defined unicode codepoints. That is 21 bits, up to +<em>U+1FFFFF</em>.</p></div>
<div class="sect2">
<h3 id="_string_matching">String Matching</h3>
<div class="paragraph"><p>Commands such as <a href="#_string"><strong><code>string</code></strong></a> <code>match</code>, <a href="#_lsearch"><strong><code>lsearch</code></strong></a> <code>-glob</code>, <a href="#_array"><strong><code>array</code></strong></a> <code>names</code> and others use string
pattern matching rules. These commands support UTF-8. For example:</p></div>
<div class="literalblock">
<div class="content">
<pre><code>string match a\[\ua0-\ubf\]b "a\u00a3b"</code></pre>
</div></div>
</div>
<div class="sect2">
<h3 id="_format_and_scan">format and scan</h3>
<div class="paragraph"><p><code>format %c</code> allows a unicode codepoint to be be encoded. For example, the following will return
a string with two bytes and one character. The same as <code>\ub5</code></p></div>
<div class="literalblock">
<div class="content">
<pre><code>format %c 0xb5</code></pre>
</div></div>
<div class="paragraph"><p><a href="#_format"><strong><code>format</code></strong></a> respects widths as character widths, not byte widths. For example, the following will
return a string with three characters, not three bytes.</p></div>
<div class="literalblock">
<div class="content">
<pre><code>format %.3s \ub5\ub6\ub7\ub8</code></pre>
</div></div>
<div class="paragraph"><p>Similarly, <code>scan &#8230; %c</code> allows a UTF-8 to be decoded to a unicode codepoint. The following will set
<code><em>a</em></code> to 181 (0xb5) and <code><em>b</em></code> to 65 (0x41).</p></div>
<div class="literalblock">
<div class="content">
<pre><code>scan \u00b5A %c%c a b</code></pre>
</div></div>
<div class="paragraph"><p><a href="#_scan"><strong><code>scan</code></strong></a> <code>%s</code> will also accept a character class, including unicode ranges.</p></div>
</div>
<div class="sect2">
<h3 id="_string_classes">String Classes</h3>
<div class="paragraph"><p><a href="#_string"><strong><code>string</code></strong></a> <code>is</code> has <strong>not</strong> been extended to classify UTF-8 characters. Therefore, the following
will return 0, even though the string may be considered to be alphabetic.</p></div>
<div class="literalblock">
<div class="content">
<pre><code>string is alpha \ub5Test</code></pre>
</div></div>
<div class="paragraph"><p>This does not affect the string classes <em>ascii</em>, <em>control</em>, <em>digit</em>, <em>double</em>, <em>integer</em> or <em>xdigit</em>.</p></div>
</div>
<div class="sect2">
<h3 id="_case_mapping_and_conversion">Case Mapping and Conversion</h3>
<div class="paragraph"><p>Jim provides a simplified unicode case mapping. This means that case conversion
and comparison will not increase or decrease the number of characters in a string.
(Although it may change the number of bytes).</p></div>
<div class="paragraph"><p><a href="#_string"><strong><code>string</code></strong></a> <code>toupper</code> will convert any lowercase letters to their uppercase equivalent.
Any character which is not a letter or has no uppercase equivalent is left unchanged.
Similarly for <a href="#_string"><strong><code>string</code></strong></a> <code>tolower</code> and <a href="#_string"><strong><code>string</code></strong></a> <code>totitle</code>.</p></div>
<div class="paragraph"><p>Commands which perform case insensitive matches, such as <a href="#_string"><strong><code>string</code></strong></a> <code>compare -nocase</code>
and <a href="#_lsearch"><strong><code>lsearch</code></strong></a> <code>-nocase</code> fold both strings to uppercase before comparison.</p></div>
</div>
<div class="sect2">
<h3 id="_invalid_utf_8_sequences">Invalid UTF-8 Sequences</h3>
<div class="paragraph"><p>Some UTF-8 character sequences are invalid, such as those beginning with <em>0xff</em>,
those which represent character sequences longer than 3 bytes (greater than U+FFFF),
and those which end prematurely, such as a lone <em>0xc2</em>.</p></div>
<div class="paragraph"><p>In these situations, the offending bytes are treated as single characters. For example,
the following returns 2.</p></div>
<div class="literalblock">
<div class="content">
<pre><code>string bytelength \xff\xff</code></pre>
</div></div>
</div>
<div class="sect2">
<h3 id="_regular_expressions_2">Regular Expressions</h3>
<div class="paragraph"><p>If UTF-8 support is enabled, the built-in regular expression engine will be
selected which supports UTF-8 strings and patterns.</p></div>
<div class="paragraph"><p>See REGULAR EXPRESSIONS</p></div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_built_in_commands">BUILT-IN COMMANDS</h2>
<div class="sectionbody">
<div class="paragraph"><p>The Tcl library provides the following built-in commands, which will
be available in any application using Tcl. In addition to these
built-in commands, there may be additional commands defined by each
application, plus commands defined as Tcl procedures.</p></div>
<div class="paragraph"><p>In the command syntax descriptions below, words in <code><strong>boldface</strong></code> are
literals that you type verbatim to Tcl.</p></div>
<div class="paragraph"><p>Words in <code><em>italics</em></code> are meta-symbols; they serve as names for any of
a range of values that you can type.</p></div>
<div class="paragraph"><p>Optional arguments or groups of arguments are indicated by enclosing them
in <code>?question-marks?</code>.</p></div>
<div class="paragraph"><p>Ellipses (<code>...</code>) indicate that any number of additional
arguments or groups of arguments may appear, in the same format
as the preceding argument(s).</p></div>
<div class="sect2">
<h3 id="CommandIndex">Command Index</h3>
<div class="tableblock">
<table rules="none"
width="100%"
frame="void"
cellspacing="0" cellpadding="4">
<col width="12%" />
<col width="12%" />
<col width="12%" />
<col width="12%" />
<col width="12%" />
<col width="12%" />
<col width="12%" />
<col width="12%" />
<tbody>
<tr>
<td align="left" valign="top"><p class="table"><a href="#cmd_2"><strong><code>after</code></strong></a></p></td>
<td align="left" valign="top"><p class="table"><a href="#_aio"><strong><code>aio</code></strong></a></p></td>
<td align="left" valign="top"><p class="table"><a href="#_alarm"><strong><code>alarm</code></strong></a></p></td>
<td align="left" valign="top"><p class="table"><a href="#_alias"><strong><code>alias</code></strong></a></p></td>
<td align="left" valign="top"><p class="table"><a href="#_append"><strong><code>append</code></strong></a></p></td>
<td align="left" valign="top"><p class="table"><a href="#_apply"><strong><code>apply</code></strong></a></p></td>
<td align="left" valign="top"><p class="table"><a href="#_array"><strong><code>array</code></strong></a></p></td>
<td align="left" valign="top"><p class="table"><a href="#_binary"><strong><code>binary</code></strong></a></p></td>
</tr>
<tr>
<td align="left" valign="top"><p class="table"><a href="#_break"><strong><code>break</code></strong></a></p></td>
<td align="left" valign="top"><p class="table"><a href="#_case"><strong><code>case</code></strong></a></p></td>
<td align="left" valign="top"><p class="table"><a href="#_catch"><strong><code>catch</code></strong></a></p></td>
<td align="left" valign="top"><p class="table"><a href="#_cd"><strong><code>cd</code></strong></a></p></td>
<td align="left" valign="top"><p class="table"><a href="#cmd_4"><strong><code>class</code></strong></a></p></td>
<td align="left" valign="top"><p class="table"><a href="#_clock"><strong><code>clock</code></strong></a></p></td>
<td align="left" valign="top"><p class="table"><a href="#_close"><strong><code>close</code></strong></a></p></td>
<td align="left" valign="top"><p class="table"><a href="#_collect"><strong><code>collect</code></strong></a></p></td>
</tr>
<tr>
<td align="left" valign="top"><p class="table"><a href="#_concat"><strong><code>concat</code></strong></a></p></td>
<td align="left" valign="top"><p class="table"><a href="#_continue"><strong><code>continue</code></strong></a></p></td>
<td align="left" valign="top"><p class="table"><a href="#_curry"><strong><code>curry</code></strong></a></p></td>
<td align="left" valign="top"><p class="table"><a href="#_dict"><strong><code>dict</code></strong></a></p></td>
<td align="left" valign="top"><p class="table"><a href="#_env"><strong><code>env</code></strong></a></p></td>
<td align="left" valign="top"><p class="table"><a href="#_eof"><strong><code>eof</code></strong></a></p></td>
<td align="left" valign="top"><p class="table"><a href="#_error"><strong><code>error</code></strong></a></p></td>
<td align="left" valign="top"><p class="table"><a href="#_eval"><strong><code>eval</code></strong></a></p></td>
</tr>
<tr>
<td align="left" valign="top"><p class="table"><a href="#cmd_2"><strong><code>eventloop</code></strong></a></p></td>
<td align="left" valign="top"><p class="table"><a href="#_exec"><strong><code>exec</code></strong></a></p></td>
<td align="left" valign="top"><p class="table"><a href="#_exists"><strong><code>exists</code></strong></a></p></td>
<td align="left" valign="top"><p class="table"><a href="#_exit"><strong><code>exit</code></strong></a></p></td>
<td align="left" valign="top"><p class="table"><a href="#_expr"><strong><code>expr</code></strong></a></p></td>
<td align="left" valign="top"><p class="table"><a href="#_fconfigure"><strong><code>fconfigure</code></strong></a></p></td>
<td align="left" valign="top"><p class="table"><a href="#_file"><strong><code>file</code></strong></a></p></td>
<td align="left" valign="top"><p class="table"><a href="#_finalize"><strong><code>finalize</code></strong></a></p></td>
</tr>
<tr>
<td align="left" valign="top"><p class="table"><a href="#_flush"><strong><code>flush</code></strong></a></p></td>
<td align="left" valign="top"><p class="table"><a href="#_for"><strong><code>for</code></strong></a></p></td>
<td align="left" valign="top"><p class="table"><a href="#_foreach"><strong><code>foreach</code></strong></a></p></td>
<td align="left" valign="top"><p class="table"><a href="#_format"><strong><code>format</code></strong></a></p></td>
<td align="left" valign="top"><p class="table"><a href="#_getref"><strong><code>getref</code></strong></a></p></td>
<td align="left" valign="top"><p class="table"><a href="#_gets"><strong><code>gets</code></strong></a></p></td>
<td align="left" valign="top"><p class="table"><a href="#_glob"><strong><code>glob</code></strong></a></p></td>
<td align="left" valign="top"><p class="table"><a href="#_global"><strong><code>global</code></strong></a></p></td>
</tr>
<tr>
<td align="left" valign="top"><p class="table"><a href="#_history"><strong><code>history</code></strong></a></p></td>
<td align="left" valign="top"><p class="table"><a href="#_if"><strong><code>if</code></strong></a></p></td>
<td align="left" valign="top"><p class="table"><a href="#_incr"><strong><code>incr</code></strong></a></p></td>
<td align="left" valign="top"><p class="table"><a href="#_info"><strong><code>info</code></strong></a></p></td>
<td align="left" valign="top"><p class="table"><a href="#_join"><strong><code>join</code></strong></a></p></td>
<td align="left" valign="top"><p class="table"><a href="#_kill"><strong><code>kill</code></strong></a></p></td>
<td align="left" valign="top"><p class="table"><a href="#_lambda"><strong><code>lambda</code></strong></a></p></td>
<td align="left" valign="top"><p class="table"><a href="#_lappend"><strong><code>lappend</code></strong></a></p></td>
</tr>
<tr>
<td align="left" valign="top"><p class="table"><a href="#_lassign"><strong><code>lassign</code></strong></a></p></td>
<td align="left" valign="top"><p class="table"><a href="#_lindex"><strong><code>lindex</code></strong></a></p></td>
<td align="left" valign="top"><p class="table"><a href="#_linsert"><strong><code>linsert</code></strong></a></p></td>
<td align="left" valign="top"><p class="table"><a href="#_list"><strong><code>list</code></strong></a></p></td>
<td align="left" valign="top"><p class="table"><a href="#_llength"><strong><code>llength</code></strong></a></p></td>
<td align="left" valign="top"><p class="table"><a href="#_lmap"><strong><code>lmap</code></strong></a></p></td>
<td align="left" valign="top"><p class="table"><a href="#_load"><strong><code>load</code></strong></a></p></td>
<td align="left" valign="top"><p class="table"><a href="#_local"><strong><code>local</code></strong></a></p></td>
</tr>
<tr>
<td align="left" valign="top"><p class="table"><a href="#_loop"><strong><code>loop</code></strong></a></p></td>
<td align="left" valign="top"><p class="table"><a href="#_lrange"><strong><code>lrange</code></strong></a></p></td>
<td align="left" valign="top"><p class="table"><a href="#_lrepeat"><strong><code>lrepeat</code></strong></a></p></td>
<td align="left" valign="top"><p class="table"><a href="#_lreplace"><strong><code>lreplace</code></strong></a></p></td>
<td align="left" valign="top"><p class="table"><a href="#_lreverse"><strong><code>lreverse</code></strong></a></p></td>
<td align="left" valign="top"><p class="table"><a href="#_lsearch"><strong><code>lsearch</code></strong></a></p></td>
<td align="left" valign="top"><p class="table"><a href="#_lset"><strong><code>lset</code></strong></a></p></td>
<td align="left" valign="top"><p class="table"><a href="#_lsort"><strong><code>lsort</code></strong></a></p></td>
</tr>
<tr>
<td align="left" valign="top"><p class="table"><a href="#_namespace"><strong><code>namespace</code></strong></a></p></td>
<td align="left" valign="top"><p class="table"><a href="#cmd_4"><strong><code>oo</code></strong></a></p></td>
<td align="left" valign="top"><p class="table"><a href="#_open"><strong><code>open</code></strong></a></p></td>
<td align="left" valign="top"><p class="table"><a href="#cmd_1"><strong><code>os.fork</code></strong></a></p></td>
<td align="left" valign="top"><p class="table"><a href="#cmd_1"><strong><code>os.gethostname</