[asterisk-users] ExecIf and empty variables (early evaluation)
Philipp Kempgen
philipp.kempgen at amooma.de
Wed Jul 22 10:30:24 CDT 2009
Benny Amorsen schrieb:
> Imagine that you have this code:
>
> exten => _X!,n,Set(foo=${QUEUE_WAITING_COUNT(${QueueName})}))
>
> If ${QueueName} happens to be unset, this will cause a warning:
>
> [Jul 22 14:26:17] ERROR[8114]: app_queue.c:5187
> queue_function_queuewaitingcount: QUEUE_WAITING_COUNT requires an
> argument: queuename
>
> The obvious solution:
>
> exten => _X!,n,ExecIf($["${QueueName}" != ""]?Set(foo=${QUEUE_WAITING_COUNT(${QueueName})}))
>
> However, this doesn't actually work! Functions and variables on the
> right hand side are evaluated BEFORE it is decided whether it needs to
> be executed at all!
>
> This is fairly harmless in this case, in that it just causes a warning.
You could split it up into multiple statements:
if ("${QueueName}" != "") {
Set(foo=${QUEUE_WAITING_COUNT(${QueueName})});
} else {
Set(foo=-1); // or whatever
}
(don't remember how to write that in extensions.conf format)
Pros:
- conditional evaluation
- more readable (ExecIf() looks ugly)
Cons:
- more statements
- less readable then a ternary conditional expression in "real"
programming languages:
foo = ($queuename != "" ? queue_waiting_count($queuename) : -1)
> However, what about this case?
>
> exten => _X!,n,ExecIf($[${bar} < 10]?Set(foo=${INC(bar)}))
>
> Which you can argue that this code is in poor taste, it is definitely
> surprising that INC is evaluated in this case, changing ${bar} even if
> ${bar} >= 10.
>
> It probably isn't possible to do something about this, but now you have
> all been warned... This could be a good reason for avoiding side effects
> in functions, and thereby banning ${INC()}
Ban ExecIf(). Use AEL. Use if(){} blocks. :-)
In order to use control structures like "if .. else"/"switch .. case"
it's almost necessary to write your dialplan in AEL because the same
thing is so incredibly hard to read and write in extensions.conf
format (GotoIf()).
Although very basic dialplans look good in extensions.conf my
suggestion is to use AEL and let the AEL compiler figure out how to
translate that into an Asterisk dialplan.
Philipp Kempgen
--
AMOOMA GmbH - Bachstr. 126 - 56566 Neuwied -> http://www.amooma.de
Geschäftsführer: Stefan Wintermeyer, Handelsregister: Neuwied B14998
Asterisk: http://the-asterisk-book.com - http://das-asterisk-buch.de
Videos of the AMOOCON VoIP conference 2009 -> http://www.amoocon.de
--
More information about the asterisk-users
mailing list