Exercises, examples and other material relating to training module T214. This topic is presented on public courses
Learning to program in Tcl,
Tcl Basics
A miscellany of commands, including more on error and file handling, and information commands.
Related technical and longer articles
errors v exceptionsSolution Centre - all article listingSolution Centre - all article listing
| Articles and tips on this subject | updated |
| 3583 | Expanding a list of parameters in Tcl - {*} and eval In Tcl commands, parameters are separated by spaces, just like Tcl lists are. So it would sometimes be very useful to be able to write
set action {piemiddle apple}
set $action
to take a list of parameters from a Tcl list - in this case setting the "piemiddle" variable to the ... | 2012-01-28 |
| 3570 | Trapping errors in Tcl - the safety net that catch provides What could possibly go wrong when you open a file in a program? Lots of things, actually.
* You could try to open a file in directory you cannot access
* You could try to read from a file that does not exist
* You could try to read from something that's not a plain file - e.g. a directory
* You could ... | 2012-01-07 |
| 3287 | Exceptions - Tcl style Checking for all possible errors isn't a "failsafe" way of coding. If you try and check for absolutely any error, you're still very likely to miss one or two possibilities, and it's fare better to add a block to your code to say (in essence) "if anything at all goes wrong, go here...". And that's ... | 2011-05-12 |
| 782 | Converting between Hex and Decimal in Tcl Tcl is much used in the semiconductor industry, so there's a common requirement to use it to convert values back and forth between hexadecimal (base 16) and decimal (base 10).
Easily done! The format command can be used to convert from decimal to hexadecimal, and the expr command can be used to convert ... | 2011-03-01 |
| 2467 | Tcl - catching an error before your program crashes There are times when a Tcl command can fail because of the data being passed in to it ... and when it fails, it can do so with a spectacular crash! For example, the glob command which matches files to a pattern (Tcl's ls or dir if you like to think of it that way) can go 'belly up' if there are no files ... | 2009-10-22 |
| 366 | Error handling in Tcl through catch Tcl is a truly interpretive language - and that means that you can get a syntax error if a user makes an illegal entry ... even in the middle of a program running. For example
puts -nonewline "please enter a number: "
flush stdout
gets stdin value
set doubled [expr $value * 2]
puts "Result is $doubled"
puts ... | 2008-06-07 |
| 239 | What and why for the epoch The Epoch occurred on 1st January 1970 ... at midnight, at the start of the day, GMT. It's an important concept in many programming languages, which work with times and dates before and after the epoch - it means that you can compare dates and times easily, even where month ends and different timezones ... | 2008-05-16 |
| 1338 | Handling Binary data in Tcl (with a note on C) In Tcl, all variables are held as strings, and most of the commands will split / divide strings at new line or space characters by default. However, there are a few commands that do NOT make that distinction and since a Tcl string (Unlike a C string) may contain any bit pattern at all, they provide a ... | 2007-09-11 |
| 1334 | Stable sorting - Tcl, Perl and others Have you come across a STABLE sort? A Stable sort is one in which all the incoming elements which evaluate to an equal value when tested for sorting purposes remain in the same order in the output as they were in the input. Perhaps I had better give you an example.
I have a log file and there's a ... | 2007-09-07 |
| 1277 | AgtInvoke - a command to drive Agilent Tcl software extensions In Tcl, there's a lot of commands available already - and there would be a lot more if separate commands were provided for each operation you might wish to perform. So some of the commands that are provided are really megacommands that wrap a whole lot of others up in a single group.
Let's take string ... | 2007-07-25 |
| 748 | Getting rid of variables after you have finished with them If you've finished with a variable in your program, you can usually just leave it "in situe" and it will be destroyed and the memory it occupied will be released when you exit from your program. In many languages, variables within named blocks of code have an even shorter "shelf life" - by default, a ... | 2006-06-09 |
| 461 | Shortened interactive commands In Tcl, if you're working in interactive mode you can abbreviate the commands you type in, as long as your commands remain unique. So instead of puts you can write put or even pu ... but if you try to run just the p command you'll get
% p hello
ambiguous command name "p": package pid proc puts pwd
% ... | 2006-06-05 (short) |
Examples from our training material
| edo | Running an external process in Tcl |
| err1 | Example of a Tcl program that fails |
| err2 | A Tcl program that uses catch to trap a run time error |
| firstline | Using catch to trap runtime errors |
| goalie | catching exceptions - Tcl style |
| lick | Finding size of .gif image in Tcl - binary scan |
| vt | Tcl program that manipulates a variable |
| vtt | Tcl program that uses the trace command to report the manipulation of a variable |
Pictures
Tcl trainees on a practical
Background information
Some modules are
available for download as a sample of our material or under an
Open Training Notes License for free download from
http://www.training-notes.co.uk.
Topics covered in this module
Information.
The info command.
Other information commands.
history.
Error control.
unknown.
File handling.
fconfigure.
seek.
Variable control.
unset.
Tracing a variable in Tcl.
Special variables.
Other Subjects for group discussion.
Complete learning
If you are looking for a complete course and not just a information on a single subject, visit our
Listing and schedule page.
Well House Consultants specialise in training courses in
Tcl/Tk, Expect,
Python,
Perl,
PHP, and
MySQL. We run
Private Courses throughout the UK (and beyond for longer courses), and
Public Courses at our training centre in Melksham, Wiltshire, England.
It's surprisingly cost effective to come on our public courses -
even if
you live in a different
country or continent to us.
We have a technical library of over 700 books on the subjects on which we teach.
These books are available for reference at our training centre. Also
available is the Opentalk
Forum for discussion of technical questions.