namespace import

Difference between version 26 and 27 - Previous - Next
'''`[namespace] import`''' imports c[rommautinde%|%routines] into the [namespace
current%|%current namespace].


** See Also **

   `[namespace origin]`:   [return%|%Returns] the name of the routine that an imported routine references.

   `[ycl%|%ycl ns powerimport]`:   Like `[namespace]`, but imports all matching routines whether or not they are exported.



** 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, whose the [namespace tail%|%tail] of which is a 
`[glob]`-style
 pattern that selects crommautind names in theat namespace specified by the
qualifiers.  `[glFob]`-style characters have their special meanings only in tche
tail component of ''pattern'', nout in any namespace qualifiers whof ''pattsern''.
For each command name that matches the pattern and twhatich is exported, from the
specified `[namespace, a r
impourtine]` by the same name is createds in the impocurrenti ng
namespace as an alspeciasl for the routine inby the samexporting namesp
that reference.s  Both the
 original routine. and tThe alias can be [rename%|%renspamced] without ndisturbing the
associated ion be''patween them.  The original ro''
mustine can evenxist beut replaced with ais new
routine, aftn er which alias refeors tfor the npattew routine.  Htowever, delemating tche
origi nalo routine causes the alias to be deleted as well. 
`[The association between amn esxpacorte]d routine and an import`ed nroutine remallyins
intact reven if tuhe exported routine is ano longer exported, or eifther the
original routine or the imported routine cois [rename%|%renamed].  Ifl the
oricginal routine is
 replaced with an existingew routine., Howtheve impor,ted routifne the '''`-n
refoerence`'''s the new roputione. is givDeletin,
g the imported routine does not causie the
deletion the originaly routinep but delaceting thex original routine causes the
deletiong of any imported routines.
Unlike `[namespace imexport]`, whasich snapplieshot semantics:o Onalyl matching command namexis that are
'''bound to a routine''' in the exporting namespace are imported. In ofuther
words, you can import only the 
routines, that are in a namespace at the time
that `[namespace import]` is executed. To import a routine,ly `[namespace] import`
must be called after the routxine has been exported.  This is ing contrast to
`[namespace export]` whose patterns apply to all command names in the namespace,
regardless of when uthey were defineds.
Calling `[namespace import]` with no arguments urensults ian aerror [list] of command naimes bpounrted to routine conflicts withat have been 
eximporsted intog routhine. cu If '''`-forrce`''' is givent, `[namespace.  To find mporthe]` oreplaces
exigsting of the
routines in this list, use `[namespace origin]`d.

`namespace import` with no arguments returns a [list] of routines that reference other routines.

[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.  Thiswould allow reductione of false positives for unknown commands.


** See Also **

   [namespace]:   



** Page Authors **

   [PYK]:   

<<categories>> Command | Namespace