PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, November 10, 2022

[FIXED] How to access a variable defined in proc 'a' from a different proc 'b' which does not call proc 'a'?

 November 10, 2022     global, proc, tcl, upvar     No comments   

Issue

I am trying to execute a tcl script which makes exclusive calls to procs a and b. The two procs are not related to each other.

proc a {} {
   set var1 "a"
}

proc b {} {
   # Do something here with: $var1
}

 # script.tcl
 a
 b

I do not have access to the script.tcl as well. When proc 'a' is called, I need to store the var1 somehow such that I can access it later within proc 'b' when it is called. How can I get the value of var1 in proc b? Doesn't seem like I can use 'global' and 'upvar' here?


Solution

A simple way is to define the variable in the global scope by preceding the variable name with ::

proc a {} {
   set ::var1 "a"
}

proc b {} {
   puts $::var1
}

Other methods would be to use the global command in each proc or to define the variable in a special namespace of its own.



Answered By - Chris Heithoff
Answer Checked By - Marilyn (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing