Environment Variables in Standard ML
Our program demonstrates how to work with environment variables in Standard ML. Environment variables are a universal mechanism for conveying configuration information to programs.
To set a key/value pair, we use the setenv
function. To get a value for a key, we use the getenv
function. This will return an empty string if the key isn’t present in the environment.
We use OS.Process.getEnv
to list all key/value pairs in the environment. This returns a list of strings in the form (KEY, value)
. Here we print all the keys.
Running the program shows that we pick up the value for FOO
that we set in the program, but that BAR
is empty.
The list of keys in the environment will depend on your particular machine.
If we set BAR
in the environment first, the running program picks that value up.
Note that Standard ML doesn’t have built-in functions for environment variables like Go does. We’ve created wrapper functions to mimic the behavior of Go’s os.Setenv
and os.Getenv
. Also, Standard ML doesn’t have a direct equivalent to Go’s strings.SplitN
, so we’ve adjusted our approach to printing environment variables accordingly.