|
Example statment:
set x "Hello World" x = (Hello World) // The brackets ( ) is not present // and I used it to denote that // Hello World is considered // as one argument |
Evaluation:
puts $x Phase 1: substitute the content of each variable ==> puts (Hello World) Phase 2: evaluate (execute) the resulting command ==> Hello World This is the result of the evaluation and is returned by the Tcl command (.e.g., result can be assigned to a variable) |
Keep in mind that the substitution takes place before the command is passed to the Tcl evaluation engine
|
set x Hello puts $x Phase 1: substitute the content of each variable ==> puts Hello This is the actual Tcl command this will be executed |
puts \$x // Prints $x |
When the backslash (\) character is the last character on a line, the Tcl command/statement is continued on the next line |
|
This is kinda like a compound statement in C++ or Java where you "consider" multiple statements between braces { ... } as one songle statement.
puts Hello World // Error, "puts" takes only 1 argument // There are two argements in the command // NOTE: puts with 2 argument will write // output to an output file !! // So you will get an "unexpected error": // can not find channel named "Hello" |
|
Characters within braces { ... } are passed to a command unsubstitued (i.e., verbatim).
Example:
set x ABC puts {$x} // prints: $x // Reason: // {$x} --> (no substitution) --> $x // puts receives $x and will print $x |
When the
last character of the line is a backslash (\),
it is still interpreted as a
line continuation character.
Example:
set x ABC puts {Hello \ $x} // prints: Hello $x |
I.e., the end-of-line is not treated as the end of a Tcl command.
(Or else the Tcl interpreter cannot detect opening brace { and think the line has ended and give you a syntax error !)
Correct: set x { Hello World } |
(BTW, I hate this feature...)
Examples:
"foo{bar" "foo{b}ar" |
A brace character { or } inside a quoted string is treated as an a regular character with no special meaning. |
set x ABC puts "foo{$x" // Output: foo{ABC puts "foo{$x}" // Output: foo{ABC} |
Examples:
"foo{bar" "foo{b}ar" |
A quote character " inside a braced string is treated as an a regular character with no special meaning. |
Examples:
set x ABC puts {foo"$x"} // Output: foo"$x" |
set Z Albany set Z_LABEL "The Capitol of New York is: " puts "\n................. examples of differences between \" and \{" ................. examples of differences between " and { puts "$Z_LABEL $Z" The Capitol of New York is: Albany puts {$Z_LABEL $Z} $Z_LABEL $Z puts "\n....... examples of differences in nesting \{ and \" " ....... examples of differences in nesting { and " puts "$Z_LABEL {$Z}" The Capitol of New York is: {Albany} puts {Who said, "What this country needs is a good $0.05 cigar!"?} Who said, "What this country needs is a good $0.05 cigar!"? puts "\n................. examples of escape strings" ................. examples of escape strings puts {There are no substitutions done within braces \n \r \x0a \f \v} There are no substitutions done within braces \n \r \x0a \f \v puts {But, the escaped newline at the end of a\ string is still evaluated as a space} But, the escaped newline at the end of a string is still evaluated as a space |
In other words: you must recurse.
[cmd arguments] |
will:
(Apply everything what you have learned to evaluate this command)
The command: expr {3 + 4} // will returns: 3 + 4 = 7 Then: puts [expr {3 + 4}] // Outputs: 7 // NOTE: // Puts receives 7, NOT [expr {3 + 4}] !! Compare: puts {[expr {3 + 4}]} // Outputs: [expr {3 + 4}] |