October 10, 2015

What does Tcl do if you try to run a command that is not defined?

In Tcl, if a command doesn't exist within the loaded code the interpretter will look through the commands that can be loaded vis package ifneeded, and failing that will call the unknown proc. And there's nothing to stop you defining unknown for yourself, as I did as a demonstration on Friday afternoon on the Learning to program in Tcl course. Example source code [here]

This example also shows a loop in which a whole load of proces is defined, using names in a variable for the name of the command. Yu may think this is a bit of an odd thing to do, but in effect it's what Tk does as it defines commands for each widget, and also what ITcl ([incr-Tcl]) does for its objects - it's just not a common think in a little demo program.

Finally, the example shows how yo accesss shell environment variables via the array env

Posted by gje at 07:48 PM | Comments (0)
More about Graham Ellis of Well House Consultants
Useful link: Tcl training
Related topics: via article database

Tcl - a new example for data reformatting

Tcl is an excellent language for combing through and reformatting data - though rarely a first choice language for such tasks if users aren't doing other things with it too.

Towards the end of last week's Learning to program in TCL course, a "side task" requirement came up to reformat our course diary into a .csv file so that a reseller who sells our courses to fill gaps / niches in his own more generic schedule.

Data of the form
  30,11,2015 EL 8
needs to be transformed into data of the form
  ELFULL,30/11/2015,09:00,04/12/2015,17:00,Standard,1,1,SN12 7NY,En-GB,1350,1350,GBP,Well House,Learning to Program in Perl,Other (IT)Code

Complete program [here] and sample data [here].

The main code is as simple as
  loadDataWH "diary" intoHere
  saveDataQA "qa.csv" intoHere

so you can see that even in Tcl, we're using good, structured programming techniques - in this case letting me provide a number of different input formats and a number of output genereators.

Other points of note ...

• calculating end date from start date, allowing for month and year ends:

  set starts [clock scan "$day,$month,$year 12 00" -format "%d,%m,%Y %H %M"]
  set ends [expr $starts + ($courselength -1) * 3600 * 24]
  set eday [clock format $ends -format %e]
  set emonth [clock format $ends -format %m]
  set eyear [clock format $ends -format %Y]

reduces day, month, year of start to seconds from 1.1.1970, adds the number of days of the course (*3600*24) to make it seconds, and converts back. Much easier than trying to work out month lengths, especially for February in leap years.

• course names ending in "P" are 4 days and "L" are five days:

  set courselength 5
  set courseformat {Learning to Program in %s}
  if {[regexp {P$} $coursecode]} {
    set courselength 4
    set courseformat {%s Programming}

• use of upvar to pass big chunks of data out of and into procs effectively and easily.

• allowing blank lines on input to be ignored

  if {[string length $lyne] < 1} continue

• ... and many other useful techniques, worth a study!

Posted by gje at 06:23 AM | Comments (0)
Useful link: Tcl training
Related topics: via article database

Catching failed commands and not crashing the program in Tcl

Where a command may fail in Tcl (causing Tcl to crash), you can defer it within a catch command. That will return a true value if it fails, and an optional extra parameter gives you the associated error message.

A program to look for the biggest file in or below a directory [here] fails if you run it on a tree with any empty folders, at

  set files [glob "$current/*"]

but that problem may be overcome with a catch:

  if {[catch {set files [glob "$current/*"]}]} {
    set $files ""

Updated code [here].

There's a second catch withn our example too - where the code checks for the file size, it's necessary to catch the file size if it might be used on a "special" such as a socket, pipe or device file.

From our Tcl Programming course as run last week.

Posted by gje at 05:07 AM | Comments (0)
Useful link: Tcl training
Related topics: via article database

October 09, 2015

Loading packages in your Tcl program

In Tcl, extra bundles of code can be loaded from packages which are files of more ... brought in with package require from a folder that's listed in the auto_path variable.

Packages are defined with a package provide in the source code of the package which must be indexed in a file called pkgIndex.tcl. Failure to index will result in Tcl failing to find the package. There's a built in Tcl command called pkg_mkIndex which allows you to generate the index automatically.

Namespaces are technically different to packages, but it'c common practise to define a package in a Tcl namespace to avoid any risk of conflicting names of procedures, and to make it clearer for the maintenance programmer later on to know what's loaded from where.

Example of a program that loads packages - [here]. The packages it loads are [here] (without a namespace and [here] for one with a namespace

Posted by gje at 12:14 PM | Comments (0)
Useful link: Tcl training
Related topics: via article database

Should we get an AA or Visit Wiltshire hotel assessment?

We chat / work with other accommodation providers around the UK to exchange experiences and best practise - and one of the Facebook groups was discussing the ranking / rating (star) system and whether it's useful to apply. Here's what I posted there - worth sharing:

We are not listed / inspected. We took a careful look a number of years ago and decided that most of our stuff is 4/5 star but one or two things that guests don't need / ask for were missing, and that would pull us down to a much lower rating. And the cost would have added several pounds per room per night. We simply don't fit proper.

We are listed with Visit Wiltshire, who changed their policy to allow self-assessed properties a couple of years back, probably because few people were going in for inspections and rankings. We find our Trip Advisor position (currently 1st of 14) and LateRooms reviews far more important in bringing in fresh guests that VisitWiltshire where we *are* now listed too - but the most important guests of all are those on recommendation and repeat business.

"Self-grading" and visitor reviews offer us the best of both worlds - they allow us to give a true picture without an expense we have to pass on; the reviews confirm that the grading we have described is indeed genuine, or perhaps even under-played.

Posted by gje at 07:29 AM | Comments (0)
Related topics: via article database

Well House Consultants Ltd.
Copyright 2015