Page 176 - Asterisk™: The Future of Telephony
P. 176

This would have assigned the variable TEST to the string “2+1”, instead of the value 3.
               In order to remedy that, we would put spaces around the operator like so:
                   exten => 234,1,Set(TEST=$[2 + 1])
               This is no longer necessary in Asterisk 1.2 or 1.4 as the expression parser has been made
               more forgiving in these types of scenarios, however, for readability’s sake, we still rec-
               ommend the spaces around your operators.
               To concatenate text onto the beginning or end of a variable, simply place them together
               in an expression, like this:
                   exten => 234,1,Set(NEWTEST=$[blah${TEST}])

               Dialplan Functions


               Dialplan functions allow you to add more power to your expressions; you can think of
               them as intelligent variables. Dialplan functions allow you to calculate string lengths,
               dates and times, MD5 checksums, and so on, all from within a dialplan expression.

               Syntax

               Dialplan functions have the following basic syntax:
                   FUNCTION_NAME(argument)
               Much like variables, you reference a function’s name as above, but you reference a
               function’s value with the addition of a dollar sign, an opening curly brace, and a closing
               curly brace:
                   ${FUNCTION_NAME(argument)}
               Functions can also encapsulate other functions, like so:
                   ${FUNCTION_NAME(${FUNCTION_NAME(argument)})}
                    ^             ^ ^             ^        ^^^^
                    1             2 3             4        4321
               As you’ve probably already figured out, you must be very careful about making sure
               you have matching parentheses and braces. In the above example, we have labeled the
               opening parentheses and curly braces with numbers and their corresponding closing
               counterparts with the same numbers.

               Examples of Dialplan Functions

               Functions are often used in conjunction with the Set() application to either get or set
               the value of a variable. As a simple example, let’s look at the LEN() function. This
               function calculates the string length of its argument. Let’s calculate the string length of
               a variable and read back the length to the caller:




               148 | Chapter 6: More Dialplan Concepts
   171   172   173   174   175   176   177   178   179   180   181