When Kernighan and Richie wrote the first book about the language C, they used a simple program that printed out the string "Hello World" as the first example program. Over the years, the traditional starting place for programming discussions has become the "Hello, World" program. Once you can print out a string, you've mastered the mechanics of getting something to run without delving into the intricacies of getting a real program to work the way you expect it to work.
The command to output a string in Tcl is the 'puts
' command.
The puts
command will print a single unit of text to the standard
output device.
In the tutorial package, puts
is redirected to print to the bottom
screen. By default, puts
will move the cursor to the beginning of
the next line after printing out a line of text, instead of leaving the
cursor at the end of the text.
A single unit of text is either a single word (for example:
puts OUCH!
), or several words within quotes or braces. (for example:
puts "Two Words"
).
Tcl can consider a string of words as a single unit. To tell the Tcl interpreter that several words should be considered as one unit, place the words within quotes or braces. The quotes and braces work the same in this simple example. In the next lesson we'll start learning the differences between them.
If you do not enclose a string in braces or quotes, then the string will
be interpreted as several arguments instead of as one argument. Many
commands in Tcl (including puts
)can accept multiple
arguments. If the string is not enclosed in quotes or braces, then the
separate words in the command will interpreted as separate arguments, by the
Tcl interpreter, which will pass the argument to the puts
command.
The puts
will try to evaluate these words as optional arguments,
which will probably result in an error.
If you typed puts Two Words
without the quotes around
Two Words
, the puts
command would try to
interpret the first word (Two) in a special way, and would give you an
error.
A command in Tcl is just a list of words. The end of the list is marked by a newline or a semicolon. The newline is the character that is sent to the computer when you press the ENTER key on your keyboard. The newline may be a carriage return or a line feed, or both.
The first word in a list is the command that the Tcl interpreter will
execute, and following words are the arguments. If the first symbol
in a command is a #
, then the line is a comment, and
won't be parsed or executed. A comment can be put on a line by itself,
or after a command. However, since some commands in Tcl may contain multiple
words the interpreter can't distinguish between a
#
that starts a comment, and one that is an argument. So,
in order to put a comment on a line after a command the comment must be
preceded by a semi-colon, to terminate the command.
My personal style is to always use the ;# for comments.
For now, run the example, and try modifying the string a little to get
familiar with the tutorial system, and running scripts. When you are
comfortable with the puts
command, click Next Lesson to continue.