0

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

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)有没有办法将变量 ${test_var} 返回给调用者,以便调用者可以使用它?

4

1 回答 1

1

使用 Pass by version [针对上述问题 a)]

只需取出作为参考传递的变量的 $ 和花括号 - 而不是这样: -

call TEST_LIB::testproc_byref ${test_var}

用这个 :-

call TEST_LIB::testproc_byref test_var

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

下面的答案是使用“return”关键字从 proc 返回单个值。但这仅涵盖要返回单个值的情况。

proc 内部支持“return”,因此调用者可以返回和使用 proc 中的操作值。

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

输出

在调用 proc test_var 之前:1

进程内部

调用 proc test_var 后的 test_var 5 - 返回:5

于 2015-05-21T20:46:22.907 回答