Reputation: 15139
Procs were introduced in BIGIP-11.4.0 and I am trying to access external variables being passed as reference to the proc or to be returned from the proc. No success so far. not too much documentation either on this feature either.
https://devcentral.f5.com/wiki/irules.call.ashx
See example code below. I am moving too much duplicate code to procs and running into this issue. Thoughts? Any workaround?
TCL has a keyword named upvar which is supported in Big IP 11.4.x. This is similar to using pass by reference. But this does not seem to be working for me. May be I am missing something. In the below case, the value of {test_var} should be 10 after the call to the proc. Instead It is still 1.
Proc
proc testproc_byref { {test_var}} {
upvar 1 $test_var var
set var 10
log local0. "Inside the proc by ref. Setting its value to 10"
log local0. "test_var ${test_var}"
}
Caller
call TEST_LIB::testproc_byref ${test_var}
log local0. "AFTER calling the proc test_var : ${test_var}"
Output
Before calling the proc test_var : 1
Inside the proc by ref. Setting its value to 10
test_var 1
AFTER calling the proc test_var : 1
A) Is there a way to pass the variable ${test_var} as reference from the caller to the proc so that the manipulated variable value in the proc can be available to the caller?
Or
B) Is there way to return the variable ${test_var} back the caller so that It can be used by the caller?
Upvotes: 0
Views: 312
Reputation: 15139
Using Pass by version [for the question a) above]
Just take out the $ and curly braces of the variable being passed as reference so - instead on this :-
call TEST_LIB::testproc_byref ${test_var}
use this :-
call TEST_LIB::testproc_byref test_var
Using Return [for the question b) above]
The below answer is using "return" keyword to return a single value from the proc. But this covers only the scenario when you have single value to be returned.
"return" is supported from inside the proc so manipulated value in the proc can be returned and used by the caller.
commonlib iRule
proc testproc { {test_var}} {
set test_var 5
log local0. "Inside the proc"
log local0. "test_var ${test_var}"
return ${test_var}
}
Caller
set returned_test_var [call TEST_LIB::testproc ${test_var}]
log local0. "AFTER calling the proc test_var - Returned : ${returned_test_var}"
OUTPUT
Before calling the proc test_var : 1
Inside the proc
test_var 5 AFTER calling the proc test_var - Returned : 5
Upvotes: 1