Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
 
This week, we're updating our course layouts and descriptions. Presentation and materials always gently change over time, but just occasionally there's a need to make a step change to clear out some of the old and roll in the new. That's now happening - but over a long and complex site it's not instant and you'll see sections of the site changing up to and including 19th September.

See also [here] for status update
 
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.  Thus

proc 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. No
2. 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:
set CFG(variable1) "A global variable"
set CFG(variable2) "Another"

proc doSomething {} {
       global CFG
       puts $CFG(variable1)
}

doSomething


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.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2014: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho