Sharing Variables - 2021.2 English

Vivado Design Suite User Guide: Using Tcl Scripting (UG894)

Document ID
UG894
Release Date
2021-11-17
Version
2021.2 English

The golden rule regarding variables is that global variables are forbidden. The Xilinx Tcl Store linter checks for global variables inside the scripts and generates an error message if any global variable is defined.

If some variables need to be shared within procs of an app, then they should be declared inside the namespace. This is done by creating the variables with the keyword variable inside the namespace. Such variables can be used like global variables, but only within the namespace.

The code below creates the variable verbose that can be shared within all the procs that belong to the same namespace. The variable is initialized to 0:

namespace eval ::tclapp::mycompany::template {
  variable verbose 0
  }

To access the variable inside a proc, the variable should be declared with the keyword variable inside the proc.

For example:

proc ::tclapp::mycompany::template::::my_command1::my_command1 { args } {
  variable verbose
  # The variable verbose can be accessed here
  set verbose 1
}
proc ::tclapp::mycompany::template::my_command1::helper1 { args } {
  variable verbose
  # The variable verbose can be accessed here
  if {$verbose} {
    ..
  }
}
Note: The variable verbose can also be accessed from any namespace (including the global namespace) by using the full namespace qualifier. For example:
set ::tclapp::mycompany::template::verbose 1