Consider this code:
set my_global_var "arijit"
proc my_proc {} {
puts $my_global_var;
}
my_proc
In this code, I have a constraint that I cannot have a "global
my_global_var" statement inside 'my_proc' proc.
With that constraint, is there any way in TCL by which I can instruct
TCL to lookup for global variables automatically.
I mean, given a variable usage, see if it is defined locally. If not,
check for global defination like C/C++.
Is there any way I can get this behavior in TCL (without explicitly
saying that my_global_var is global using "global my_global_var")?
My appologies, if this is not the right forum to ask this question.
Thanks
Arijit
>Am a beginner in TCL. Need a quick help...
>
>Consider this code:
>
>set my_global_var "arijit"
>
>proc my_proc {} {
> puts $my_global_var;
>}
>
>my_proc
>
>In this code, I have a constraint that I cannot have a "global
>my_global_var" statement inside 'my_proc' proc.
>
>With that constraint, is there any way in TCL by which I can instruct
>TCL to lookup for global variables automatically.
No - well, not that I know of.
The easiest thing would be to say
proc my_proc {} {
puts $::my_global_var
}
and in case your constraint prohibits this, too (care to tell what
this constraint is?) you could explicitly jump into global scope like
proc my_proc {} {
uplevel #0 {puts $my_global_var}
}
HTH
Helmut Giese
why on earth is that constraint imposed?
>
> With that constraint, is there any way in TCL by which I can instruct
> TCL to lookup for global variables automatically.
>
no (well, maybe by cheating*)
> I mean, given a variable usage, see if it is defined locally. If not,
> check for global defination like C/C++.
>
tcl is not C/C++
> Is there any way I can get this behavior in TCL (without explicitly
> saying that my_global_var is global using "global my_global_var")?
>
no, but if you know it is global you can be explicit
$::my_global_var
which avoids the actual use of the global command
> My appologies, if this is not the right forum to ask this question.
>
correct forum for sure.
> Thanks
> Arijit
>
*the cheat
override the proc command to automatically insert the global command
as the proc is being defined - that way you don;t have the global in you
codebase, but it is there at runtime.
Bruce
my_proc
meet your needs?
If not, it sounds as though you want your proc to behave differ-
ently from the built-in proc. That's not too hard; is it indeed
what you're after?
rename proc _proc
_proc proc {name argl body} {
_proc $name $argl "eval global \[info globals\]\n$body"
}
Testing:
% proc try x {return $x$tcl_version}
% try it
it8.4
But this is a questionable trick. The separation of globals and locals
has good reasons, and locals are to be preferred whenever possible
(faster, need less memory, have no side effects).
Where does this constraint come from -- your teacher or your boss?
If it is your boss, then you need to post the exact constraint language
because it maybe saying more or less then you think -- or better yet ask him
how you should be doing it (I'm sure it will get pointed out during the code
review if you go off on a tangent from the company accepted policy).
It is from your teacher, I fear he was looking for you to use namespace
referencing -- in which case other posters have already done your homework
for you.
BTW, in either case since you appear hesitant to ask your boss/teacher the
question, what makes you think they are not reading c.l.t?
--
+--------------------------------+---------------------------------------+
| Gerald W. Lester |
|"The man who fights for his ideals is the man who is alive." - Cervantes|
+------------------------------------------------------------------------+
> In this code, I have a constraint that I cannot have a "global
> my_global_var" statement inside 'my_proc' proc.
Oh boy, a puzzle game.
upvar #0 my_global_var my_global_var
is my entry