1.Difference between “” & {} .
myvar = "some string", then {a $myvar b} will
result in the string literal "a $myvar b"... while "a $myvar
b" will result in the string literal "a some string b"
See the below code for explanation.If we have a variable
myvar with value as abcd
if we puts {} ,value of $abcd will not be substituted if we
use " " value will be substituted .
% set myvar
"abcd"
abcd
% puts {1 $myvar 2}
1 $myvar 2
--------->No Substitution
% puts "1
$myvar 2"
1 abcd 2
---------->Substitution
%
2. Upvar Command
Usage: Used
when we have to change the value of a global variable from inside a procedure’s
scope.
Upvar:
Create link to variable in a different stack frame. Upvar simplifies the
implementation of call-by-reference calling and
also makes it easier to implement Tcl procedures that are new control
constructs.
What is the
use of upvar?
A) Map a
variable from the calling scope into the local procedure scope.
B) Map a
variable from the local scope into the calling scope.
C) Copy the
value of a variable from the calling scope to the local scope
The upvar
command links a local variable with another variable (usually global).Any
change made to local variable will also change the global variable. The upvar
command allows you to easily pass arrays and arguments into procedures.
Syntax :
upvar level $target_variable link_variable
Example:1
proc example {one two} {
upvar $one local1
upvar $two local2
set local1 Kavitha
set local2 Anbarasu
}
set glob1 David
set glob2 Beckam
puts $glob1
puts $glob2\n
example glob1 glob2
puts $glob1
puts $glob2
Output
David
Beckam
Kavitha
Anbarasu
In the above example we are able to change the value of two global variables glob1 and glob2 from within a procedure.
Beckam
Kavitha
Anbarasu
In the above example we are able to change the value of two global variables glob1 and glob2 from within a procedure.
3.Uplevel
uplevel is similar to the upvar command but used to evaluate commands in the scope of the calling procedure. Use with care!
uplevel incr x
Increments the variable x in the calling procedure.
proc a {} {
set x a
uplevel 3 {set x Hi}
puts "x
in a = $x"
}
proc b {} {
set x b
a
puts "x
in b = $x"
}
proc c {} {
set x c
b
puts "x
in c = $x"
}
set x main
c
puts "x
in main == $x"
%
c
x
in a = a
x
in b = b
x
in c = c
%
puts "x in main == $x"
x in main == Hi
%
See the above code: Most inside proc in
in level 0 always.
a is in level 0,b in level 1 and c in
level 2 and the main program is in level 3.
So in proc a if i change the value of
level then i can change the value of variable x of any proc be it a,b,c or main
proc from method "a" itself. Try changing level to 3,2,1,0 and see
the output.
If in proc a we have uplevel 2 {set x Hi} below will be the output: i.e
level 2 i.e c value will be changed and so on.
x in a = a
x in b = b
x in c = Hi
x in main == main
4.EVAL
Command
The eval command
will evaluate a list of strings as though they were commands typed at the % prompt
or sourced from a file. The eval command normally returns the final
value of the commands being evaluated. If the commands being evaluated
throw an error (for example, if there is a syntax error in one of the strings),
then eval will will throw an error.
eval arg1
??arg2?? ... ??argn??
Evaluates arg1 - argn as
one or more Tcl commands. The args are concatenated into a
string, and passed to tcl_Eval to evaluate and execute.
Eval returns the result (or error code) of that
evaluation.
eval is the Tcl interpreter itself. It takes a
Tcl script as a string, passes it to the interpreter for evaluation, and
returns the result. In fact, eval takes
any number of string arguments and concatenates them together into a single
string with whitespace separating the arguments before
evaluation.
eval is useful for evaluating Tcl scripts
passed by name.
• eval
- Evaluate a Tcl script
• eval arg ?arg
...?
set a 10
set b a
eval puts $$b
Example:1
set cmd {puts
"Evaluating a puts"}
puts $cmd ---->> puts "Evaluating a puts"
puts "Evaluating
a puts" ---->> Evaluating a puts
eval $cmd ----->> Evaluating a puts
Example 2:
set str
{set lst [list 100 200 300 400]}
set a
[eval $str]
puts
$a : 100 200 300 400
Example 3:
set a {puts "this is log
file"}
puts $a ------>> puts "this is log file"
eval $a ------->> this is log file
puts "this is log file"
Global
variables:
The scope in
which a variable will be evaluated can be changed with
the global and upvar commands.
The global command
will cause a variable in a local scope (inside a procedure) to refer to the
global variable of that name.
• global varname ?varname
...?
• This
command has no effect unless executed in the context of a proc body. If
the global command is executed in the context of a proc body,
it creates local variables linked to the corresponding global variables
proc accum
{string} {
global accumulator
append
accumulator $string \n
}
Different ways of
global variable identification.
set var 3
proc glob {} {
global var
puts
"the value of variable is $var"
}
glob
or
Declaring global
variable using double colon (::)
proc proc01 {} {
puts
“The value of var1
is: $::var1”
}
5.Difference
of local and global variable.
set x 100
proc fun {} {
global x
puts "inside
fun $x"
}
proc fun1 {} {
set x 500
puts "inside
fun1 $x"
}
fun --------->> inside fun 100
fun1 --------->> inside fun1 500
NOTE:
global
only declares a variable, not creates it. More
precisely, in a special table of variables available in the current scope
(procedure in your case), global
creates a
special "link" entry which points to the similar table in the global
scope. Then all accesses to that local entry are transparently redirected to
the linked global entry. Hence, after global
finished,
all the stuff like existence/value is defined by the linked-to variable.
6.Spawning
telnet using expect