Trying isolated test cases interactively, and pasting the command into the editor when satisfied, can greatly reduce debugging time there is no need to restart the application after every little change - just make sure it's the right one, before restarting. Interactive tclsh tries to guess what we mean, and "hi" is the unambiguous prefix of the "history" command, whose results we see here. Another command worth remembering is "info":. Another helpful error: "option" is not an option, but the valid ones are listed. To get information about commands, it makes sense to type the following:.
What in Tcl is called error is in fact more like an exception in other languages - you can deliberately raise an error, and also catch errors. One reason for errors can be an undefined command name. One can use this playfully, together with catch , as in the following example of a multi-loop break , that terminates the two nested loops when a matrix element is empty:.
The throw command does not exist in normal Tcl, so it throws an error, which is caught by the catch around the outer loop. This global variable provided by Tcl contains the last error message and the traceback of the last error. Silly example:.
Get latest updates about Open Source Projects, Conferences and News.
For a quick overview how some procedures are called, and when, and what do they return, and when, the trace execution is a valuable tool. Let's take the following factorial function as example:. We need to supply a handler that will be called with different numbers of arguments two on enter, four on leave. Here's a very simple one:. So we can see how recursion goes down to 1, then returns in backward order, stepwise building up the final result. To find out how exactly a proc works and what goes wrong where , you can also register commands to be called before and after a command inside a procedure is called going down transitively to all called procs.
You can use the following step and interact procedures for this:. The simplest way to inspect why something goes wrong is inserting a puts command before the place where it happens. Say if you want to see the values of variables x and y, just insert.
The output will go to stdout - the console from where you started the script. On Windows or Mac, you might need to add the command. If at some time you want to see details of what your program does, and at others not, you can define and redefine a dputs command that either calls puts or does nothing:. For more debugging comfort, add the proc interact from above to your code, and put a call to interact before the place where the error happens. Some useful things to do at such a debugging prompt:. Checking data for certain conditions is a frequent operation in coding. Absolutely intolerable conditions can just throw an error:.
If you don't need hand-crafted error messages, you can factor such checks out to an assert command:. Note that the condition is reverted - as "assert" means roughly "take for granted", the positive case is specified, and the error is raised if it is not satisfied. Tests for internal conditions that do not depend on external data can be used during development, and when the coder is sure they are bullet-proof to always succeed, s he can turn them off centrally in one place by defining. This way, assertions are compiled to no bytecode at all, and can remain in the source code as a kind of documentation.
If assertions are tested, it only happens at the position where they stand in the code. Using a trace, it is also possible to specify a condition once, and have it tested whenever a variable's value changes:. The "; " at the end of the trace causes the additional arguments name element op, that are appended to the command prefix when a trace fires, to be ignored as a comment.
Better ideas welcome. In any case, these few lines of code give us a kind of bounds checking - the size of Tcl's data structures is in principle only bounded by the available virtual memory, but runaway loops may be harder to debug, compared to a few assertt calls for suspicious variables:. Bugs happen.
Tcl Programming/Print version
The earlier found, the easier for the coder, so the golden rule "Test early. Test often" should really be applied. One easy way is adding self-tests to a file of Tcl code. When the file is loaded as part of a library, just the proc definitions are executed. If however you feed this file directly to a tclsh, that fact is detected, and the "e.
Tcl Core Team Interview – OSnews
If the result is not the one expected, this is reported on stdout; and in the end, you even get a little statistics. The Tcl command proc itself doesn't complain if it is called with an existing name. Here is one way to add this functionality. Early in your code, you overload the proc command like this:. From the time that is sourced, any attempt to override a proc name will be reported to stderr on Win-wish, it would show on the console in red.
You may make it really strict by adding an "exit" after the "puts stderr Fix some regsub magic on 'name' left as an exercise. While on Unixes, the standard channels stdin , stdout , and stderr are the same as the terminal you started wish from, a Windows wish doesn't typically have these standard channels and is mostly started with double-click anyway.
To help this, a console was added that takes over the standard channels stderr even coming in red, stdin in blue. The console is normally hidden, but can be brought up with the command. You can also use the partially documented "console" command. The console's text area is actually a text widget created in this interpreter. For example:. Since the console is a Tk text widget, you can use all text widget commands and options on it for example, changing colors, bindings You can resize the whole thing with. The button appears between the text widget and the scroll bar, and looks and does as expected.
There is also a way back: the main interpreter is visible in the console interpreter under the name, consoleinterp. Here's a simple experiment on how to connect two Tcl processes so that one call it "debugger" can inspect and control the other "debuggee". Both must have an event loop running which is true when Tk runs, or when started with e. As this goes over a socket connection, the two processes could be on different hosts and operating systems though I've so far tested only the localhost variety.
Use at your own risk, of course The "debugger" in this version remo.
Now from remo you can call any Tcl command in the "debuggee", where it is executed in global scope, so in particular you can inspect and modify global variables. But you could also redefine procs on the fly, or whatever tickles your fancy Examples from a remo session, showing that the two have different pids, how errors are reported, and that quoting is different from normal needs more work :. A string is a finite-length sequence of characters.
Now, what is a character?
A character is not the same as a glyph, the writing element that we see on screen or paper - that represents it, but the same glyph can stand for different characters, or the same character be represented with different glyphs think e. Also, a character is not the same as a byte, or sequence of bytes, in memory. Let's try the following working definition: "A character is the abstract concept of a small writing unit".
This often amounts to a letter, digit, or punctuation sign - but a character can be more or less than that. Such composition is the software equivalent of "dead keys" on a typewriter.
Although an abstract concept, a character may of course have attributes, most importantly a name: a string, of course, which describes its function, usage, pronunciation etc. Very important in technical applications is of course the assignment of a number typically a non-negative integer to identify a character - this is the essence of encodings, where the numbers are more formally called code points. Other attributes may be predicates like "is upper", "is lower", "is digit". The relations between the three domains are not too complicated: an encoding controls how a Conversely, making sense of a set of pixels to correctly represent a sequence of characters, is the much more difficult art of OCR, which will not be covered here.
Work on encodings, mapping characters to numbers code points , has a longer history than electronic computing. I have worked on Univac machines that used six bits per "Fieldata" character, as hardware words were 36 bits long. The most important purpose, outside the US, was of course to accommodate more letters required to represent the national writing system - Greek, Russian, or the mixed set of accented or "umlauted" characters used in virtually every country in Europe. Even England needed a code point for the Pound Sterling sign.
The general solution was to use the additional positions available when ASCII was implemented as 8-bit bytes, hex A whole flock of such encodings were defined and used:. These national multibyte encodings are:. If the 2x7 pattern was directly implemented, files in such encodings could not be told apart from ASCII files, except for unreadability.
Elsewhere, in order to handle both types of strings transparently in a more practical manner, the "high ASCII" approach was extended so that a byte in FF were interpreted as halves of multibyte codes. For instance, the first Chinese character in GB, row 16 col 1 decimally for short , gives the two bytes. To add to the "ideograph soup" confusion, unlike euc-cn and euc-kr, euc-jp Japan was not widely adopted on the Windows or Macintosh platforms, which instead tend to use the incompatible ShiftJIS, which re-arranges the codes to make space for older single-byte codes for phonetic katakana characters.
The Unicode standard is an attempt to unify all modern character encodings into one consistent bit representation. Initiated by the computer industry www. Software must allow Unicode strings to be fit for i18n.
- The Stainless Steel Rat Saves the World (Stainless Steel Rat, Book 6).
- Reviews in Mathematical Physics - Volume 16.
- Obtaining Expect for UNIX.
From Unicode version 3. The total count in Unicode 3. And the number is growing. Characters are represented as sequences of The general principle of UTF-8 is that the first byte either is a single-byte character if below 0x80 , or indicates the length of a multi-byte code by the number of 1's before the first 0, and is then filled up with data bits.
- Signal Detection and Estimation?
- Foreign Banking and Investment in the United States: Issues and Alternatives?
- Manual of Exotic Pet Practice.
- Assuring the Quality of Health Care in the European Union: A Case for Action!
- Tcl Core Team Interview!
- Tcl packaging guidelines!
- Information About Writing Embedded Event Manager Policies Using Tcl.
All other bytes start with bits 10 and are then filled up with 6 data bits. It follows from this that bytes in UTF-8 encoding fall in distinct ranges:. The distinction between initial and non-initial helps in plausibility checks, or to re-synchronize with missing data. Besides, it's independent of byte order as opposed to UCS, see below.
Tcl however shields these UTF-8 details from us: characters are just characters, no matter whether 7 bit, 16 bit, or in the future more. This isn't always used elsewhere, but will generally override an otherwise declared character encoding if a file starts with it.