Forum Discussion

AlexS_yb's avatar
AlexS_yb
Icon for Cirrocumulus rankCirrocumulus
Dec 05, 2022

Scope of variables when using call

Hi I have some irule code that looks like in one file   MyLib_v1.0 proc process_URL {} { log local0. "process_URL: Enter" if { [ACCESS::session exists] && [ACCESS::session exists -state_allow -...
  • PeteWhite's avatar
    Dec 05, 2022

    The proc does not inherit the variables from the caller. It will inherit all static variables and functions eg HTTP::path, but there are two ways to allow these variables in the procedure - either send them as calling variables, or use upvar.

    Sending them as variables is more dependable - you don't need to worry about naming or suchlike but you can end up sending a lot of information. Add this to the procedure eg

    proc process_URL {var1 var2 var3 {optionalvar "default value"} } {
    ...
    }

    You can also only have a single variable but make it a list.

    Upvar depends on naming of variables to import a specific variable into the proc scope. Very useful if you want to modify the original variable, but open to errors if the variable doesn't exist or suchlike

    https://www.tcl.tk/man/tcl/TclCmd/upvar.html