Version 1 of interp serialize

Updated 2006-07-19 05:34:17

Wed Jul 19, 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] {
            foreach v [$interp eval info vars ${ns}::*] {
                if {[array exists $v]} {
                    lappend result "array set $v [list [$interp eval array get $v]]"
                } else {
                    lappend result "set $v [list [$interp eval set $v]]"
                }
            }
        }
        return [join $result "; "]
    }