views:

381

answers:

3

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 b} {puts "$a $b"} % foo a b a b

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

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

Thx

+3  A: 
% 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 Flower
+4  A: 

@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
glenn jackman
A: 

Use TclX's showproc:

package require Tclx
showproc foo
Hai Vu