namespace import

Difference between version 25 and 26 - Previous - Next
'''`[namespace] import`''' imports commands into a namespace.



** Synopsis **

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



** Documentation **

   [http://www.tcl.tk/man/tcl/TclCmd/namespace.htm%|%official reference]:   



** Description **

Each ''pattern'' is a qualified name, the [namespace tail%|%tail] of which is a `[glob]`-style
pattern that selects command names in the namespace specified by the
qualifiers.  `[glob]`-style characters have their special meanings only in the
tail component of ''pattern'', not in any namespace qualifiers of ''pattern''.
For each command name that matches the pattern and that is exported from the
specified namespace, a routine by the same name is created in the importing
namespace as an alias for the routine in the exporting namespace.  Both the
original routine and the alias can be [rename%|%renamed] without disturbing the
association between them.  The original routine can even be replaced with a new
routine, after which alias refers to the new routine.  However, deleting the
original routine causes the alias to be deleted as well. 

`[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.


'''Examples''':

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


** Alternative: `[interp alias]` **

`namespace import` only imports exported commands, and also matches patterns
rather than comparing for equality.  `[interp alias]` can be used if these
behaviours are not desired:

======
interp alias {} ::name::of::somecommand {} ::name::of::link
======



** Misc **

[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] [http://www.tcl.tk/cgi-bin/tct/tip/261.html%|%261] specifies is the behavior with no arguments. That's not quite what I'm suggesting.)

   1. 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 **

   [namespace]:   


** Page Authors **

   [PYK]:   

<<categories>> Command | Namespace