Recent Changes - Search:


Mod /

Export

(:toc:)

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" }
  }

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:

  1. Code for ::myns::foobar must be loaded before it can be imported.
  2. All functions normally get imported at once, rather than just the needed ones.

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:

  1. Command calls under 8.4 or less may not use shortform commands
  2. The command created in the parent namespace also gets exported.
  3. 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.

Edit - History - Print - Recent Changes - Search
Page last modified on January 14, 2008, at 02:07 PM