namespace eval

Difference between version 11 and 12 - Previous - Next
'''[namespace] eval''', evaluates a [script] in a given namespace.



** See Also **
    
   `[lambda]`:   

   `[namespace]`:   

   `[eval]`:   



** Synopsis **

    :   '''[namespace] eval''' ''namespace arg'' ?''arg ...''?



** Description **

''arg''s are [concat%|%concatenated] together in the same fashion as `[eval]`, with with a space between each one, and the result is evaluated in the namespace called
''namespace''.  ''namespace'' and any namespaces that qualify it are automatically created as necessary.

Unlike `[eval]`, `namespace eval` operates at new level, such that `[uplevel] 1` accesses
the level from which `[namespace eval]` was called.  In spite of this, `[return]`  within
`[namespace eval]` script operates as if it were `[uplevel] return`, i.e., it causes
the caller of `[namespace eval]` to return.  It would probably be more useful
if `[return]` within a `namespace eval` script behaved the way it does within
`[source] ...` script, causing only `[namespace eval]` itself to return.

For better performance, `namespace eval ...` is usually
[bytecode%|%byte-compiled] when it is given one argument which is a literal
string:

======
proc main {
    namespace eval [info cmdcount] {
        set msg {this script is eligible to be byte-compiled}
        return $msg
    }
}
======

----

[PYK] 2016-03-12:  `namespace eval` adds an evaluation level, but doesn't allow
a `[tailcall]` to replace that level.  To create a command prefix that changes
to some namespace to call the command, while arranging for the caller of the
command to be one [uplevel%|%level] up: 

======
list apply [list args {::tailcall {*}$args} some_namespace] some command
======

The resulting command prefix is useful, for example, as the value of the`-unknown` option for a [namespace ensemble].  `[ycl] ns call` is an
implementation of this.


<<categories>> Command | namespace