Book Image

Tcl/Tk 8.5 Programming Cookbook

Book Image

Tcl/Tk 8.5 Programming Cookbook

Overview of this book

With Tcl/Tk, you can create full-featured cross-platform applications in a simple and easy-to-understand way without any expensive development package; the only tools required are a simple text editor and your imagination. This practical cookbook will help you to efficiently interact with editors, debuggers, and shell type interactive programs using Tcl/Tk 8. This cookbook will comprehensively guide you through practical implementation of Tcl/Tk 8.5 commands and tools. This book will take you through all the steps needed to become a productive programmer in Tcl/Tk 8. Right from guiding you through the basics to creating a stand-alone application, it provides complete explanation of all the steps along with handy tips and tricks. The book begins with an introduction to the Tcl shell, syntax, variables, and programming best practices in the language. It then explores procedures and the flow of events with control constructs followed by advanced error trapping and recovery. From Chapter 4, a detailed study of string expressions and handling enables you to handle various string functions and use lists to expand the string functionality. The book then discusses in-depth the Tcl Dictionary and how to utilize it to store and retrieve data. File operations and Tk GUI handling are covered extensively along with a developing a real-world address book application to practice the concepts learned.
Table of Contents (20 chapters)
Tcl/Tk 8.5 Programming Cookbook
Credits
About the Author
Acknowledgment
About the Reviewers
www.PacktPub.com
Preface

Variables


As with all the programming languages, it is the variable that allows for true flexibility and usability. Tcl differs from some scripted languages, as, there is no need to implicitly declare the variable type. For example a variable of "3" will be stored within Tcl with the same internal representation, as if it have been defined as the integer 3. If the variable is then used in a calculation, Tcl will then convert it to an integer for computation. This is referred to as shimmering in Tcl.

Basic variable commands

Variable command

Explanation

global var

This command is used to declare a global variable. It is only required within the body of a procedure.

incr var value

This command will increment the value stored in var by the value provided. Value must contain an integer. If no value is passed, the command defaults to increase the value by one (1).

set var value

This command sets var to the value provided. Conversely, the value may contain a Tcl command, the results of which will be utilized as the final value. The Command must be enclosed within square braces.

unset var var var

The unset command deletes one or more variables. If the nocomplain flag is passed as the first argument, all the errors are suppressed. Variable names are NOT comma delimited.

In the following examples, we will create a variable with an integer value of 3, increment that value, and then delete the variable.

Getting Ready

To complete the following examples, launch your Tcl Shell as appropriate, based on your operating platform.

How to do it…

For setting a variable, enter the following command:

% set x 3
3

How it works…

The set command returns 3 to confirm that the value was set correctly.

There's more…

Enter the following command:

% incr x 3
6

The incr command has increased the value of x by 3 and returned 6.

Unsetting a variable

Enter the following command:

% unset x
%

The unset command deletes the variable x and simply returns to the command prompt.

If the named variable does not exist, an error will be generated, as shown in the following example:

% unset y
can't unset "y": no such variable

To avoid error reporting for variables, include the nocomplain switch, as illustrated here:

% unset nocomplain y
%

In this instance, the unset command has ignored the error and simply returned to the command line. This is invaluable when passing a list of variables to unset to ensure non-existing variables do not generate an error. Additionally, you should insert -- (double minus, no spaces) after all the options, in order to remove a variable that has the same name as the many options.