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

Referencing files in Tcl


Tcl commands that accept filenames as arguments require that they be in one of three formats, depending on the platform in use. The platform in use is stored in the global TCL_platform array variable, created at the start of the program. Please note that to address issues of portability, you must manually manipulate the formats to ensure that they are annotated correctly.

These formats are absolute, relative, and volume-related.

File Formats

Explanation

Absolute

Absolute names are fully qualified and give a path to the file relative to a particular volume.

Relative

Relative filenames are unqualified and give the path to the desired file relative to the current working directory.

Volume-related

Volume-related filenames are partially qualified and either accepts the path relative to the current working directory on the current volume, or relative to the directory of a specified directory.

The following conventions are platform-specific annotations for both the directory structure and the specific filenames.

UNIX (UNIX, Linux and Mac OS X)

On the UNIX style platforms, Tcl uses path names, wherein the various components are separated by the slash (/) character. Multiple adjacent slashes are handled as a single occurrence. Trailing slashes are ignored completely. For example, passwd and passwd/ both refer to the file passwd in the current directory

Convention

Meaning

.

Special character that refers to the current directory

..

Special character that refers to the parent directory

/

Root directory

/etc/passwd

Absolute path to the file passwd in the directory etc

passwd

Relative path to the file passwd in the current directory

etc/passwd

Relative path to the file passwd in the directory etc from the current working directory

../passwd

Relative path to the file passwd in the parent directory

Windows

Tcl supports both drive-related and Universal Naming Convention (UNC) file naming conventions. Both the slash (/) and backslash (\) characters may be used as separators; however, care must be exercised when utilizing the backslash characters, as they can result in undesirable effects if the filename is not enclosed within quotes. Drive-related filenames consist of the optional drive letter followed by the absolute or relative path. UNC filenames follow the form of \\servername\sharename\path\file. The UNC filename must contain the server and share components, at least.

Convention

Meaning

.

Special character that refers to the current directory

..

Special character that refers to the parent directory

\\MyServer\MyShare\passwd

Absolute UNC path to the file passwd on server MyServer in the share MyShare

C:passwd

Volume related path to the file passwd in the current directory

C:\passwd

Absolute path to the file passwd in the root directory of the C drive

\passwd

Volume-related path to the file passwd in the root directory of the current volume.

etc\passwd

Volume-related path to the file passwd in the directory etc on the current volume.

In addition to the filename conventions listed in the preceding table, Tcl supports the Berkeley UNIX C Shell (csh) tilde (~) substitution. In the case of a filename with a preceding tilde, it will be interpreted by replacing the tilde with the current user's home directory. This is not platform-dependant.