Movatterモバイル変換


[0]ホーム

URL:


  1. Home
  2. Reference documentation
  3. VCL reference

Operators

Fastly VCL provides various arithmetic and conditional operators.

Operator precedence

Operator precedence defines theorder of operations when evaluating an expression. Higher precedence operators are evaluated before those with lower precedence. Operators are listed in the following table, highest precedence first. For example,a || b && c reads asa || (b && c) because&& has higher precedence than||.

Operatorassociativity determines which side binds first for multiple instances of the same operator at equal precedence. For example,a && b && c reads as(a && b) && c because&& has left to right associativity.

OperatorNameAssociativity
()Grouping for precedenceleft to right
!Boolean NOTright to left
&&Boolean ANDleft to right
||Boolean ORleft to right

HINT: use explicit parentheses in expressions to avoid having to remember operator precedence rules.

Negation

Numeric literals may be negated by prefixing the- unary operator. This operator may only be applied to literals and not to numeric values in other contexts.

String concatenation

Adjacent strings are concatenated implicitly, or explicitly by using the+ operator. All of the following lines of code achieve the same result:

setreq.http.some-header="helloworld";
setreq.http.some-header="hello""world";
setreq.http.some-header="hello"+"world";

Concatenation works with literal strings, as well as headers and variables (including non-string types):

set var.string_variable="Hello "+req.http.cookie:user_name+" you are number "+ var.int_variable;

Conditional operators

Logical AND and OR operators (&& and|| respectively) are defined for conditional expressions.

WARNING: Logical operators haveshort-circuit evaluation, whereby the right-hand side is only evaluated when necessary. For example, givena && b, ifa is false the resulting value will always be false, so there's no need to evaluateb. This can be important when the right-hand operand has a visible side effect, such as a call to a function.

Conditional expressions may be inverted by prefixing the! operator.

Conditional operators produceBOOL values, suitable for use inif (...) statements:

if (!(beresp.status>=500&&beresp.status<600)) {
# ... the response is not an HTTP 5XX status
}

Comparison operators

The comparison operators are:

OperatorPurposeExampleTypes
==Equalresp.status == 200 (note there is no=== in VCL)All types
!=Not equalreq.url.path != "/"All types
~Matches regexreq.url ~ "^/products(?:/?.*)\z"STRING
~MatchesACLclient.ip ~ allowed_usersIP
!~Does not match regexreq.http.cookie:optin !~ "ads"STRING
!~Does not matchACLclient.ip !~ blocked_usersIP
>Greater thanreq.restarts > 0Numeric types andTIME
<Less thannow < var.expiry_timeNumeric types andTIME
>=At leastresp.status >= 500Numeric types andTIME
<=At mostrandomint(1,100) <= 75Numeric types andTIME

FLOAT comparisons have special cases for operands which are NaN: The!= operator always evaluates to true when either operand is NaN. All other conditional operators always evaluate to false when either operand is NaN. For example, if a given variable is NaN, that variable will compare unequal to itself: bothvar.nan == var.nan andvar.nan >= var.nan will be false.

STRING comparisons have special cases for operands which are not set (as opposed to empty): The!= and!~ operators always evaluate to true when either operand is not set. All other conditional operators always evaluate to false when either operand is not set. For example, if a variable is not set, that variable will compare unequal to itself: Bothreq.http.unset == req.http.unset andreq.http.unset ~ ".?" will be false.

Floating point infinities are signed, and compare as beyond the maximum and minimum values for FLOAT types, such that for any finite value: −∞ < n < +∞.

Note that as there are currently no numeric expressions in general, these operators are limited to use with specific operands. For example,var.i < 5 is permitted but2 < 5 is not.

Assignment operators

These operators allow values or the results of expressions to be assigned to a variable using theset statement.

OperatorPurposeExample
=Simple assignmentset var.my_string = "42";
+=Additionset var.my_integer += 4;
-=Subtractionset var.my_integer -= 4;
*=Multiplicationset var.my_integer *= 4;
/=Divisionset var.my_integer /= 4;
%=Remainderset var.my_integer %= 4;
|=Bitwise ORset var.my_integer |= 16;
&=Bitwise ANDset var.my_integer &= 65280;
^=Bitwise XORset var.my_integer ^= 16;
<<=Left shiftset var.my_integer <<= 4;
>>=Right shiftset var.my_integer >>= 4;
rol=Left rotateset var.my_integer rol= 4;
ror=Right rotateset var.my_integer ror= 4;
&&=Logical ANDset var.my_boolean &&= var.is_authenticated;
||=Logical ORset var.my_boolean ||= randombool(1,3);

Theset syntax is the only situation in which these operators may be used. Since the operator may only occur once in aset statement, these operators are mutually exclusive, so precedence between them is nonsensical.

The values the operators produce are used for assignment only. Theset statement assigns this value to a variable, but does not itself evaluate to a value.

If both operands (left-hand side and right-hand-side) areINTEGER, anINTEGER variant of the operator, where available, is used. The advantage of this is that the operation is faster and more precise (compared withFLOAT variants), but because the range ofINTEGER is more limited, overflows can happen. For example, the integer variants of the+=,-=, and*= operators wrap around as if they were unsigned 64-bit integers.

FLOAT arithmetic has special cases for operands which are NaN: Arithmetic operators evaluate to NaN when either operand is NaN.

FLOAT arithmetic has special cases for operands which are floating point infinities: In general all arithmetic operations evaluate to positive or negative infinity when either operand is infinity. However, some situations evaluate to NaN. Some of these situations aredomain errors, in which casefastly.error is set toEDOM accordingly. Others situations are not domain errors: ∞ − ∞ and 0 × ∞. These evaluate to NaN but do not setfastly.error.

The left-shift<<= is equivalent to multiplying by powers of two, but the high-order bits disappearing. For example,<<= 3 is equivalent to multiplying by8, shifting off the highest three bits.

The right-shift>>= is the arithmetic shift: that is, for both positive and negative integers it is equivalent to dividing by powers of two. For example,>>= 3 is equivalent to dividing by8, shifting off the lowest three bits. For negative integers this is also known as sign-extension: at the binary level, the sign bit (the highest bit) is copied to all the vacant bits.

The left-rotaterol= moves the bits towards the high end but the bits dropping off from the high end are rotated back to the low end. For example,rol= 1 moves the highest bit to the low end, and moves all the other bits up.

The right-rotateror= moves the bits towards the low end but the bits dropping off the low end are rotated back to the high end. For example,ror= 1 moves the lowest bit to the high end, and moves all the other bits down.

Shift and rotate operations with negative amounts perform the operation in the opposite direction. For example,<<= -3 is equivalent to>>= 3, androl= >> -3 is equivalent toror= 3.

The remainder operator%= performs the same function as amodulus operator for positive numbers.

For right operands larger than the bit width of INTEGER (64), shifts will yield zero for non-negative numbers, or -1 for negative numbers. Rotates will use the operand modulo the bit width of INTEGER. For example,>>= 99 will yield zero or -1, andror= 99 is equivalent toror= 35 because 99 % 64 is 35.

Logical AND and OR are provided in assignment form by the&&= and||= operators respectively. These areshort-circuit operators, seeconditional operators above.

Reserved punctuation

Punctuation appears in various syntactic roles which are not operators (that is, they do not produce a value).

PunctuationExample Uses
{}Block syntax
[]Stats ranges
()Syntax around if conditions, function argument lists
/Netmasks for ACLs
,Separator for function arguments
;Separator for statements and various other syntactic things
!Invert ACL entry
.To prefix fields in backend declarations
:Port numbers for backend declarations, and used in the stats syntax

The following lexical tokens are reserved, but not used:*&|>><<++--%


[8]ページ先頭

©2009-2025 Movatter.jp