Skip to content

Variables

Paul M Fox edited this page Nov 9, 2016 · 4 revisions

Variables in SCL are represented as a series of numbers, letters and underscores prefixed with a $. Variable names (the bit after the $) can start with any character, including numbers. Variables are case sensitive.

An alternate form of variable expression, used in complex operations, is of the form ${variable_name}. This is useful mostly for concatenation, but it can be used anywhere.

Even though SCL looks like a bit like functional language, the value of variables is mutable. You can assign one value in one place and then re-assign it in another. Variables will hold their value throughout their block scope.

Assignment

Assigning a value to a variable uses the familiar = operator:

$myVar = 1

If the variable exists in the current scope, then the value will be assigned to it; if the variable doesn't exist, then it will be created in the current scope. Variables must be assigned one at a time, and can have any literal or variable value, including heredocs.

Creation

If you want to create a 'new' variable (that is, one that unique within its block scope), you can use the := operator. This works in almost the same way as the = operator, except that it always creates a variable (or replaces one with the same name).

An example might be:

$myVar := 1

See blocks and scope to understand the utility of creating new variables.

Conditional assignment or creation

Sometimes you may want to assign or create a variable with a given value unless it has already been declared. This comes up often in web applications, because environment variables can be passed through to the parser (see below). It's pretty common to be in the situation where you want to assign a value unless the environment has a better value for it.

In this circumstance the ?= operator comes to the rescue:

$MY_ENV_VAR ?= "some value"

If MY_ENV_VAR were declared in the environment, it would have been passed to the SCL parser as an SCL variable, and thus have a value which would be preserved. If no variable called $MY_ENV_VAR has been declared, then $MY_ENV_VAR will be assigned the string value "some value".

Interpolation

Variables are interpolated at compile time, meaning that the value they represent will be replaced with their literal value.

A simple use of a variable might look like this:

$myVar1 = "value"
$myVar2 = value

block
    key = $myVar1 // This is interpolated to 'key = "value"'

block
    key = $myVar2 // This is interpolated as 'key = value'

They can be used anywhere in SCL, including block declarations. For example, this is valid SCL:

$myBlockName = block
$myValue     = "value"

$myBlockName
	value = $myValue

// This is interpolated as
// block
//    value = "value"

Some interpolation won't work using the simple syntax. Consider, for example, how to concatenate two variables in a string, joined by an underscore. You might want to approach it like this:

$var1 = 1
$var2 = 2
something = "$var1_$var2"

That won't work (and in fact SCL will give an error) because an underscore is a valid character in variable names, so there's no way for SCL to know when you have finished typing your variable name. Instead, you can use the alternate variable expression form:

$var1 = 1
$var2 = 2
something = "${var1}_${var2}"

which will yield:

something = "1_2"

Escaping

Variables can be escaped by placing a backlash in front of the leading dollar sign, or by using two dollar signs. Code wrapped in backticks will be treated as an HCL literal — a bit like a one-line heredoc.

These are all valid escapes:

// This outputs the string `$var`
blackslash_escape = "\$var"

// and so does this
doubledollar_escape = "$$var"

// Slashes themselves need to be escape
$escaped_slash = \\n is an escaped newline

// ...dollars on their own don't need to be escaped
$lonely_dollar = `$ on its own`

// ...and everything can be escaped with backticks
everything = "`$escaped_slash, $lonely_dollar` = $escaped_slash, ${lonely_dollar}"

// Finally, backticks can also be escaped
backticks = "\`hello\`"

produces the HCL:

blackslash_escape = "$var"
doubledollar_escape = "$var"
everything = "$escaped_slash, $lonely_dollar = \n is an escaped newline, $ on its own"
backticks = "`hello`"

Clone this wiki locally