namespace qualifiers

Synopsis

namespace qualifiers string

Description

namespace qualifiers is the complement (almost - see below) of namespace tail. It returns the leading namespace qualifiers for string, or, when string is ::, the empty string. Qualifiers are namespace names separated by ::. The operation of namespace qualifiers is purely lexical. It does not check whether the namespace names exist.

Examples

The value of

namespace qualifiers ::foo::bar::x

is

::foo::bar

Issues

Namespace qualifiers is inconsistent when applied to names in the root namespace:

% namespace qualifiers ::foo::bar
::foo
% namespace qualifiers foo::bar
foo
% namespace qualifiers foo

% namespace qualifiers ::foo

This makes it a bit tricky to use correctly. Using ensemble extend it's not hard to write a more ergonomic version:

extend namespace {
    proc join {prefix name} {
        if {[string match ::* $name]} {
            return $name
        }
        if {$prefix eq "::"} {
            return ::$name
        }
        return ${prefix}::$name
    }
    proc prefix {name} {
        string range $name 0 end-[string length [namespace tail $name]]
    }
}

With these two procs, $x is always equal to:

namespace join [
    namespace prefix $x
] [
    namespace tail $x
]

See Also

namespace