Using A Value Returned By A Command In TCL
In Variables In TCL, we were unable to see the value returned by a set command. To capture the value returned by a command, we enclose that command inside []. In Grouping Strings In TCL, we have read about groping using "" and {}. Here we are going to write a file named return.tcl and then we will run it as, tclsh return.tcl
puts [set pi 3.14];
puts [set pi];
puts [puts "A string"];
puts "[set x 5]";
puts {[set y 3]};
puts "\[set z 6\]";
In the first line, we are setting a value 3.14 to a variable pi and displaying the value returned by the set command. In the second line, we are just displaying the value returned by the set command.
Next, 'A string' is printed and cursor moves to the next line but puts doesn't return any value, so nothing is being displayed. Command execution using [] works when enclosed inside "" but not {}. So we see 5 and [set y 3].
In the last line, [ has been escaped, so set command is not executed and [set z 6] is being displayed.
We can also assign a value to multiple variables using [] e.g. set a [set temp [set pi 3.14]].
Happy reading and thank you. Meet you soon.
Comments
Post a Comment