[OpenSIPS-Users] routing function variable scope

Kingsley Tart kingsley at dns99.co.uk
Wed Apr 28 12:19:01 EST 2021


Hi,

(sorry!)

Is there a way in routing functions to create variables local to just
that function?

I had a problem earlier where I had a function call another, and they
both used the same var name (intending them to be local to the
function), but the sub-function ended up overwriting the value of the
same-named var in the calling function.

I find myself using AVPs and then unsetting them afterwards in order to
not tread on vars that other functions might use but with all of the
tidyup code things get messy.

Foe example, this would loop forever, but using an AVP as the counter
variable would then need another loop to remove all of the added values
(but not all values in that AVP, in case that deletes AVPs set
somewhere else):

route(test_outer);

route[test_outer] {
	$var(counter) = 0;
	while ($var(counter) < 10) {
		route(show_multiples, $var(counter));
		$var(counter) = $var(counter) + 1;
	}
}

route[show_multiples] {
	$var(counter) = 1;
	while ($var(counter) < 5) {
		$var(multiple) = $param(1) * $var(counter);
		xlog("multiple=$var(multiple)\n");
		$var(counter) = $var(counter) + 1;
	}
}

Is there a better way, apart from trying to make sure that variables in
each function don't share names with variables in other functions?

-- 
Cheers,
Kingsley.




More information about the Users mailing list