Local variables
Fastly VCL supports variables for storing temporary values during request processing.
Declaring a variable
Variables must be declared before they are used, usually at the beginning of a function or block. Block scope variables can only be used within the braced block where they are declared. Local scope variable declarations apply to an entire function's scope even if a variable is declared within a block.
Variables start with var. and their names consist of characters in the set [A-Za-z0-9._-]. (: is explicitly disallowed.)
The declaration syntax for a block scope variable is:
declare block var.<name> <type>;
The declaration syntax for a local scope variable is:
declare local var.<name> <type>;
Variables can be assigned a value at declaration time. The syntax for this is:
declare block var.<name> <type> = <assignment-expr>;
Variable types
Variables can be of the following types:
- BOOL
- INTEGER
- FLOAT
- TIME (absolute time)
- RTIME (relative time)
- STRING
- REGEX
Variables are initialized to the zero value of the type:
0for numeric typesfalsefor BOOLNULLfor STRING- An unsatisfisable regex for REGEX.
An unsatisfiable regex is a regex that will never match any string.
Usage
Scope
Block scope variables are scoped to the nearest enclosing set of {} braces, for example from an if statement or a subroutine. Braces can also be used on their own to limit the scope of variables.
{ declare block var.s STRING; # var.s is in scope}# var.s is no longer in scopeLocal scope variables are always scoped to the subroutine they are declared in.
{ declare local var.s STRING;}# var.s is in scopeBoolean variables
Boolean assignments support boolean variables on the right-hand side as well as BOOL-returning functions, conditional expressions, and the true and false constants.
declare block var.boolean BOOL;
# Inline assignment as part of a declarationdeclare block var.boolean BOOL = true;
# BOOL assignment with RHS variableset var.boolean = true;set req.esi = var.boolean;set resp.http.Bool = if(req.esi, "y", "n");
# BOOL assignment with RHS functionset var.boolean = http_status_matches(resp.status, "200,304");
# BOOL assigment with RHS conditionalset var.boolean = (req.url == "/");
# not-set check, like 'if (req.http.Foo) { ... }'set var.boolean = (req.http.Foo);Numeric variables
Numeric assignment and comparison support numeric variables (anything except STRING or BOOL) on the right-hand side, including conversion in both directions between FLOAT and INTEGER types, rounding to the nearest integer in the FLOAT to INTEGER case.
Invalid conditions or domain errors like division by zero will set "fastly.error".
declare block var.integer INTEGER;declare block var.float FLOAT;
# Inline assignment as part of a declarationdeclare block var.float FLOAT = 1.3;declare block var.integer INTEGER = req.bytes_read;
# Numeric assignment with RHS variable and# implicit string conversion for headerset var.integer = req.bytes_read;set var.integer -= req.body_bytes_read;set resp.http.VarInteger = var.integer;
# Numeric comparison with RHS variableset resp.http.VarIntegerOK = if(req.header_bytes_read == var.integer, "y", "n");String variables
String assignments support string concatenation on the right-hand side.
declare block var.restarted STRING;
# Inline assignment as part of a declarationdeclare block var.s STRING = "I am a string!";
# string concatenation on RHSset var.restarted = "Request " if(req.restarts > 0, "has", "has not") " restarted.";Time variables
Time variables support both relative and absolute times.
declare block var.time TIME;declare block var.rtime RTIME;
# Inline assignment as part of a declarationdeclare block var.rtime RTIME = req.grace;declare block var.time = std.time("Fri, 10 Jun 2016 00:02:12 GMT", now);
set req.grace = 72s;set var.rtime = req.grace;set resp.http.VarRTime = var.rtime;
set var.time = std.time("Fri, 10 Jun 2016 00:02:12 GMT", now);set var.time -= var.rtime;# implicit string conversion for headerset resp.http.VarTime = var.time;IP Variables
Local variables support both IPv4 and IPv6:
declare block var.ipv4 IP = "192.168.0.1";declare block var.ipv6 IP = "::1";