0

I want to rename or delete namespace in tcl, can anyone tell me how to do it?

rename gk ""

Here gk is the namespace

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129

3 Answers3

2

I don't know of any way to rename an existing namespace. To delete a namespace:

namespace delete namespace_name

All the child namespaces, proc, vars within the namespace will be deleted as the result of this call.

Hai Vu
  • 37,849
  • 11
  • 66
  • 93
  • 1
    Namespaces cannot be renamed. There's no operation to do so, and the implementation strongly assumes that it is impossible. – Donal Fellows Aug 28 '13 at 09:49
0

Use this proc to rename any given namespace:

proc renameNameSpace {oldNameSpace newNameSpace} {
    foreach proc [info procs ::${oldNameSpace}::*] {
        rename $proc ::${newNameSpace}::[lindex [split $proc ::] end]
    }
}
0

Maybe this approach offers a solution. Where ns1 is the source namespace, and ns2 the destination.

proc xxxx {ns1 ns2}  {
    namespace eval ::${ns2} {}
    set map [list $ns1 $ns2]
    
    foreach type {commands vars procs } {
        foreach item [info $type ${ns1}::*] {
            set err ""
            catch { rename $item [string map $map $item] } err
            if { $err != "" } { puts $err }
        }
    }
namespace delete $ns1
}