The traditional starting place for a tutorial is the classic "Hello, World" program. Once you can print out a string, you're well on your way to using Tcl for fun and profit!
The command to output a string in Tcl is the puts command.
A single word after the puts command will be printed to the standard output device. Normally the next text will be printed on the next line. The two commands
puts Hello, puts World
produce:
Hello, World
To have them printed on the same line, use the -nonewline option:
puts -nonewline Hello, puts World
But as the string we want to print has more than one word, it will be easier to enclose the string in double quotes or braces ({}). A set of words enclosed in quotes or braces is treated as a single unit, while words separated by whitespace are treated as multiple arguments to the command:
puts "Hello, World"
Quotes and braces can both be used to group several words into a single unit. However, they actually behave differently. Next you'll start to learn some of the differences between their behaviors.
Note: in Tcl, single quotes (') are not significant, as they are in other programming languages such as C, Perl and Python.
Many commands in Tcl (including puts) can accept multiple arguments. If a string is not enclosed in quotes or braces, the Tcl interpreter will consider each word in the string as a separate argument, and pass each individually to the command.
A full command in Tcl is the command name - the first word, in the example above puts - followed by a list of words terminated by a newline or semicolon. Tcl comments are indicated by a # at a position where Tcl expects a new command (i.e., following a newline or semicolon), and continue until the end of the line.
puts "Hello, World - In quotes" ;# Note: comment after a command. # This is a comment at beginning of a line puts {Hello, World - In Braces} puts "This is line 1"; puts "this is line 2" puts "Hello, World; - With a semicolon inside the quotes" # Words don't need to be quoted unless they contain white space: puts HelloWorld
Hello, World - In quotes Hello, World - In Braces This is line 1 this is line 2 Hello, World; - With a semicolon inside the quotes HelloWorld
As stated, comments may appear wherever a new command can be expected. The following is a syntactic error - there should be a semicolon before the hash-sign:
puts {Bad comment syntax example} # *Error* - no semicolon!
wrong # args: should be "puts ?-nonewline? ?channelId? string" while executing "puts {Bad comment syntax example} # *Error* - no semicolon!" (file "example.tcl" line 1)
This also illustrates the habit of Tcl to inform you about (run-time) errors. This will be explained later.