| 1.2 Interactive Execution |
|
|
Tcl, and its extension Tk, are interpreted. This means that to the
programmer it appears as if the computer speaks Tcl/Tk. Programmers can enter
commands and expect them to be carried out without any apparent need for
translation. By The Tcl interpreter is often named Running under Windows 95, you can start the tclsh?? interpreter by just clicking on tclsh?? which can be found from the Start button. The effect is to bring up what looks like a DOS window with a % sign as the prompt. Running under Unix, you will have to find out what the name for tclsh?? actually is and, possibly, what directory contains it. If you installed Tcl yourself, you will know this. Otherwise, ask your system administrator. Once you have the file name of the interpreter, you can execute it. The effect is the same as with Windows 95. The effect is to bring up a DOS window that is actually running the Tcl interpreter. The default prompt is again the % sign. Here is a sample session as it would appear under either Windows 95 or Unix. Characters appearing after a % sign have been typed by a Tcl user. The rest has been printed by the Tcl interpreter:
% are you there?
invalid command name "are"
% puts "Hellow Orld"
Hellow Orld
% proc say_hello {} { puts "Hellow Orld" }
% say_hello
Hellow Orld
% proc say_hello {} { return "Hellow Orld" }
% say_hello
Hellow Orld
From the tclsh?? interpreter, you can execute any command that Tcl knows
simply by typing it as shown above. Everything you type after the % prompt is
a The window in which the above activity takes place can be called the
The proc command shown in the example is used to define a new procedure named say_hello. After it has been defined, this procedure can be executed like any Tcl command. Because Tcl is interpreted, procedures can be defined and redefined at will. In fact, in this example, the procedure, say_hello, is implemented and executed twice. The first execution generates an empty return string which is not shown by the interpreter. What is shown is the output from a puts command. The second execution returns the same string. Because the return string is nonempty, the interpreter shows it. While using the tclsh?? interpreter interactively, it is possible to
execute Tcl statements that have been entered into a file. Tcl's
proc say_hello {} {
puts "Hellow Orld"
}
If hello is available in the current directory, then the following
session is possible with tclsh??.
% source hello % say_hello Hellow Orld You can enter a complete pathname instead of hello. This may not be necessary in Unix if you switch directories before starting the interpreter. In Windows 95, you will have to switch directories after starting the interpreter. Use Tcl's built-in cd command, for example, % cd "C:\Program Files\Tcl Scripts"The quotes are required because of the spaces in the pathname. When writing Windows pathnames for Tcl, you should replace every backslash "\" with a slash "/." Tcl will do the right thing when it passes the pathname through to Windows. You can terminate the Tcl interpreter in several ways. One way is to use
the Remark
The Tk interpreter is usually named With Tk, however, you see an extra window. This is called the
Tk's command window is not useful in production scripts. Its sole value is that it lets you experiment with Tk interactively. It is true that Tk's command window could be used like Tcl's command window to communicate with the user through UNIX/DOS-like input and output, but the point of Tk is to get away from such an interface. Figure 1.2a shows an example of an interactive use of Tk through its command window. The root window appears at the top of the figure and the command window at the bottom. The root window is named "wish." Since the appearance of a command window varies greatly between Unix and DOS, only the text from that window is shown. This text shows that two commands were executed. The first created a label with the text "Hellow Orld" and the second posted that label to the root window.
A slightly more complicated example is shown in Figure 1.2b. The example shows two additional commands. The first of these creates a button and the second posts that button to the root window. The user can "push" the button by clicking on it with the mouse. The button is set up so that when the user does this the root window vanishes and the interpreter stops executing.
|
Author's Home Page |
|
Order from Amazon. |