4
votes

I'm probably missing something obvious here but maybe you can help me.

I have a tcl script. Here it is in its entirety:

puts [encodeSDR 25]

proc encodeSDR {input} {
  set sdr "test"

  return $sdr
}

when I run this script I get an error:

c:\temp>tclsh testenviro.tcl
invalid command name "encodeSDR"
  while executing 
"encodeSDR"
  invoke from within
"puts [encodeSDR 25]"
  (file "testenviro.tcl" line 1)

What am I missing? Seems like this should work perfectly.

PS. Found possible reason:

There when I put the puts call below the proc it worked - so does it not load the entire script first? seems weird if that's the case but maybe thats it.

1
Code in procedures can be byte-code compiled, but the "global" level code is still interpreted. Realize that proc is just a regular command, and the results are less surprising. I often put all "global" code in a main proc, then the only "global" command is the call to main.glenn jackman
what if encodeSDR were in a different script?Thufir

1 Answers

3
votes

You are correct in that the entire script is not loaded before any execution takes place. A TCL script is interpreted one command at a time, and then will move to the next one only once it is finished. There is no compiling beforehand to check for procedures you may be referencing down below.

In your case, it first tried to interpret the puts command, which involved calling encodeSDR. Since that had not been defined in memory as of yet, the interpreter had no idea what you were trying to do.

One thing to watch out for is if you define the procedure by itself (say during testing/debug), and then later add it into a script in a way like your example. It will work just fine until you close out of the session, and TCL's memory gets released. The next time you load the script, however, it will fail because the procedure was never loaded for that session.