4

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

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

Andrew Grimm
  • 78,473
  • 57
  • 200
  • 338
Xofo
  • 41
  • 1
  • 2

3 Answers3

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

For more goodies, reread info(n).

Alexander Gromnitsky
  • 2,949
  • 2
  • 33
  • 38
7

@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
  • 238,783
  • 38
  • 220
  • 352
0

Use TclX's showproc:

package require Tclx
showproc foo
Hai Vu
  • 37,849
  • 11
  • 66
  • 93