Version 4 of Problems and Solutions

Updated 2006-12-29 22:22:39

On this page I will write about little problems which I have with Tcl and about how I solve those problems. I think that these problems are so small, they don't deserve a whole wiki page, but are worth mentioning anyway.

Anybody is welcome to add their problems and solutions, since this is after all a wiki.

JMPB


When looking at most modules you see, code like this is everywhere:

  proc ::base64::encode { ... } { ...
  ...
  proc ::base64::decode { ... } { ...
  ...

Lots of typing to simply put a few procs into a seperate namespace. It may seem that this is a trivial issue - and it is. But I think it is worth looking for an alternative. So, I simply thought this one up:

  proc _proc { name args body } {
    proc "::${::_namespace}::$name" args body
  }

To use:

  set _namespace "base64"
  ...
  _proc encode { ... } { ...
  ...
  _proc decode { ... } { ...
  ...

I have not simply temporarily replaced proc because that could mess up when other modules are loaded.

Would be happy to hear about improvements to this. JMPB


MG You can also do

  namespace eval ::mynamespace {
    proc a {} {return "this is ::mynamespace::a"}
    proc b {} {return "this is ::mynamespace::b"}
  }

I think the reason that most people write it out in full for each proc is so that it's easy, at a glance, to see exactly what/where each proc is. (If you have 20 or 30 reasonably large procs, you could have to do a lot of scrolling to find the namespace eval they're inside to figure out what the proc is actually called.)


2006-12-29 jmpb - So how about access to namespace variables? Is there some way of accessing the variables inside a namespace eval without needing to know the namespace name (i.e. ::..::varName)? Our would upvar be preferred? Upvar takes quite some speed from the script from what I heard, so...