Skip to content

Instantly share code, notes, and snippets.

@tueda
Created September 23, 2013 12:40
Show Gist options
  • Save tueda/6669864 to your computer and use it in GitHub Desktop.
Save tueda/6669864 to your computer and use it in GitHub Desktop.
LoadVariable[var, env, default] tries to load a variable from the environment variables, if the variable is undefined.
(*
* Defines the given symbol if it has not been defined. For the value of the
* symbol, this function tries to load it from the given environment variable.
* The optional third argument is the default value which is used when the
* environment variable is undefined. This function returns the value of the
* symbol if defined, otherwise returns $Failed.
*)
LoadVariable[symbol_, env_String, default_:Null] := (
If[!ValueQ[symbol],
If[Environment[env] =!= $Failed,
symbol = Environment[env];
,
If[default =!= Null,
symbol = default;
];
];
];
If[ValueQ[symbol],
symbol
,
$Failed
]
);
SetAttributes[LoadVariable, HoldFirst];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment