Tcl file command example
Namespaces are created using the namespace command. In the above program, you can see there is a namespace with a variable myResult and a procedure Add. This makes it possible to create variables and procedures with the same names under different namespaces. You can see in the previous namespace examples, we use a lot of scope resolution operator and it's more complex to use. We can avoid this by importing and exporting namespaces. You can remove an imported namespace by using forget subcommand.
Tcl supports file handling with the help of the built in commands open, read, puts, gets, and close. Tcl uses the open command to open files in Tcl. Opens an existing text file for reading purpose and the file must exist. This is the default mode used when no accessMode is specified. Opens a text file for writing, if it does not exist, then a new file is created else existing file is truncated. Opens a text file for writing in appending mode and file must exist.
Here, your program will start appending content in the existing file content. Opens a text file for reading and writing both. It first truncate the file to zero length if it exists otherwise create the file if it does not exist. It creates the file if it does not exist. The reading will start from the beginning, but writing can only be appended.
Any file that has been opened by a program must be closed when the program finishes using that file. In most cases, the files need not be closed explicitly; they are closed automatically when File objects are terminated automatically. When the above code is compiled and executed, it creates a new file input. Error handling in Tcl is provided with the help of error and catch commands. The syntax for each of these commands is shown below.
In the above error command syntax, message is the error message, info is set in the global variable errorInfo and code is set in the global variable errorCode. In the above catch command syntax, script is the code to be executed, resultVarName is variable that holds the error or the result. The catch command returns 0 if there is no error, and 1 if there is an error.
As you can see in the above example, we can create our own custom error messages. Similarly, it is possible to catch the error generated by Tcl. Tcl provides a number of built-in functions procedures for various operations. Functions for creating namespaces and packages. Each of the above except for math and system functions are covered in earlier chapters. Math and system built-in functions are explained below. Calculates if arg is a floating-point value, returns arg, otherwise converts arg to floating-point and returns the converted value.
Calculates the floating-point remainder of the division of x by y. If y is 0, an error is returned. Calculates if arg is an integer value of the same width as the machine word, returns arg, otherwise converts arg to an integer. Calculates a pseudo-random number between 0 and 1. The arg, which must be an integer, is used to reset the seed for the random number generator of rand.
Calculates integer value at least bits wide by sign-extension if arg is a bit number for arg if it is not one already. The "regexp" command is used to match a regular expression in Tcl. A regular expression is a sequence of characters that contains a search pattern. It consists of multiple rules and the following table explains these rules and corresponding use. Occurrences matches the range between digit1 and digit2 occurrences of previous regex expression.
Here, regex is the command. We will see about optional switches later. Patterns are the rules as mentioned earlier. Search string is the actual string on which the regex is performed. Full match is any variable to hold the result of matched regex result. Submatch1 to SubMatchn are optional subMatch variable that holds the result of sub match patterns. Let's look at some simple examples before diving into complex ones.
A simple example for a string with any alphabets. When any other character is encountered the regex, search will be stopped and returned. The following example shows how to search for multiple patterns.
This is example pattern for any alphabets followed by any character followed by any alphabets. It was developed as an extension to Tcl scripting language by John Ousterhout. Tk remained in development independently from Tcl with version being different to each other, before, it was made in sync with Tcl in v8. Generally, all Mac and Linux mac come with Tk pre-installed. In case, it's not available or you need the latest version, then you may need to install it. It is just a small program that enables you to type Tk commands and have them executed line by line.
It stops execution of a tcl file in case, it encounters an error unlike a compiler that executes fully. We will use this as first program, we run on the platform you choose. Run the downloaded executable to install the Tcl and Tk, which can be done by following the on screen instructions. Most Linux operating systems comes with Tk inbuilt and you can get started right away in those systems. Active Tcl community edition is free for personal use. Now, use the following commands to extract, compile and build after switching to the downloaded folder.
The list of special variables is listed below. The basic component of a Tk-based application is called a widget. A component is also sometimes called a window, since, in Tk, "window" and "widget" are often used interchangeably.
Tk is a package that provides a rich set of graphical components for creating graphical applications with Tcl. Tk provides a range of widgets ranging from basic GUI widgets like buttons and menus to data display widgets. The widgets are very configurable as they have default configurations making them easy to use.
Tk applications follow a widget hierarchy where any number of widgets may be placed within another widget, and those widgets within another widget. The main widget in a Tk program is referred to as the root widget and can be created by making a new instance of the TkRoot class.
The type here refers to the widget type like button, label, and so on. Arguments can be optional and required based on individual syntax of each widget. The options range from size to formatting of each component. Widget uses a structure similar to naming packages. In Tk, the root window is named with a period. The variable name should start with a lowercase letter, digit, or punctuation mark except a period.
After the first character, other characters may be uppercase or lowercase letters, numbers, or punctuation marks except periods.
It is recommended to use a lowercase letter to start the label. The colors can be declared using name like red, green, and so on. It can also use hexadecimal representing with. The number of hexadecimal digits can be 3, 6, 9, or The default unit is pixels and it is used when we specify no dimension.
The other dimensions are i for inches, m for millimeters, c for centimeters and p for points. Used to set the color rectangle to draw around a widget when the widget does not have input focus.
Sets the 3D relief for this widget. The condition may be raised, sunken, flat, ridge, solid, or groove. Variable associated with the widget. When the text of widget changes, the variable is set with text of widget.
Basic widgets are common widgets available in almost all Tk applications. Layout widgets are used to handle layouts for the Tk application.
Frame widget is used group other widgets and place, pack, and grid are layout manager to give you total control over your adding to windows. Selection widgets are used to select different options in a Tk application. The list of available selection widgets are as shown below. Sets the closeness of mouse cursor to a displayable item. The default is 1.
This value may be a fraction and must be positive. Mega widgets include many complex widgets which is often required in some large scale Tk applications. There are a number of widgets that supports displaying text. Most of these provides the option of font attribute. The image widget is used to create and manipulate images. The image will be deleted visually and from memory once "image delete imgobj" command executes.
Events in its simplest form is handled with the help of commands. Window manager is used to handle the top level window. It helps in controlling the size, position, and other attributes of the window. In Tk,. As you can see alpha is one of the attributes available. The geometry manager is used to manage the geometry of the window and other frames. We can use it to handle the position and size of the window and frames.
The layout widgets are used for this purpose. Here, w refers to width and h refers to height. Here, stdout makes the program to print in the standard output device. In command substitutions, square brackets are used to evaluate the scripts inside the square brackets. A simple example to set a value to a variable and print it is shown below. These are commonly called escape sequences ; with each backslash, followed by a letter having its own meaning. Tcl - Commands Advertisements. Let see example of Arithmetic operators in TCL.
Checks if the value of left operand is greater than the value of the right operand. If yes, then condition becomes true and return 1 else return 0. Check if the value of left operand is less than the value of the right operand. If any of the two operands is non-zero, then condition becomes true and return 1 else return 0. Used to reverse the result of any expression.
Here in the output, you can see the value of 'a' has now become 1 from 0. While the value of 'b' has become 0 from 1. If condition-exp is true, exp1 is evaluated and the result is returned.
If the cond-exp is false, exp2 is evaluated and its result is returned. In our example, exp1 is true as the value of A is greater than 6. String comparison operator compares the value of both operands. If the value of operand are same, then it will return 1 else return 0. In example value for both A and B is 7, therefore result return 1.
If the required value found in the defined list, it returns 1 else return 0. In example value 1 exists in variable 'a' hence it will return 1. There are various flow control and decision making command which are used to alter the flow of a program. Program executions always start from the top of source file to the bottom.
The switch statement enables a variable to be tested for equality against a list of values. It evaluates the list of values and returns the result of that evaluation.
If no values matches then default values will be returned. Example : In the following example, value of a is , and with the same code we switch statement for another value of b is The out will show value for both a and b.
0コメント