如何打印出tcl proc?

Xof*_*ofo 4 tcl redefinition proc-object

给出一个简单的tcl proc

proc foo {a b} {puts "$a $b"}
Run Code Online (Sandbox Code Playgroud)

我可以使用什么tcl命令打印出程序foo...这就是我想要proc 的文本回来...

例如:

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

% puts $foo
  can't read "foo": no such variable
Run Code Online (Sandbox Code Playgroud)

我怎么foo {a b} {puts "$a $b"}回来?

Ale*_*sky 8

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

欲了解更多信息,请重读信息(n).


gle*_*man 7

@Henry,如果任何参数都有默认值,它会比这更复杂:

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
Run Code Online (Sandbox Code Playgroud)