Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
Packages and Namespaces are like Suitcases and Aircraft

Are 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.
(written 2007-09-09, updated 2007-09-11)

 
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
T213 - Tcl/Tk - Libraries, Packages and Namespaces
  [146] example of Tcl namespaces and packages - (2004-12-10)
  [1529] Tcl - learning how to use namespaces, packages and libraries together - (2008-02-02)
  [2476] Tcl - uplevel to run code at calling level - (2009-10-24)
  [3066] Separating groups of variables into namespaces - (2010-11-24)
  [3417] What is a namespace and why do we need them? - (2011-09-03)
  [3418] Tcl packages, pkg_mkIndex, pkgIndex.tcl -what are they and why use them. - (2011-09-03)
  [4522] Loading packages in your Tcl program - (2015-10-09)


Back to
Handling Binary data in Tcl (with a note on C)
Previous and next
or
Horse's mouth home
Forward to
Tk locks up - 100% c.p.u. on a simple program (Tcl, Perl, Python)
Some other Articles
Melksham Laundrette
Google, wwmdirectory, Freshwater, ATP - new scam?
Moving to Melksham? Househunting map.
Tk locks up - 100% c.p.u. on a simple program (Tcl, Perl, Python)
Packages and Namespaces are like Suitcases and Aircraft
Handling Binary data in Tcl (with a note on C)
A series of tyre damages
Ignore case in Regular Expression
Expanding a grid - Tcl/Tk
Stable sorting - Tcl, Perl and others
4759 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2024: 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/1339_.html • PAGE BUILT: Sun Oct 11 16:07:41 2020 • BUILD SYSTEM: JelliaJamb