Code blocks

Last Updated : Oct 31, 2017 |

The code blocks for the act on statements contain the code necessary to carry out actions. Four kinds of statements can go into the code block: Assignment Statement, Conditional Statement, Function Call, and Print Statement.

Code Blocks

A list of statements that can go into a code block is provided below.

  • Assignment Statement. For example:

    • %var = “1”;

    • %var = HEADERS[“From”][0];

    • HEADERS[“From”][0] = “From: Alice <sip:alice@atlanta.com>;tag=1928301774”

    • HEADERS[“To”][0] = %val;

  • Conditional Statement. For example:

if (%var = “value”) then

{

…Code…

}

else

{

…Code…

}

  • The operators can be:

    • = for equality

    • != for negation of equality

    Either side of the operators can be a variable, a quoted string, any of the built-in arrays’ values or a regular expression get()/match() call.

    If the condition is true then the code in the then {} block is executed otherwise the else {} block will be executed.

  • Function Call. Usually called on a built-in function. For example:

    • remove(): To remove a header

    • append(): To append a string to a header

    • regex_replace(): To replace text within a header using a regular expression

  • Print Statement. Prints the parameters given in the log file of the process as an INFO level log. The parameters must be separated by commas and can be any of the following free string in quotes, variables, or any built-in variable.

    • print “foo”, “bar”

    • print “Body(1) is – “, %BODY[1]