BIG-IP 11.5.2 - PROCs - 通过引用传递或 return

BIG-IP 11.5.2 - PROCs - passing by reference or return

Procs 是在 BIGIP-11.4.0 中引入的,我正在尝试访问作为对 proc 的引用传递的外部变量或从 proc returned。 到目前为止没有成功。也没有太多关于此功能的文档。

https://devcentral.f5.com/wiki/irules.call.ashx

请参阅下面的示例代码。 我将过多的重复代码移动到 procs 和 运行 到这个问题中。 想法?任何解决方法?

按引用传递

TCL 有一个名为 upvar 的关键字,Big IP 11 支持该关键字。4.x。 这类似于使用按引用传递。 但这似乎对我不起作用。可能是我遗漏了什么。 在下面的例子中,调用 proc 后 {test_var} 的值应该是 10。相反,它仍然是 1.

过程

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}"
}

来电

call TEST_LIB::testproc_byref ${test_var}
log local0. "AFTER calling the proc test_var : ${test_var}"

输出

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) 有没有办法将变量 ${test_var} 作为调用者的引用传递给 proc,以便调用者可以使用 proc 中的操纵变量值?

B) 有没有办法 return 变量 ${test_var} 返回调用者,以便调用者可以使用它?

使用按版本传递 [针对上面的问题 a)]

只需取出作为引用传递的变量的 $ 和花括号 所以 - 而不是这个 :-

call TEST_LIB::testproc_byref ${test_var}

使用这个:-

call TEST_LIB::testproc_byref test_var

使用Return [对于上面的问题b)]

下面的答案是使用 "return" 关键字来 return 来自 proc 的单个值。但这仅涵盖当您有单个值要 returned 时的场景。

"return" 由过程内部支持,因此过程中的操纵值可以 returned 并由调用者使用。

commonlib iRule

proc testproc { {test_var}} {
set test_var 5
log local0. "Inside the proc"
log local0. "test_var ${test_var}"
return ${test_var}
}

来电

set returned_test_var [call TEST_LIB::testproc ${test_var}]
log local0. "AFTER calling the proc test_var - Returned : ${returned_test_var}"

输出

在调用过程之前 test_var : 1

过程内部

test_var 5 调用过程后 test_var - Returned : 5