Exercises, examples and other material relating to training module T207. This topic is presented on public courses Learning to program in Tcl
, Tcl Programming
, Tcl Programming
As your code grows from 1 to 10 to 100 lines, you'll find that it's helpful to split it into a series of logical blocks, each of which can be run as a command. You'll learn how to define such procedures, how to pass information in and out of them, and how to access information that's held in your main program and in the calling procedure too.
|Articles and tips on this subject||updated|
|4398||Accessing variables across subroutine boundaries - Perl, Python, Java and Tcl|
In a perfect programming world, code is broken into named blocks (methods, functions, subroutines, classes, procedures, commands, modules, packages etc) and data is passed around between the blocks purely as parameters and return values. And variables are local to the block in which they're used. This ...
|3629||Sharing lots of values in Tcl without having lots of global declarations|
Q: How can I make a lot of values defined in my Tcl main program available in various procs, without having to pass them all down the chain or declare them as global all over the place?
A question from today's Tcl course - to which my first reaction was "why do you want to do that?". However, having ...
|3414||Passing back multiple results in Tcl - upvar and uplevel|
What's the effect of having a meal? There are many. "You're not hungry any more" is the obvious answer, but also "there's washing up to do", "there's a gap on the shelves" and so on.
And yet when programming with an assignment statement (or a set command in Tcl - I am running a Tcl course this week) ...
|3192||Tcl - Some example of HOW TO in handling data files and formats|
During the Tcl course I was running earlier this week, we got involved in a number of interesting topics such as
• How to clean up input lines into lists of fields (use split)
• How to reformat awkward fields (use regexp and regsub)
• How to combine files that vary in format from year ...
|1404||Tcl - global, upvar and uplevel.|
In Tcl, variables are "scoped" to the procedure in which they're used. That's a sensible default, since you want to be able to write main applications without the risk of interfering with variables within your procs.
A global command in a proc allows you to refer to a variable in the main code from ...
|2929||Passing a variable number of parameters in to a function / method|
How many columns are there in each row in a table? It will vary, depending on the table!
If I want to write a "row" function in PHP, passing in as parameters each of my columns, I don't know the parameter count. But I can find out using the fun_num_args function, and I can then get each of them using ...
|2520||Global and Enable - two misused words!|
The word global is used in declaring variables in some languages such as Tcl and Python to indicate that the variable being referred to is shared with the variable of the same name at the top scope. To use the word global, which implies that the declaration makes the variable visible everywhere, is misleading
|2476||Tcl - uplevel to run code at calling level|
Tcl procs (they're Tcl's functions) can pass information back via the return command, and they can access data in the top level of code via the global commands. Incoming parameters can be passed in 'by name' so that any values altered within the proc are also altered in the calling code - there's an ...
|2466||Tcl - passing arrays and strings in and back out of procs|
When you want to pass data INTO a proc in Tcl from a regular variable, you write the variable name with a $ in front of it in the code and the value is substituted. But this doesn't work if (a) you are passing an array (which cannot be expanded into a string or (b) you want to change the value in a ...
|1782||Calling procs in Tcl and how it compares to Perl|
In languages such as Java, you must call your named blocks of code (methods) with the correct number and type of parameters, but in Perl you may call them with as many or as few parameters as you like ... then write the named block of code (a sub) to handle (or ignore) whatever it gets.
This week, I'm ...
One of the vital topics on all our programming courses is that of variable scope. Variable Scope may be defined as the area of a program in which a variable is visible, and how long that variable is accessible for.
Why do I describe variable scope as a vital subject when you can write simple programs ...
|1163||A better alternative to cutting and pasting code|
If you're new to coding, you'll be so concerned to be writing code that works that you may not take a look at coding technique. Your nose will be so close to the grindstone as you work that you won't take the time to look and ask "Do I need to keep grinding anyway?"
If you find yourself writing a piece ...
|775||Do not duplicate your code|
If you've writing or maintaining a program and you find yourself cutting and pasting a chunk of code, STOP and think again.
By duplicating a block of code, you're duplicating your maintainance task from that point onwards - any fixes applied to the original much be applied to the copy too. And that's ...
|409||Functions and commands with dangerous names|
There are some words that we use in our day to day programming life that seem a little scarey and inappropriate to their real task ... and sometimes those words worry trainees on our courses. Examples:
PHP's reset function. It's name seems to threaten to clear our data from somewhere, but all it does ...
Examples from our training material
|add2|| Answer to final exercise|
|addindemo|| Sample code to complete - final exercise |
|another|| upvar to return a second value|
|cbn|| Using upvar to call by name|
|doublit|| Another upvar example for learners|
|dr2|| Optional parameter in Tcl|
|dresser|| Changing a value within a proc and returning the altered value|
|gsc|| The global scope|
|knots|| Unloading an array of inputs as local variables in a proc|
|p1|| Sharing a variable using global|
|p2|| Sharing a variable using upvar|
|p3|| Sharing a variable using upvar and call by name - RECOMMENED|
|p4|| returning a variable - RECOMMENED for passing 1 piece of data back|
|p5|| Sharing a variable using uplevel - obscure!|
|pr1|| Defining a proc (command) and using it|
|pr3|| loading a file of procs shared with other applications|
|scope1|| Variable default to being local|
|tied|| Using an array of inputs as if they were local variables in a proc|
|up1|| Using upvar to effect a variable in the calling code|
|uquery|| Shared file of procs|
|uv|| Call by value and call by name|
|vardemo|| Calling a proc with a variable number of arguments|
|whatandwhen|| Exercise - please write the proc to complete|
|wwanswer|| First sample answer - "getnumber"|
|wwbetter|| Better sample answer - "getnumber" |
|yetan|| uplevel to store a value in parent|
PicturesRadia software is tailored through Tcl procedures
Teaching Tcl around the world
Some modules are available for download
as a sample of our material or under an Open Training Notes License
for free download from [here]
Topics covered in this module
Holding commonly used command sequences.
Proc and return.
Upvar, uplevel and global.
Structured programming and OO.
Packages and namespaces.
If you are looking for a complete course and not just a information on a single subject, visit our Listing and schedule
Well House Consultants specialise in training courses in
. We run
throughout the UK (and beyond for longer courses), and
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.