:TITLE: Simple Text Output
;#
;# RCSID: $Header: /cvsroot/tcl/tcltutorial/original/Tcl1.lsn,v 1.1 2004/11/04 16:01:13 davidw Exp $
;# Copyright (c) 1995 Clif Flynt
;# 9300 Fleming Rd.
;# Dexter, MI 48130
;# clif@cflynt.com
;# See file "NOTICE" for licensing terms.
;#
:LESSON_TEXT_START_LEVEL 0:
Puts is the output command for text.
A single unit of text after the puts command will be
printed to the standard output device (in this case, the lower window).
The default behavior is to append a newline to the string.
Strings of multiple words must be grouped with quotes or braces. The effect of quotes and braces isn't quite identical. See the next lesson for the first intro to Quoting Hell.
See later lessons on puts for the options to puts.
A command in Tcl is a list of words terminated by a newline or semicolon.
Comments in Tcl have a # in the position where a command could be.
In order to put a comment on a line after a command, it must be
preceded by a semi-colon, to terminate the command.
See File I/O if you are interested in sending output to a file.
See format if you want to use formatted output. :TEXT_END: :LESSON_TEXT_START_LEVEL 1: The traditional starting place for programming discussions is the classic "Hello, World" program. Once you can print out a string, you are prepared to conquer the world (of programming).
The command to output a string in Tcl is the 'puts' command.
A single unit of text after the puts command will be printed to the
standard output device (in this case, the lower window). The default
behavior is to 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.
If the string has more than one word, you must enclose the string in 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. Quotes and braces can both be used to group several words into a single unit. However, they actually behave differently. In the next lesson you'll start to learn some of the differences between their behaviors.
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 puts command. The puts command
will try to evaluate the words as optional arguments.
This will probably result in an error.
A command in Tcl is a list of words terminated by a newline or
semicolon. Comments in Tcl have a # in the position where a command
could be. Some commands in Tcl may contain multiple words, 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.
:TEXT_END:
:LESSON_TEXT_START_LEVEL 2: 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" ).Placing
multiple words within braces or quotes tells the Tcl
interpreter to consider them as a single unit.
In the simple examples in this lesson, the quotes and braces will work the same. However, there are differences between them, which we will explore in the next lesson.
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 puts command. The puts command
will try to evaluate the words as optional arguments.
This will probably result in an error.
For example, 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 string in Tcl is simply a list of words. The first word is the command to be executed, the following words are the arguments. The command string is terminated by a newline or a semicolon. The newline is the character sent to the computer when you press the ENTER key. The newline may be a carriage return, a line feed, or both.
A comment is also a list of words. To distinguish it as a comment to the Tcl interpreter, begin the comment string with the # symbol. This tells the Tcl interpreter not to parse or execute the next words as a command.
A comment can be put on a line by itself, or after a command string on the same line. In the latter case, the comment string must be preceded by a semicolon, to terminate the preceeding command. Without the semicolon, the Tcl interpreter will consider the # symbol as an argument to the command, resulting in an error.
My personal style is to always use the ;# for comments. This form is always correct.
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.
:TEXT_END:
:CODE_START: ;# This is a single line comment puts "Hello, World - In quotes" ;# This is a comment after the command. puts {Hello, World - In Braces} # Add a semicolon to make this OK ;# Because of the semicolon this is two puts commands on one line. puts "This is line 1"; puts "this is line 2" ;# The semicolon inside quotes does not end the command puts "Hello, World; - With a semicolon inside the quotes" :TEXT_END: