Packages and Namespaces are like Suitcases and Aircraft
Archive - Originally posted on "The Horse's Mouth" - 2007-09-09 06:45:09 - Graham EllisAre Suitcases and Aircraft the same things? No, clearly they're not, but you'll often use them in close assocoation with one another. When I was in Albany, New York State, the other week ... I travelled there from Melksham with a suitcase of luggage, and I used an Aircraft for my journey. Taking each item of luggage separately, or going by boat, would both have been changes that would have made my trip impractical. And so it is with packages and namespaces in Tcl. They are separate facilities, but they are naturally used together.
A package is a bundle of pieces of code - usually procs - which are held in a separate file in a separate directory. They're loaded in as required with a package require statement, which looks in the various directorys that are in your auto_path varaible for a file called pkgIndex.tcl.
The command pkg_mkIndex (that's a Tcl command!) is used to automatically index and create each of your directories of packages, and in your main program you can use lappend to add extra directories onto the auto_path.
Finally, notes: All packages must be provided with a version number - it cannot be left out. Packages are only actually loaded when their first proc is called and not at the point you say you require them.
Sample files:
twice - program that calls in a package
toes.tcl - the package it loads
Namespaces are a way to allow you to zone your variables - give separate names to procs and variables that are called the same thing within their own areas of code. So you can do a namespace eval shapes { and the block of code down to the } would all be evaluated it it own private area. If you defines a proc called rectangle within shapes then you would refer to it at the upper level as shapes::rectangle or - in absolute terms - ""shapes::rectangle.
Notes. It's usual to have your package name, your namespace name and your file name all the same so that you and your maintainance programmer can easily see what goes with what later on. Procs can be exported from namespaces and imported to others if you wish to reduce the amount of "::" notation you're using, but shouldn't be used too much as they somewhat nullify the whole point of separate namespaces.
Sample files:
2wice - program that uses a namespaced package
doubler.tcl - that namespaced package
Used together, Packages and Namespaces provide an excellent mechanism for you to split up code between a number of developers and share code between a number of applications in Tcl, without any serious issue of conflicts bewteen the people and the areas. The use of namespaces further provides some built in self-documentation which is a huge help in knowing what's where in a large system.
We cover Packages and Namespaces on our public Tcl programming course, as they're an integral and important part of most larger Tcl applications these days. For newcomers to programming who start to learn Tcl on a Monday, this subject on Wednesday will be more of a demonstration than something they'll do a long practical on, but for experienced programmers there's a very useful exercise to be done. And for nearly everyone who has existing code to maintain, there's a lightbulb moment where they see how the colon-colon (::) notation comes together.
The example that you're looking at here was written within the last few days to illustrate some points I was making while running a private Tcl course in the Netherlands; such courses can be (and are) tailored to suit customer needs. We can also run tailored private course at our own training centre.