tcltk-toolkittclsh

TCL: Can I extract a procedure from a script file without sourcing it?


I was wondering if there is a way to extract a procedure from another script without sourcing all of it. The goal in this case is to not create a new file with the methods separated from the main script file, but to use the procs that are into that script.

I have use open, read and source and eval.


Solution

  • The implementation of source is almost exactly like open+read+eval (except with the C API). It isn't smart.

    In general, getting the contents of a procedure can only be done by using info body after it has been created (plus info args and info default). But often people write code such that the word proc is in the first column of a line and the body is all indented, with first following non-indented line being the closing }. That won't work in all cases (the main trickiness is when namespace eval is in use) but will in many. Even then, the word proc is probably the first on its line (with indenting) and the close-brace is aligned with it.

    I'm one of the few people that really writes code that doesn't always follow this convention. You don't need to worry about me.