-->

Previous | Table of Contents | Next

Page 515

will return the pid associated with $f, the file handle for the ps command that was opened.

If a file (or process) is opened in a mode that supports reading, then you can read from the file using the gets command. To process all the lines of a file, the following while command is often used:

while { [ gets $f line ] >= 0 }

This works because the gets command returns -1 when EOF is reached. In this case, the gets command reads in a line from the file handle $f and assigns the value to the variable line. In the body of the loop, $line can be accessed and manipulated.

If a file is opened for writing, then the puts command can be used to write output to the file. If the file handle $f corresponds to a file opened for writing, the command

puts $f "This is a line of text";

will write the string "This is a line of text" to the open file.

The only other file I/O command is the close command, which takes as its argument a file handle. To close the file you opened earlier, you would simply use

close $f;

It is probably a good idea to close any file handles that are open at the end of a program. Also, if the same file handle variable is to be reused several times in a program, it is a good idea to close it before the next open.

In addition to reading and writing from files, it is sometimes necessary to obtain information about files. tcl provides the file command to accomplish this. The file command's syntax is

file option filename

where filename is the name of the file to run the tests on and option is one of the following options.

The following options return true (1) or false (0) information about files:

executable True if the file is executable by the current user
exists True if the file exists
isdirectory True if the file is a directory
isfile True if the file is a regular file
owned True if the current user owns the file
readable True if the file is readable by the current user
writeable True if the file is writable by the current user

The following options return additional information about a file:

atime Returns the time the file was last accessed in seconds since Jan 1, 1970

Page 516

mtime Returns the time the file was last modified in seconds since Jan 1, 1970
size Returns the size of the file in bytes
readlink Returns the value of a symbolic link if the given file is a symbolic link
type Returns a string giving the type of the file

Procedures

Procedures are tcl's equivalent of functions in the C language. To create a procedure, the proc command is used, which has the following syntax:

proc procedure_name {arguments} {body}

The number of arguments is variable, and an empty argument list is specified by {}. body can contain any valid tcl statement and can be as long as required.

A simple procedure that takes no arguments is as follows:

proc test_proc {} { puts "procedure test"; }

To invoke this procedure, simply give its name:

test_proc;

You will get the output:

procedure test

A more realistic example is a file output procedure, which takes in as an argument a filename:

proc cat {filename} {

set f [open $filename r];

while { [ gets $f line ] >= 0 } {

puts $line;

}

close $f;

}

To invoke this procedure with /etc/passwd as its argument, use the following:

cat /etc/passwd

This prints out the contents of /etc/passwd.

Three important commands for use in procedures are return, global, and catch. The global command is used to give a procedure access to global variables, and the return command is used to return a value from a procedure. The catch command is useful for detecting errors and returning a failure value.

You can rewrite the cat procedure to be a little more robust by doing the following:


proc cat {filename} {

Page 517


set ret_code 0;

catch {

set f [open $filename r];

while { [ gets $f line ] >= 0 } {

puts $line;

}

close $f;

set ret_code 1;

}

return $ret_code;

}

This code demonstrates the use of both catch and return. If any parts of the procedure fail, it returns 0 (false), but if the cat is successful, it returns 1 (true). This information will be useful if cat is called with a process to execute as its argument.

The tk Toolkit

The tk toolkit is a toolkit that allows X Window graphical user interfaces (GUIs) to be written using the tcl scripting language. The tk toolkit adds to the tcl language by allowing the creation of GUI components called widgets. This section looks briefly at the available tk widgets and shows how to create them.

Introduction to Widgets

The basic method for creating a widget is

widget_type path option

where widget_type is one of the widget types given in the following list, path is a window pathname (usually starting with a dot, which is the name of the root window), and option is any option that the widget understands.

The tk toolkit defines the following widget types:

canvas Allows for drawing objects
entry Allows for the input of single line of text
frame Used to contain other widgets
listbox Displays a set of strings and allows for choosing one or more of them
menu Displays a menu bar and menu items
text Displays multiple lines of text
label Displays a single line of static text
button A widget that displays a clickable button
checkbutton Displays a checkable box
radiobutton Displays several mutually exclusive checkable boxes
scale Similar to a scrollbar that sets a value

Previous | Table of Contents | Next