Lesson 2: Speaking the Computer's Language
I mentioned earlier that the only thing a computer understands is numbers. Have you ever met someone from another country who spoke a different language? If this person didn't speak English then you need the help of an interpreter. An interpreter is a person who speaks both English and one or many other languages. You would speak to the person who didn't understand English and the interpreter would translate what you said into the person's native language.
A computer has several interpreters for several different computer languages. We are using a scripting language called Tcl, pronounced tickle. It is an acronym for Tool Command Language or Tcl (say tickle), as it is commonly called. Tcl is a language that has rules about how we talk to the computer. The Wish Shell application is available on many different computer platforms, such as Microsoft Windows and Unix. If you are using the batteries-included Tcl package for Mac OS X, the Wish Shell app is in your Applications->Utilities folder. Scroll down to find the Wish Shell app, as shown in Figure 6. You can activate the interpreter by double-clicking the Wish Shell app.
Let's take a look at what the interpreter is and how it is used. Start the Wish Shell application and you'll see two windows. One is a graphical window, with the name Wish Shell on the title bar and the other is the interpreter window. The interpreter window has the name Console on its title bar (see Figure 7).
The console window is where we will access the Tcl interpreter. The console window has a % sign. This percent sign is called a command line prompt. We type in our Tcl commands at this prompt. Let's see if we can say hello to our computer. You can type in the following command at the console prompt. Remember the language has special rules and must be typed in as shown. Type the word "puts," then a space, and then in quotes, "hello!". Try this example in the console window.
Figure 8 shows what your console window should display if you typed the command in correctly.
We told the computer to 'put' to its console, with the Tcl command,
the message 'hello!'. The message to be displayed on the console is
enclosed in quotes. When the computer displays your message you'll notice
there are no quotes.
You have just run a simple command on the Tcl interpreter. Congratulations to you. You just executed your very first computer program.
Now what happens if you typed in the command wrong? Wanna see the computer get mad at you? The example shown in Figure 9 shows what happens when we don't type in the command and use a space. Spaces are important in Tcl scripting for the computer to understand what is a command and what is the command's data, called an argument. Repeat after me, "The space separates the command from the arguments."
When the computer is upset by what you typed into the console interpreter it will display an error message. An error message is a helpful way of telling you, the programmer, that you have offended the computer. Don't worry, most computers rarely hold a grudge for long.
Lesson 3: Tcl Basics
Let's look at a few basic concepts of the Tcl language. Suppose we want to ask the computer to remember a magic number for us. Using Tcl we would type in the following command:
set magic_number 2
set has a special meaning in Tcl -- it instructs the computer
to store the data. This is called setting a variable. In order for the
computer to store the data it needs a special name for the variable, so that the computer can remember where to find the stored data. In our case,
we named the data variable,
magic_number. Let's take a closer look at
this command. The Tcl command is
set, followed by a space, then the variable
name, followed by a space and the data assigned to the variable. Remember
our mantra, Tcl uses space-separated commands and arguments.
- Tcl command: set
- variable name and data assigned to variable. Variable name: magic_number Data assigned to variable name: 2
In order for the computer to retrieve and display the data we need to type the following:
When you are telling the computer (using the Tcl language) to retrieve data you must type the $ sign in front of the variable name you used for your data. Experimenting at the console, you would see the results shown in Figure 10.
set magic_number 2 puts $magic_number