beetlebug▾
/

namespace eval

Runs a script in a namespace, and makes the namespace when it does not exist.

namespace eval NS ARG ?ARG ...?

Description

Joins the ARG words as concat does and runs the result in the namespace NS. A NS that does not exist is made, with each namespace of its path. A relative NS is read from the current namespace, so namespace eval b in a is ::a::b.

In the script, namespace current is NS, a new proc goes in NS, and variable makes a variable of NS. A plain set of a name with no :: also sets a variable of NS. An error in the script is the error of namespace eval.

Arguments

ArgumentTypeDefaultDescription
NSnamespace namerequiredThe namespace, made when it does not exist.
ARGscriptrequiredA part of the script, joined as by concat.

Returns

The result of the script.

Examples

% namespace eval a::b::c {namespace current}
::a::b::c

% namespace eval a {namespace eval b {namespace current}}
::a::b

% namespace eval q {variable x 5}
> set q::x
5

# set of a plain name makes a variable of the namespace.
% namespace eval q2 {set y 6}
> list [info exists q2::y] [info exists ::y]
1 0

% namespace eval a set w 2
2

Errors

% namespace eval a
wrong # args: should be "namespace eval name arg ?arg...?"
-errorcode TCL WRONGARGS

% namespace eval a {error boom}
boom
-errorcode NONE

Facts

Capability
None.
Tcl release
Tcl 9.1 and 8.6.
Since
ztcl 1.0.0-beta.1

See also