Updated 2015-11-24 05:50:22 by pooryorick

namespace import imports commands into a namespace.

Synopsis  edit

namespace import ?-force? ?pattern pattern ...?

Documentation  edit

official reference

Description  edit

Each pattern is a qualified name, the tail of which is a glob-style pattern that selects command names in the namespace specified by the qualifiers. glob-style characters only have their glob pattern meanings in the tail component of pattern. In the namespace qualifiers of pattern they have no special meaning. Each command name that matches the pattern and that is exported from the specified namespace is used to resolve a routine which is then bound to the same name in the current namespace. An imported routine can be renamed without disturbing the binding. The routine can also be renamed in the origin namespace without disturbing the binding.

namespace import normally returns an error if an imported routine conflicts with an existing routine. However, if the -force option is given, imported routines silently replace existing routines.

namespace import has snapshot semantics: Only matching command names that are bound to a routine in the exporting namespace are imported. In other words, you can import only the routines that are in a namespace at the time that namespace import is executed. To import a routine, namespace import must be called after the routine has been exported. This is in contrast to namespace export whose patterns apply to all command names in the namespace, regardless of when they were defined.

Calling namespace import with no arguments results in a list of command names bound to routines that have been imported into the current namespace. To find the origin of the routines in this list, use namespace origin.

DGP: namespace path and namespace import offer different mechanisms for access to commands in other namespaces. Use the one that does what you want. There's no general preference, only the particular preference of each programmer, guided by needs and taste.

namespace import ::foo::x
namespace import a::p*

Misc  edit

escargo 2006-12-04: I tested this to see what namespace import returns, and it didn't return anything. There are two behaviors that I think might be useful that aren't implemented:

  1. Have namespace import return a list of the commands that are imported. Right now I don't see any easy way to determine what gets imported. (DKF: This is what 8.5 now does, BTW.) (What TIP 261 specifies is the behavior with no arguments. That's not quite what I'm suggesting.)
  2. Alternatively, have namespace import take another parameter, perhaps -asif, that tells the it to just return the names of what would be imported and to not do the actual import.

My motivation has to do with Nagelfar, actually. The second option would allow a code analyzer to follow along with "package require" and "namespace import" to discover code that would be known to the code being analyzed. This would allow reduction of false positives for unknown commands.

See Also  edit