Skip to content

@Env

The type of env, which reads, writes, iterates, spreads, and destructures like a Dict of variable names and values.

Assigning nil unsets a variable. Reading a name that is not set raises; use get for a default instead.

Inherits from: std.Index[Str, Str], std.Assign[Str, Str | nil], std.Spread[Str, Str], std.Unpack[Str, Str], std.BaseIterable[Tuple[Str, Str]]

Methods

(call)[R] vars body -> R

Calls body with overrides from a dict in place, restoring them when it returns.

Parameters

NameTypeDescription
vars Dict[Str | Sym, Str | :INHERIT: | nil] The overrides, as for keyword arguments.
body (() -> R) The block to call.

(call)[R] body ...vars -> R

Calls body with the variables given as keyword arguments in place, restoring them when it returns.

A nil value unsets a variable, and :INHERIT: captures the variable's current value in the calling strand.

Parameters

NameTypeDescription
body (() -> R) The block to call.
...vars Overrides by variable name.

Example

env PATH: "/opt/bin:$(env["PATH"])" do
  run tool

env {LANG: "C", TZ: nil} do
  run tool

get[D] name … -> (Str | D | nil)

Reads a variable, returning nil rather than raising when it is unset.

Parameters

NameTypeDescription
name Str The variable name.
:default? D The value to return when the variable is unset.
:else? (() -> D) A block whose result to return when the variable is unset and there is no default.

Example

let home = env.get HOME default: /root
let tmp = env.get TMPDIR else: do
  fs.temp_dir()