Practical Programming in Tcl and Tk. Brent Welch. DRAFT, January 13, Updated for Tcl and Tk THIS IS NOT THE PUBLISHED TEXT. THE INDEX . Just how can? Do you think that you don't need sufficient time to go with downloading book Tcl And Tk. Programming For The Absolute Beginner By Kurt Wall. Covering the newest versions of Tcl and Tk, Tcl and Tk Programming for the Absolute Beginner teaches beginning and novice programmers concepts such as.
|Language:||English, Indonesian, Dutch|
|Genre:||Politics & Laws|
|ePub File Size:||27.75 MB|
|PDF File Size:||19.68 MB|
|Distribution:||Free* [*Registration needed]|
[PDF] DOWNLOAD Tcl and Tk Programming for the Absolute Beginner by Kurt Wall [PDF] DOWNLOAD Tcl and Tk Programming for the. Tcl Tk Programming Cookbook · Read more Практическое программирование на Tcl и Tk · Read more Tk Programming for the Absolute Beginner. It is a guide to the Tcl/Tk programming language and GUI toolkit. UNREGISTERED VERSION OF CHM TO PDF CONVERTER By THETA- SOFTWARE Chapter This book is meant to be useful to the beginner in Tcl as well as the expert. I use the following trick to avoid putting absolute file names into my scripts.
Part I: Tcl Basics Part I introduces the basics of Tcl. Everyone should read Chapter 1, which describes the fundamental properties of the language. Tcl is really quite simple, so beginners can pick it up quickly. The experienced programmer should review Chapter 1 to eliminate any misconceptions that come from using other languages.
You may want to look at this chapter first so you can try out the examples as you read Chapter 1. Chapter 3 presents a sample application, a CGI script, that implements a guestbook for a Web site. The example uses several facilities that are described in detail in later chapters. The goal is to provide a working example that illustrates the power of Tcl.
The rest of Part I covers basic programming with Tcl. Simple string processing is covered in Chapter 4.
Download Practical Programming in Tcl and Tk 4th Edition
Tcl lists, which share the syntax rules of Tcl commands, are explained in Chapter 5. Control structure like loops and if statements are described in Chapter 6. Chapter 7 describes Tcl procedures, which are new commands that you write in Tcl. Chapter 8 discusses Tcl arrays. Arrays are the most flexible and useful data structure in Tcl. These facilities let you build Tcl scripts that glue together other programs and process data in files. After reading Part I you will know enough Tcl to read and understand other Tcl programs, and to write simple programs yourself.
Chapter 1. It describes the basic mechanisms used by the Tcl interpreter: It touches lightly on the following Tcl commands: Tcl is a string-based command language. The language has only a few fundamental constructs and relatively little syntax, which makes it easy to learn. The Tcl syntax is meant to be simple. A simpler glue makes the job easier.
In addition, Tcl is interpreted when the application runs. The interpreter makes it easy to build and refine your application in an interactive manner. A great way to learn Tcl is to try out commands interactively. This chapter takes you through the basics of the Tcl language syntax. Even if you are an expert programmer, it is worth taking the time to read these few pages to make sure you understand the fundamentals of Tcl. The basic mechanisms are all related to strings and string substitutions, so it is fairly easy to visualize what is going on in the interpreter.
The model is a little different from some other programming languages with which you may already be familiar, so it is worth making sure you understand the basic concepts. A command does something for you, like output a string, compute a math expression, or display a widget on the screen.
Tcl casts everything into the mold of a command, even programming constructs like variable assignment and procedure definition. Tcl adds a tiny amount of syntax needed to properly invoke commands, and then it leaves all the hard work up to the command implementation. The basic syntax for a Tcl command is:. The command is either the name of a built-in command or a Tcl procedure. White space i.
Tcl does not interpret the arguments to the commands except to perform grouping, which allows multiple words in one argument, and substitution, which is used with programming variables and nested command calls. The behavior of the Tcl command processor can be summarized in three basic steps:.
Argument grouping. Value substitution of nested commands, variables, and backslash escapes. Command invocation. It is up to the command to interpret its arguments.
This model is described in detail in this Chapter. Hello, World! Example The "Hello, World! There are two points to emphasize: Arguments are interpreted by the command. In the example, stdout is used to identify the standard output stream.
Also, stderr is used to identify the standard error output, and stdin is used to identify the standard input. Curly braces are used to group words together into a single argument. The puts command receives Hello, World! The braces are syntax for the interpreter, and they get stripped off before the value is passed to the command. Braces group all characters, including newlines and nested braces, until a matching brace is found. Tcl also uses double quotes for grouping. Grouping arguments will be described in more detail later.
Variables The set command is used to assign a value to a variable. It takes two arguments: The first is the name of the variable, and the second is the value. Variable names can be any length, and case is significant. In fact, you can use any character in a variable name. The interpreter will create the variable when it is first assigned a value.
The value of a variable is obtained later with the dollar-sign syntax, illustrated in Example The second set command assigns to variable b the value of variable var. The use of the dollar sign is our first example of substitution. The actual implementation of substitution is more efficient, which is important when the value is large.
Command Substitution The second form of substitution command substitution. The Tcl interpreter takes everything between the brackets and evaluates it as a command. It rewrites the outer command by replacing the square brackets and everything between them with the result of the nested command.
This is similar to the use of backquotes in other shells, except that it has the additional advantage of supporting arbitrary nesting of commands. This command returns the length of the string foobar. The string command is described in detail starting on page The nested command runs first. Then, command substitution causes the outer command to be rewritten as if it were:. If there are several cases of command substitution within a single command, the interpreter processes them from left to right.
As each right bracket is encountered, the command it delimits is evaluated. This results in a sensible ordering in which nested commands are evaluated first so that their result can be used in arguments to the outer command.
Math Expressions The Tcl interpreter itself does not evaluate math expressions. Tcl just does grouping, substitutions and command invocations.
Browse more videos
The expr command is used to parse and evaluate math expressions. The math syntax supported by expr is the same as the C expression syntax. The expr command deals with integer, floating point, and boolean values. Logical operations return either 0 false or 1 true.
Integer values are promoted to floating point values as needed. Octal values are indicated by a leading zero e. Hexadecimal values are indicated by a leading 0x. Scientific notation for floating point numbers is supported. A summary of the operator precedence is given on page You can include variable references and nested commands in math expressions.
The following example uses expr to add the value of x to the length of the string foobar. The expression evaluator supports a number of built-in math functions.
For a complete listing, see page Example computes the value of pi:. The implementation of expr is careful to preserve accurate numeric values and avoid conversions between numbers and strings. However, you can make expr operate more efficiently by grouping the entire expression in curly braces. The explanation has to do with the byte code compiler that Tcl uses internally, and its effects are explained in more detail on page For now, you should be aware that these expressions are all valid and run faster than the examples shown above:.
Backslash Substitution The final type of substitution done by the Tcl interpreter is backslash substitution. This is used to quote characters that have special meaning to the interpreter. For example, you can specify a literal dollar sign, brace, or bracket by quoting it with a backslash. As a rule, however, if you find yourself using lots of backslashes, there is probably a simpler way to achieve the effect you are striving for.
In particular, the list command described on page 65 will do quoting for you automatically. The second set command in the example illustrates an important property of Tcl. The value of dollar does not affect the substitution performed in the assignment to x. In other words, the Tcl parser does not care about the value of a variable when it does the substitution.
In general, you do not have to worry about the value of variables until you use eval, which is described in Chapter You can also use backslash sequences to specify characters with their Unicode, hexadecimal, or octal value:. Table 11 on page 20 summarizes backslash substitutions. A common use of backslashes is to continue long commands on multiple lines. This is necessary because a newline terminates a command. The backslash in the next example is required; otherwise the expr command gets terminated by the newline after the plus sign.
There are two fine points to escaping newlines. First, if you are grouping an argument as described in the next section, then you do not need to escape newlines; the newlines are automatically part of the notCONVERTER terminate the command. In other words, the backslash-newline sequence also consumes all the leading white space on the next line. Grouping with Braces and Double Quotes Double quotes and curly braces are used to group words together into one argument.
The difference between double quotes and curly braces is that quotes allow substitutions to occur in the group, while curly braces prevent substitutions. This rule applies to command, variable, and backslash substitutions. Example Grouping with double quotes vs. In the second command of Example , the Tcl interpreter does variable and command substitution on the second argument to puts.
In the third command, substitutions are prevented, so the string is printed as is. In practice, grouping with curly braces is used when substitutions on the argument must be delayed until a later time or never done at all.
Examples include loops, conditional statements, and procedure declarations. Double quotes are useful in simple cases like the puts command previously shown. Another common use of quotes is with the format command. This is similar to the C printf function. The first argument to format is a format specifier that often includes special characters like newlines, tabs, and spaces. The easiest way to specify these characters is with backslash sequences e. The backslashes must be substituted before the format command is called, so you need to use quotes to group the format specifier.
Here format is used to align a name and a value with a tab. For more information about the format command, see page Square Brackets Do Not Group The square bracket syntax used for command substitution does not provide grouping. Instead, a nested command is considered part of the current group. In the command below, the double quotes group the last argument, and the nested command is just part of that group.
If an argument is made up of only a nested command, you do not need to group it with doublequotes because the Tcl parser treats the whole nested command as part of the group. Grouping before Substitution The Tcl parser makes a single pass through a command as it makes grouping decisions and performs string substitutions.
Grouping decisions are made before substitutions are performed, which is an important property of Tcl. This means that the values being substituted do not affect grouping because the grouping decisions have already been made. The following example demonstrates how nested command substitution affects grouping. A nested command is treated as an unbroken sequence of characters, regardless of its internal structure. It is included with the surrounding group of characters when collecting arguments for the main command.
The white space inside the nested command is ignored for the purposes of grouping the argument. By the time Tcl encounters the left bracket, it has already done some variable substitutions to obtain:. When the left bracket is encountered, the interpreter calls itself recursively to evaluate the nested command. Finally, the result of expr is substituted for everything from the left bracket to the right bracket.
The puts command gets the following as its second argument:. The point of this example is that the grouping decision about puts's second argument is made before the command substitution is done. Even if the result of the nested command contained spaces or other special characters, they would be ignored for the purposes of grouping the arguments to the outer command. Grouping and variable substitution interact the same as grouping and command substitution.
Spaces or special characters in variable values do not affect grouping decisions because these decisions are made before the variable values are substituted. The double quotes are used for grouping in this case to allow the variable and command substitution on the argument to puts.
Grouping Math Expressions with Braces It turns out that expr does its own substitutions inside curly braces. This is explained in more detail on page This means you can write commands like the one below and the substitutions on the variables in the expression still occur:.
More Substitution Examples If you have several substitutions with no white space between them, you can avoid grouping with quotes. The following command sets concat to the value of variables a, b, and c all concatenated together:. In general, you can place a bracketed command or variable reference anywhere. The following computes a command name:.
Procedures Tcl uses the proc command to define procedures. Once defined, a Tcl procedure is used just like any of the other built-in Tcl commands. The basic syntax to define a procedure is:. The first argument is the name of the procedure being defined. The second argument is a list of parameters to the procedure. The third argument is a command body that is one or more Tcl commands. The procedure name is case sensitive, and in fact it can contain any characters.
Procedure names and variable names do not conflict with each other. As a convention, this book begins procedure names with uppercase letters and it begins variable names with lowercase letters. Good programming style is important as your Tcl scripts get larger.
Tcl coding style is discussed in Chapter The Diag procedure defined in the example computes the length of the diagonal side of a right triangle given the lengths of the other two sides. The sqrt function is one of many math functions supported by the expr command.
The variable c is local to the procedure; it is defined only during execution of Diag. Variable scope is discussed further in Chapter 7. It is not really necessary to use the variable c in this example. The procedure can also be written as:.
The return command is used to return the result of the procedure. The return command is optional in this example because the Tcl interpreter returns the value of the last command in the body as the value of the procedure. So, the procedure could be reduced to:. Note the stylized use of curly braces in the example. The curly brace at the end of the first line starts the third argument to proc, which is the command body. In this case, the Tcl interpreter sees the opening left brace, causing it to ignore newline characters and scan the text until a matching right brace is found.
Double quotes have the same property. They group characters, including newlines, until another double quote is found. The result of the grouping is that the third argument to proc is a sequence of commands. When they are evaluated later, the embedded newlines will terminate each command.
The other crucial effect of the curly braces around the procedure body is to delay any substitutions in the body until the time the procedure is called. For example, the variables a, b, and c are not defined until the procedure is called, so we do not want to do variable substitution at the time Diag is defined. The proc command supports additional features such as having variable numbers of arguments.
These are described in detail in Chapter 7. A Factorial Example To reinforce what we have learned so far, below is a longer example that uses a while loop to compute the factorial function:.
The semicolon is used on the first line to remind you that it is a command terminator just like the newline character. The while loop is used to multiply all the numbers from one up to the value of x.
The first argument to while is a boolean expression, and its second argument is a command body to execute. The while command and other control structures are described in Chapter 6. The same math expression evaluator used by the expr command is used by while to evaluate the boolean expression.
There is no need to explicitly use the expr command in the first argument to while, even if you have a much more complex expression. The loop body and the procedure body are grouped with curly braces in the same way. The opening curly brace must be on the same line as proc and while. If you like to put opening curly braces on the line after a while or if statement, you must escape the newline with a backslash:.
Curly braces around the boolean expression are crucial because they delay variable substitution until the while command implementation tests the expression. The following example is an infinite loop:. You can avoid these kinds of errors by adopting a consistent coding style that groups expressions with curly braces: This bug is fixed in the 8. Ironically, Tcl 8. The incr command is used to increment the value of the loop variable i.
This is a handy command that saves us from the longer command:. The incr command can take an additional argument, a positive or negative integer by which to change the value of the variable. Using this form, it is possible to eliminate the loop variable i and just modify the parameter x. The loop body can be written like this:. Example shows factorial again, this time using a recursive definition.
A recursive function is one that calls itself to complete its work. Each recursive call decrements x by one, and when x is one, then the recursion stops. More about Variables The set command will return the value of a variable if it is only passed a single argument. It treats that argument as a variable name and returns the current value of the variable. The dollar-sign syntax used to get the value of a variable is really just an easy way to use the set command.
Example shows a trick you can play by putting the name of one variable into another variable:. This is a somewhat tricky example. Then, the set command returns the value of var, which is the value of var. Nested set commands provide another way to achieve a level of indirection. The last set command above can be written as follows:. Using a variable to store the name of another variable may seem overly complex.
However, there are some times when it is very useful. There is even a special command, upvar, that makes this sort of trick easier.
The upvar command is described in detail in Chapter 7. Funny Variable Names The Tcl interpreter makes some assumptions about variable names that make it easy to embed variable references into other strings.
By default, it assumes that variable names contain only letters, digits, and the underscore. If the variable reference is not delimited by punctuation or white space, then you can use curly braces to explicitly delimit the variable name e. You can also use this to reference variables with funny characters in their name, although you probably do not want variables named like that.
If you find yourself using funny variable names, or computing the names of variables, then you may want to use the upvar command. Any number of variable names can be passed to the unset command.
However, unset will raise an error if a variable is not already defined, unless the -nocomplain is given. Use -- to unset a variable named -nocomplain.
Using info to Find Out about Variables The existence of a variable can be tested with the info exists command. For example, because incr requires that a variable exist, you might have to test for the existence of the variable first. More about Math Expressions This section describes a few fine points about math in Tcl scripts.
In Tcl 7. The expr command must convert its arguments from strings to numbers. It then does all its computations with double precision floating point values. The result is formatted into a string that has, by default, 12 significant digits.
Seventeen digits of precision are enough to ensure that no information is lost when converting back and forth between a string and an IEEE double precision number:. In Tcl 8. Even so, Tcl was not designed to support math-intensive applications. You may want to implement math-intensive code in a compiled language and register the function as a Tcl command as described in Chapter There is support for string comparisons by expr, so you can test string values in if statements.
You must use quotes so that expr knows to do string comparisons:. However, the string compare and string equal commands described in Chapter 4 are more reliable because expr may do conversions on strings that look like numbers. The issues with string operations and expr are discussed on page Expressions can include variable and command substitutions and still be grouped with curly braces.
This is because an argument to expr is subject to two rounds of substitution: Ordinarily this is not a problem because math values do not contain the characters that are special to the Tcl interpreter.
The second round of substitutions is needed to support commands like while and if that use the expression evaluator internally. Otherwise, your values may suffer extra conversions from numbers to strings and back to numbers. Not only is this process slow, but the conversions can lose precision in certain circumstances.
Practical Programming in Tcl and Tk
For example, suppose x is computed from a math function:. If you do this:.
In contrast, if you do this:. The expression will be more accurate and run more efficiently because no string conversions will be done. The story behind Tcl values is described in more detail in Chapter 47 on C programming and Tcl.
Comments Tcl uses the pound character, , for comments. Unlike in many other languages, the must occur at the beginning of a command. A that occurs elsewhere is not treated specially. An easy trick to append a comment to the end of a command is to precede the with a semicolon to terminate the previous command:.
One subtle effect to watch for is that a backslash effectively continues a comment line onto the next line of the script. In addition, a semicolon inside a comment is not significant. Only a newline terminates comments:.
The behavior of a backslash in comments is pretty obscure, but it can be exploited as shown in Example on page A surprising property of Tcl comments is that curly braces inside comments are still counted for the purposes of finding matching brackets. The motivation for this odd feature was to keep the original Tcl parser simpler. However, it means that the following will not work as expected to comment out an alternate version of an if expression:.
The previous sequence results in an extra left curly brace, and probably a complaint about a missing close brace at the end of your script! A technique I use to comment out large chunks of code is to put the code inside an if block that will never execute:.
Command arguments are separated by white space, unless arguments are grouped with curly braces or double quotes as described below. Braces nest. The interpreter includes all characters between the matching left and right brace in the group, including newlines, semicolons, and nested braces. The enclosing i. Grouping with double quotes, " ", allows substitutions.
The interpreter groups everything until another double quote is found, including newlines and semicolons. The enclosing quotes are not included in the group of characters. A double-quote character can be included in the group by quoting it with a backslash, e.
Grouping decisions are made before substitutions are performed, which means that the values of variables or command results do not affect grouping.
Square brackets, [ ], cause command substitution. Everything between the brackets is treated as a command, and everything including the brackets is replaced with the result of the command. Nesting is allowed. You can think of this as another form of substitution in which the backslash and the next character or group of characters are replaced with a new character. Substitutions can occur anywhere unless prevented by curly brace grouping. Part of a group can be a constant string, and other parts of it can be the result of substitutions.
Even the command name can be affected by substitutions. A single round of substitutions is performed before command invocation. The result of a substitution is not interpreted a second time.
This rule is important if you have a variable value or a command result that contains special characters such as spaces, dollar signs, square brackets, or braces. Because only a single round of substitution is done, you do not have to worry about special characters in values causing extra substitutions.
Fine Points A common error is to forget a space between arguments when grouping with braces or quotes. This is because white space is used as the separator, while the braces or quotes only provide grouping.
If you forget the space, you will get syntax errors about unexpected characters after the closing brace or quote. A double quote is only used for grouping when it comes after white space. This means you can include a double quote in the middle of a group without quoting it with a backslash.
This requires that curly braces or white space delimit the group. I do not recommend using this obscure feature, but this is what it looks like: When double quotes are used for grouping, the special effect of curly braces is turned off. Substitutions occur everywhere inside a group formed with double quotes. In the next command, the variables are still substituted: When double quotes are used for grouping and a nested command is encountered, the nested command can use double quotes for grouping, too.
Spaces are not required around the square brackets used for command substitution. For the purposes of grouping, the interpreter considers everything between the square brackets as part of the current group. The following sets x to the concatenation of two command results because there is no space between ] and [. Newlines and semicolons are ignored when grouping with braces or double quotes. They get included in the group of characters just like all the others.
The following sets x to a string that contains newlines: This is line two. This is line three. During command substitution, newlines and semicolons are significant as command terminators. If you have a long command that is nested in square brackets, put a backslash before the newline if you want to continue the command on another line. This was illustrated in Example on page 8.
A dollar sign followed by something other than a letter, digit, underscore, or left parenthesis is treated as a literal dollar sign. Table Hexadecimal specification of character code. Hexadecimal specification of a bit Unicode character value. Replaced with literal c if c is not one of the cases listed above. Multiply, divide, remainder. Add, subtract. Left shift, right shift.
Bitwise AND. Bitwise XOR. Logical OR.
If x then y else z. Built-in math functions acos x asin x atan x Arccosine of x. Arcsine of x. Arctangent of x. Cosine of x. Hyperbolic cosine of x. Exponential, ex. Greatest integral value less than or equal to x. Log base 10 of x. There are concepts in 18 groups on this diagram, double the number of Ruby concepts and almost 3 times more than in CoffeeScript , e.
Learning Sinatra read online brookendsolutions. However, the study is thorough enough to determine ballpark estimates on the general productivity levels of the languages.
Of the languages we're considering that were included in the study, Smalltalk, Eiffel, and Perl were the highest with a language level of Visual Basic was next highest on the list, at level Nat Biotechnol. The same is true about Perl and Unix: almost all Unix system calls are available directly from Perl Ruby Pocket Reference institutocrescer.
For the float example, we need both constants float, otherwise the arithmetic would be done in the higher type, that is double. For the long precision, just one of the constants in long double would force the arithmetic to use long double. Reference Guide Quickstudy: Computer By - Inc. Pivot Tables Charts Quick Study: Download [PDF] Books hapi. Brief Edition: Learn the easier Cash Basis Method of accounting used by contractors and other service based businesses to perform Strategic Planning for Social Media in Libraries: Voice X.
By - Kenneth R. Subtitle What s this? Download [PDF] D3. Download [PDF] Excel Volume 1 Excel Essentials By - M. Download [PDF] Exploring:The general syntax for HTML tags is:. Specify the visual for the main window. Probably not. Internationalization This bug is fixed in the 8. Substitutions occur everywhere inside a group formed with double quotes.
- THE BRONZE HORSEMAN PAULLINA SIMONS PDF
- PDF THE LOST SYMBOL DAN BROWN
- RS AGARWAL VERBAL AND NONVERBAL REASONING BOOK
- LADY ALMINA AND THE REAL DOWNTON ABBEY PDF
- THE ILIAD AND THE ODYSSEY PDF
- GONE WITH THE WIND EBOOK
- HANDE WEG VON DIESEM BUCH PDF
- FORCE FIREFOX TO PDF
- ELECTRICAL ENGINEERING BOOK SITES
- THE SECRET BOOK PDF TORRENT