Recently, I have been working on a small project of mine that is written in TCL. The language is a very interesting because it is completely string based. Everything, is a string even lists are just strings packaged in a formal syntax for the interpreter to realize. My code required me to manipulate arrays which are not direct variables and must be accessed via commands. This requirement makes passing arrays between functions fairly annoying and possibly time consuming since the array is being recalled and set back into memory between passes.

As I looked for solutions to this problem I discovered a neat command called upvar. This command just references a variable from a different level of the interpreter. At first this command seemed very puzzling to me. What exactly is it doing?! Most posts online just reiterate how it may be difficult to understand upvar at first and never offer anything of substance to help you understand it.

It’s simple really. You are already familiar with commands like regsub and append in TCL where you do not pass the variable but just the variable name (you don’t place a $ in front of the variable name). If you think about those commands they save the results to that variable as well. They are using the variable the same way upvar does (assuming your calling it from 1). The command has helped a great deal in passing arrays between functions. Just be ware though that the upvar’d variables are modified in the callers proc as well.

Categories: Technology

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *