Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 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?

like image 588
Xofo Avatar asked Dec 08 '22 04:12

Xofo


2 Answers

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

For more goodies, reread info(n).

like image 131
Alexander Gromnitsky Avatar answered Feb 03 '23 06:02

Alexander Gromnitsky


@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
like image 35
glenn jackman Avatar answered Feb 03 '23 07:02

glenn jackman