Each of the Tcl commands validates the input and it reduces the work of the interpreter.
Tcl command is actually a list of words, with the first word representing the command to be executed. The next words represent the arguments. In order to group the words into a single argument, we enclose multiple words with "" or {}.
The syntax of Tcl command is as follows −
commandName argument1 argument2 ... argumentNLet's see a simple example of Tcl command −
#!/usr/bin/tclsh puts "Hello, world!"When the above code is executed, it produces the following result −
Hello, world!In the above code, ‘puts’ is the Tcl command and "Hello World" is the argument1. As said before, we have used "" to group two words.
Let's see another example of Tcl command with two arguments −
#!/usr/bin/tclsh puts stdout "Hello, world!"When the above code is executed, it produces the following result −
Hello, world!In the above code, ‘puts’ is the Tcl command, ‘stdout’ is argument1, and "Hello World" is argument2. Here, stdout makes the program to print in the standard output device.
Command Substitution
In command substitutions, square brackets are used to evaluate the scripts inside the square brackets. A simple example to add two numbers is shown below −#!/usr/bin/tclsh puts [expr 1 + 6 + 9]When the above code is executed, it produces following result −
16
Variable Substitution
In variable substitutions, $ is used before the variable name and this returns the contents of the variable. A simple example to set a value to a variable and print it is shown below.#!/usr/bin/tclsh set a 3 puts $aWhen the above code is executed, it produces the following result −
3
Backslash Substitution
These are commonly called escape sequences; with each backslash, followed by a letter having its own meaning. A simple example for newline substitution is shown below −#!/usr/bin/tclsh puts "Hello\nWorld"When the above code is executed, it produces the following result −
Hello World
No comments:
Post a Comment