| ||||
defining global variable for Proc Posted by shafa.fahad (FahaD), 13 September 2006 Hi,I am using mutilple proc inside my expect script like proc readfile {arg1 arg2} { } proc getname {arg1 arg2} { } How can we define a global variable which can be accessible form both the procs? Thanks in advance Fahad Posted by admin (Graham Ellis), 13 September 2006 In the body of each proc declare the same global variable. Thusproc readfile {arg1 arg2} { global sharethis } proc getname {arg1 arg2} { global sharethis } Posted by shafa.fahad (FahaD), 14 September 2006 Thanks Graham...It works well, I would also like to know how to define global variables if there are mutiple heirarchy. For example, My whole script is a proc where number of procs are defined inside it in multiple hierarchy proc readconf { arg1 arg2} { proc getname { arg1} { proc getvalue {arg2 } { } } } In the above case, should i declare the global variable in each of the hierarchy as global <variablename> like proc readconf { arg1 arg2} { global var proc getname { arg1} { global var proc getvalue {arg2 } { global var } } } Also would like to know, For a variable to be a Global, should that be defined outside all procs? OR can that be defined inside a proc to be used by the following hierarchy? Thanks Fahad Posted by admin (Graham Ellis), 14 September 2006 "global" really means "shared with the top level" so you do not need to define a variable as global in the main code, even if you use it there. And you only need to define it in readconf if you use it in there outside of getname and getvalue.Have a look at namespaces if you're writing applications that you need to segment / handle more flexibly than global allows. Posted by shafa.fahad (FahaD), 14 September 2006 Thanks Graham.....I have a doubt. For the below program, if i dont declare a global variable outside the procedure it thows error #!/usr/bin/expect -f proc full {} { proc test {} { global globalVar ---> 1 puts $globalVar } proc result {} { global globalvar puts $globalvar } ----> 2 test result } set globalVar "a string" ---> 3 full 1 -- Is there any way to use globally declared variable without stating like this? because for N number of globally defined variable i have to declare N time inside the proc. 2 -- If i try to declare here, it throws error ( since my whole script is a proc i need to decalre here ) 3 -- If i dont declare here, it throws error ( i can't declare outside the proc ) Please add your suggestions.... Thanks Graham Posted by admin (Graham Ellis), 14 September 2006 1. No2. Put it after the line proc full {} { 3. The error it will throw, though, is when you try to reference its value and you have not set it to an intial value. That's not anything to do with whether or not its global. In Tcl, if you try to use a variable that hasn't yet been set, it complains. Posted by shafa.fahad (FahaD), 14 September 2006 Very Thank You Graham.... Posted by Custard (Custard), 14 September 2006 For (1) you could use a single global Associative Array, and then you only need 1 global statement;so: (untested) Code:
b This page is a thread posted to the opentalk forum
at www.opentalk.org.uk and
archived here for reference. To jump to the archive index please
follow this link.
| ||||
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho |