1. Export
[Mod export] is the mechanism used by a namespace to declare
itself as a Module and exporting its commands.
A module is a convienient way of packaging-up a
namespace as a collection of sub-commands, often
used in conjuction with [Mod uses].
Here is a simple but complete module containing two member sub-commands.
package require Mod
namespace eval ::myns::foobar {
Mod export
proc foo a { return "foo$a" }
proc bar {a b} { return "bar$a,$b" }
}
1.1 Using Modules
Normally a module would be used with [Mod uses] via Autoimport.
However, traditional Tcl usage is also possible:
namespace eval ::myapp {
namespace import ::myns::foobar::* ::myns::foobar
prod work {args} {
foo 99
bar 88
foobar bar 77
}
}
Mod and uses are intended to address the downsides of namespace
handling in traditonal Tcl, namely:
- Code for ::myns::foobar must be loaded before it can be imported.
- All functions normally get imported at once, rather than just the needed ones.
1.2 Ensembles
A module shares characteristics
of an namespace ensemble (and in fact when using 8.5 Ensembles are used in the implementation) but with the
following additional attributes:
- Command calls under 8.4 or less may not use shortform commands
- The command created in the parent namespace also gets exported.
- The [Mod export] command performs various other housekeeping.
That's the reason the above example uses a nested sub-namespace:
the foobar comand becomes available as a subcmd of myns.
This allows
users of the module to import and use it without contaminating the :: namespace.
© 2008 Peter MacDonald