Tcl Part I · Module 2 · The Tcl Execution Model
Words and Whitespace: Where One Argument Ends
In most languages, spacing is a matter of taste. In Tcl it decides how many arguments a command receives. This is a small idea and a short chapter, but it is the idea that makes the rest of the module necessary: once you accept that a space ends an argument, you immediately need a way to pass a value that contains a space. That is what quotes and braces are for, and the next few chapters are about them.
Foundation9 min readTclPart IExecution ModelWordsWhitespace
Part I · Module 2 · Chapter 2.3 · Words and Whitespace: Where One Argument Ends
1. Spacing Is Meaning
In C you can write f(a,b) or f( a, b ) and nothing changes. In Python you can add spaces around an operator freely.
In Tcl, a space between words ends an argument. So this:
puts hello thereis not puts given one message. It is puts given two arguments. And puts does not accept two arguments that way, so it fails — you saw exactly this error in Chapter 1.7.
Spacing is not style here. It is part of what you are saying.
2. The Measuring Instrument
Keep using the tool from Chapter 2.1. It reports how many words a command received:
proc show {args} {
puts "received [llength $args] word(s)"
set i 1
foreach w $args {
puts " word $i: $w"
incr i
}
}proc and llength are instruments, not lessons. Modules 10 and 6 teach them.
3. One Space or Several — It Makes No Difference
A common worry: does extra spacing create extra empty arguments?
show a b
show a b
show a breceived 2 word(s)
word 1: a
word 2: b
received 2 word(s)
word 1: a
word 2: b
received 2 word(s)
word 1: a
word 2: bAll three are the same command. A run of spaces separates words exactly as one space does, and spaces before the first word or after the last are ignored.
So you may line arguments up for readability without changing what the command receives. What you may not do is add or remove a space between two things you meant to be one.
4. The Problem This Creates
Now the consequence. Suppose a filename contains a space:
show my report.txtreceived 2 word(s)
word 1: my
word 2: report.txtThat is not one filename. It is two arguments, and any command expecting a single path will either fail or act on the wrong thing.
You cannot fix this with spacing, because the space is part of the data. You need a way to tell Tcl "these characters are one word, including the space". That is grouping, and Tcl has two forms of it:
show "my report.txt"
show {my report.txt}received 1 word(s)
word 1: my report.txt
received 1 word(s)
word 1: my report.txtBoth produce one word. They are not the same in general — they differ in what happens to $ and [ ] inside them — and Chapter 2.7, Chapter 2.8 and Chapter 2.9 are about that difference.
For now the point is narrower: grouping is how a space stops ending a word.
5. Grouping Also Works Inside a Group
Braces can hold more braces, and the inner ones do not end the outer group:
show {a {b c} d}received 1 word(s)
word 1: a {b c} dOne word still. Tcl matched the outer braces, and the inner pair came through as ordinary characters.
This matters later, because a body of code passed to a command is full of braces, and all of it has to arrive as a single word. Chapter 2.1 §5 showed that happening.
6. What a Variable's Spaces Do Not Do
Worth repeating once, because it is the fact people get wrong after learning §1:
set path "my report.txt"
show $pathreceived 1 word(s)
word 1: my report.txtThe value contains a space. The command still received one argument.
Spaces separate words in the source text. A space that arrives later, inside a substituted value, is just a character. That is Chapter 2.2 §5, and it is the half of this rule that shell users consistently expect to work the other way.
7. Common Mistakes
Passing a path without grouping it. show my report.txt is two arguments. If a filename might contain a space, group it.
Adding quotes around a variable "to be safe". Harmless, but unnecessary — §6. It suggests a shell habit rather than a Tcl reason.
Removing a space to make something one word. puts hellothere is one word, but it is also the wrong text. Group it instead.
Assuming an aligned column is extra arguments. It is not — §3. Align freely.
8. Exercises
Exercise 1 — predict, then measure. For each line, write down the word count before running it through show:
show one two three
show one two three
show "one two" three
show {one two three}
show one"two"threeThe last one is deliberately strange. Run it and explain what you get.
Exercise 2 — fix the filename. This line passes two arguments:
show quarterly report.csvProduce one argument in two different ways, and confirm both with show.
Exercise 3 — a value that looks split. Set a variable to a b c, pass it to show with no quotes, and predict the count before you run it. If you predicted 3, re-read §6.
Exercise 4 — the nested group. Predict the word count of show {outer {inner} outer}, then run it. Then predict show outer {inner} outer and run that too. Explain the difference in one sentence.
9. Remember
- An unquoted space ends a word, and therefore ends an argument.
- Runs of spaces behave like one, and leading or trailing spaces are ignored — align for readability freely.
- A value containing a space must be grouped with
"..."or{...}before it is passed. - Braces nest. An inner pair does not end the outer group.
- Spaces inside a substituted value do not split anything. Word boundaries come from the source.
- Quotes and braces both group, and differ in what they do to
$and[ ]. That is the next few chapters.
The next three chapters cover the three substitutions, starting with the one you have already been using.
Where this fits
Part of the Tcl curriculum.
