开发者

How do I print out a tcl proc?

Given a simple tcl proc like

proc foo {a b} {puts "$a $b"}

What tcl command can I use to print out the procedure foo ... that is I want the text of the proc back ...

For instance:

% proc foo {a 开发者_Python百科b} {puts "$a $b"}
% foo a b
  a b

% puts $foo
  can't read "foo": no such variable

how do I get foo {a b} {puts "$a $b"} back?


% proc foo {a b} {puts "$a $b"}
% info body foo
puts "$a $b"
% info args foo
a b

For more goodies, reread info(n).


@Henry, it's a bit more complicated than that if any arguments have default values:

proc foo {a {b bar}} {
    puts "$a $b"
}

proc info:wholeproc procname {
    set result [list proc $procname]
    set args {}
    foreach arg [info args $procname] {
        if {[info default $procname $arg value]} {
            lappend args [list $arg $value]
        } else {
            lappend args $arg
        }
    }
    lappend result [list $args]
    lappend result [list [info body $procname]]
    return [join $result]
}

info:wholeproc foo


Use TclX's showproc:

package require Tclx
showproc foo
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜