Discussion:
Accessing variable defined in one proc, in another proc
(too old to reply)
rick
2007-06-11 07:35:44 UTC
Permalink
I am defining a variable inside a proc. And I want to use this
variable in another proc.
One way is to return that variable form that proc to the main script
and then access it in another proc by using global command. But is
there any way , I can access it in another proc directly.

eg :

proc1 {

set a xxx

}

proc2 {

puts $a

}

main script :

proc1
proc2

In the above example I want a defined in proc 1 to be accessible in
proc 2.
b***@alum.mit.edu
2007-06-11 07:56:34 UTC
Permalink
Post by rick
I am defining a variable inside a proc. And I want to use this
variable in another proc.
One way is to return that variable form that proc to the main script
and then access it in another proc by using global command. But is
there any way , I can access it in another proc directly.
proc1 {
set a xxx
}
proc2 {
puts $a
}
proc1
proc2
In the above example I want a defined in proc 1 to be accessible in
proc 2.
One approach is to assign to a global variable in proc1, then
reference it in proc2. Just change proc1 to:

proc proc1 {} {
set ::a xxx
}

If you don't want this variable to be readily accessible globally, you
can make it a namespace variable and put both procs within the same
namespace:

namespace eval foo {
variable a
proc proc1 {} {
variable a
set a xxx
}

proc proc2 {} {
puts $a
}
}

You now have a namespace foo containing proc1 and proc2 and a variable
a
which is shared by the two procs but not visible to other procedures.
b***@alum.mit.edu
2007-06-11 07:57:14 UTC
Permalink
Post by b***@alum.mit.edu
Post by rick
I am defining a variable inside a proc. And I want to use this
variable in another proc.
One way is to return that variable form that proc to the main script
and then access it in another proc by using global command. But is
there any way , I can access it in another proc directly.
proc1 {
set a xxx
}
proc2 {
puts $a
}
proc1
proc2
In the above example I want a defined in proc 1 to be accessible in
proc 2.
One approach is to assign to a global variable in proc1, then
proc proc1 {} {
set ::a xxx
}
If you don't want this variable to be readily accessible globally, you
can make it a namespace variable and put both procs within the same
namespace eval foo {
variable a
proc proc1 {} {
variable a
set a xxx
}
proc proc2 {} {
puts $a
}
}
You now have a namespace foo containing proc1 and proc2 and a variable
a
which is shared by the two procs but not visible to other procedures.
Oops. I forgot to put the declaration "variable a" in the second proc.
Arjen Markus
2007-06-11 07:59:59 UTC
Permalink
Post by rick
I am defining a variable inside a proc. And I want to use this
variable in another proc.
One way is to return that variable form that proc to the main script
and then access it in another proc by using global command. But is
there any way , I can access it in another proc directly.
proc1 {
set a xxx
}
proc2 {
puts $a
}
proc1
proc2
In the above example I want a defined in proc 1 to be accessible in
proc 2.
Variables defined and used in a procedure exist only while that
procedure is being run. When the procedure returns all variables
within that procedure are gone.

What you really want is a global variable:

proc proc1 {
global a
set a 1
}
proc proc2 {
global a
puts $a
}

# Main code

proc1
proc2

You can look at the "variable" command too that defines
variables in other namespaces than the global namespace.

What you can also do is use the upvar command, if proc1
uses proc2:

proc proc1 {
set a 1
proc2
puts "After proc2: $a"
}

proc proc2
upvar 1 a use_a
puts "From proc1: $use_a"
set use_a 2
}

That works because the variable "a" as defined in proc1 still
exists when proc2 is running - proc1 is also still there
(albeit nothing happens until proc2 returns).

Regards,

Arjen
rick
2007-06-11 09:16:15 UTC
Permalink
Thanks all for your help.
I figured out the mistake I was doing

In proc1 i was defining "global a" after "set a xxx" and hence was
getting error "variable already exist" , but on declaring it global
before setting it, everything worked fine.



***********Code with error ***************
proc proc1 {
set a 1
global a
}

proc proc2 {
global a
puts $a

}

# Main code

proc1
proc2

Error : Variable "a" already exists
****************************************

**********Code without error************

proc proc1 {
global a
set a 1
}

proc proc2 {
global a
puts $a

}

# Main code

proc1
proc2
***********************************

The other approach will not be that good in my case , as I am using
many procedures instead of two which I took for example..
Larry W. Virden
2007-06-11 14:17:19 UTC
Permalink
Post by rick
The other approach will not be that good in my case , as I am using
many procedures instead of two which I took for example..
Note that global variables can be a real pest. Why? Because you never
know when someone is going to be updating them - or where, for that
matter.

In the early days of tcl, there were global variables everywhere.
Then, once namespaces were supported in tcl, programmers tended to put
their variables into uniquely named namespaces. Of course, you have to
be careful even there - because toplevel namespace names are global.
The hope, though, is that by encapsulating, so to speak, the variables
within a namespace, you reduce the number of potential name clashes.

Sometimes people will also suggest, as an alternative, using a global
array as a "holder" of the various variables you might need.

Either way, you collect your variables into a single entity, which
hopefully allows you to then track back more easily to see where the
variable might be accessed.

Loading...