2

I am trying to convert some tcl script into a C++ program. I don't have much experience with tcl and am hoping someone could explain what some of the following things are actually doing in the tcl script:

 1) set rtn [true_test_sfm $run_dir] 2) cd [glob $run_dir] 3) set pwd [pwd] 

Is the first one just checking if true_test_sfm directory exists in run_dir?

Also, I am programming on a windows machine. Would the system function be the equivalent to exec statements in tcl? And if so how would I print the result of the system function call to stdout?

1 Answer 1

4

In Tcl, square brackets indicate "evaluate the code between the square brackets". The result of that evaluation is substituted for the entire square-bracketed expression. So, the first line invokes the function true_test_sfm with a single argument $run_dir; the result of that function call is then assigned to the variable rtn. Unfortunately, true_test_sfm is not a built-in Tcl function, which means it's user-defined, which means there's no way we can tell you what the effect of that function call will be based on the information you've provided here.

glob is a built-in Tcl function which takes a file pattern as an argument and then lists files that match that pattern. For example, if a directory contains files "foo", "bar" and "baz", glob b* would return a list of two files, "bar" and "baz". Therefore the second line is looking for any files that match the pattern given by $run_dir, then using the cd command (another Tcl built-in) to change to the directory found by glob. Probably $run_dir is not actually a file pattern, but an explicit file name (ie, no globbing characters like * or ? in the string), otherwise this code may break unexpectedly. On Windows, some combination of FindFirstFile/FindNextFile in C++ could be used as a substitute for glob in Tcl, and SetCurrentDirectory could substitute for cd.

pwd is another built-in Tcl function which returns the process current working directory as an absolute path. So the last line is querying the current working directory and saving the result in a variable named pwd. Here you could use GetCurrentDirectory as a substitute for pwd.

Sign up to request clarification or add additional context in comments.

1 Comment

That sequence with glob is the sort of horrible thing people used to use a lot before Tcl had file normalize. That style of using glob and cd breaks catastrophically when file-/dir-names have spaces in; the list quoting will pretty much guarantee that what happens is not what anyone wants. In Tcl 8.5 upwards, cd {*}[glob ...] will at least be less crazy when it fails. (Using globbing to get a directory name for this sort of thing isn't recommended in a script, and it's very lazy in interactive use.)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.