TCL Procs and Levels
Problem this snippet solves:
Procs are great, they help keep your iRules tidy and can be used for libraries of common functions.
However they can get unwieldy if you have to pass lots of variables, as variables set in the global scope, are not accessible directly once inside a proc.
You can access a variable outside of a proc using 'upvar' and set a variable in the calling scope using 'uplevel'
Usually you will only need to go up 1 level, think of the level as folder structure where you would go back up one folder to access a file.
This principle falls apart when you have procs, calling other procs, it's hard to keep track of the levels.
This code snippet shows an example of passing the calling level, into a proc, so that that level can be referenced directly using its explicit level number
How to use this snippet:
Code :
proc myProc {callingLevel} { uplevel #$callingLevel "eval {set var1 123456}" upvar #$callingLevel "var1" var1 puts "var1 from level $callingLevel var1:$var1" } # var1 set in global scope set var1 "Hello!" # call proc (you will need prefix this with 'call' in an iRule) myProc [info level] # this var2 was set in the calling scope from inside the proc puts "var2: $var2"
Tested this on version:
No Version Found