Scripting Languages Unit V TCL Department of Information Technology
Scripting Languages Unit V TCL Department of Information Technology
Unit V
TCL
Department of Information Technology
• In Tcl language there are several commands that are used to alter the
flow of a program.
• When a program is run, its commands are executed from the top of
the source file to the bottom. One by one. This flow can be altered by
specific commands.
• Commands can be executed multiple times. Some commands are
conditional. They are executed only if a specific condition is met.
#!/usr/bin/tclsh
set files [glob *.tcl]
foreach file $files
{
puts $file
}
• The script prints all files with the .tcl extension to the console. The glob
command returns a list of files that match the *.tcl pattern.
BVRIT HYDERABAD College of Engineering for Women
We go through the list of files and print each item
set files [glob *.tcl] of the list to the console.
foreach file $files $ ./globcmd.tcl
{ attributes.tcl
allfiles.tcl
puts $file printing.tcl
} hello.tcl
read.tcl
files.tcl
globcmd.tcl
write2file.tcl
cwd.tcl
readfile.tcl
isfile.tcl
addnumbers.tcl
This is a sample output of the globcmd.tcl script
proc tclver {}
{
set v [info tclversion]
puts "This is Tcl version $v"
}
tclver
• This is the body of the tclver procedure. It is executed when we execute the tclver
command. The body of the command lies between the curly brackets.The procedure is
called by specifying its name.
Glob matching
• Simple pattern matching using is accomplished by string match.
• This performs glob matching using just three patterns: * matches any number
of characters ? matches a single character and [abc] matches any one of the
set of characters enclosed in the brackets.
• (Remember that if you use a choice-of-alternatives pattern it must be hidden
within braces or stored as the value of a variable, otherwise the interpreter
will try to do a command substitution.)
• Thus string match {[A-z] [a-z]*} $name will return true if the value of $name is
a string of alphabetic characters with the first character capitalized.
close fileName