how can I redirect a proc output into a file in tcl, for example, I have a proc foo, and would like to redirect the foo output into a file bar. But got this result
To accomplish this I wrapped the call to my Tcl proc in a shell script. This works on unix, not sure about other OS.
file - foo.tcl:
proc foo {} {puts "Hi from foo"}
file - foo.csh (any shell script will work, I'm using csh for this example):
enter code here
#!/usr/bin/tclsh source foo.tcl eval "foo $argv"
file - main.tcl:
exec foo.csh > ./myoutput.txt
Of course these commands can be made 'fancy' by wrapping them in safety measures like catch, etc... for clarity sake I didn't include them, but I would recommend their use. Also I included $argv which isn't needed since proc foo doesn't take args, but typically IRL there will be args. Be sure to use >> if you just want to append to the file rather than overwriting it.