Version 7 of interp serialize

Updated 2007-07-31 18:17:01 by LV

Wed Jul 19, 2006, CMcC: A quickie to serialise the variables in a sub-interpreter.

This has undoubtedly already been done, but I can't recall where it is.

nstree returns a flattened trees of all namespaces in the interp.

    proc nstree {interp {prefix ""}} {
        set result {}
        foreach ns [$interp eval namespace children $prefix] {
            lappend result $ns
            lappend result {expand}[nstree $interp $ns]
        return $result

serialize returns a valid tcl command to recreate the variables in the interp.

    proc serialize {interp} {
        $interp eval {unset -nocomplain errorCode; unset -nocomplain errorInfo}
        set result {}
        foreach v [$interp eval info globals] {
            if {[string match tcl* $v]} continue
            if {[$interp eval array exists $v]} {
                lappend result "array set $v [list [$interp eval array get $v]]"
            } else {
                lappend result "set $v [list [$interp eval set $v]]"

        foreach ns [nstree $interp] {
            set subresult {}
            foreach v [$interp eval info vars ${ns}::*] {
                if {[array exists $v]} {
                    lappend subresult "array set [namespace tail $v] [list [$interp eval array get $v]]"
                } else {
                    lappend subresult "set [namespace tail $v] [list [$interp eval set $v]]"
            lappend result [list namespace eval $ns [join $subresult {; }]]
        return [join $result "; "]

interp serializing

Category Example