# Ada Programming/Libraries/Ada.Environment Variables

This language feature is only available from Ada 2005 on.
Ada.Environment_Variables is a unit of the Predefined Language Environment since Ada 2005.

##### Introduction

The Ada.Environment_Variables package allows an Ada program to read, write and modify environment variables. Exactly what constitutes an environment variable is implementation defined. All the code in this article is compiled and executed on a Slackware Linux system. The compiler used is GNATMAKE GPL 2008 (20080521).

##### Using Ada.Environment_Variables

Environment variables are simple key/value pairs. Both the key and the value are strings. With the Environment_Variables package we can check if a key exists, we can set new key/value pairs, we can delete keys, we can read values, and we can iterate through all the key/value pairs (as can be seen from the very straightforward specification of the package):
All of the following examples will be built around this simple core program:
Simply insert the code from the following examples after the IO.Put_Line line. So, let's move on and see how we can read an environment value using Ada.

##### Ada.Environment_Variables.Value

The specification for Value looks like this:
Reading an environment variable is done using the Value function. It takes a string as its sole parameter and returns the value of a matching environment variable. Try adding this to the core program:
When running this on my machine, I get the following output:
As you can see, a Constraint_Error is raised if the given Name parameter does not match any existing environment variable. If the notion of environment variables isn't supported in the target operating system, a Program_Error is raised instead.
Ada.Environment_Variables.Value Example Source

##### Ada.Environment_Variables.Exists

The specification for Exists looks like this:
The Exists function is used to check whether or not a given environment variable name is known to the operating system. If it is, the function will return boolean True, otherwise it returns boolean False.
The resulting output on my computer is:
The manual does not say anything about what happens if the execution environment lacks support for environment variables in this case, but I suspect a Program_Error is raised, as with Value.
Ada.Environment_Variables.Exists Example Source

##### Ada.Environment_Variables.Set

The specification for Set looks like this:
With Set we can define new and alter existing environment variables:
The output of this is:
If setting or altering a given environment variable is prohibited by the operating environment, a Constraint_Error is raised. As with Value, a Program_Error is raised if the execution environment has no notion of environment variables.
Ada.Environment_Variables.Set Example Source

##### Ada.Environment_Variables.Clear

The specification for Clear looks like this:
Deleting environment variables is done using one of the Clear procedures. If a Name parameter is given, Clear will try to delete all environment variables with that name. If no Name parameter is given, Clear will try to delete all existing environment variables.
First lets see how Clear works when given a Name parameter:
And the output:
Now lets see Clear in action with no Name parameter:
And the output is:
Exactly as expected.
Ada.Environment_Variables.Clear Example Source

##### Ada.Environment_Variables.Iterate

The specification for Iterate looks like this:
Iterate enables us to serially act upon all the environment variables that are available to the program at the time of execution. We can do this by creating a procedure that takes two String parameters and then give Iterate access to this procedure.
Such a program could look something like this:
Here's a small sample of the output generated by the above program on my system:
There's a very important note about Iterate in the manual. It states:
   Making calls to the procedures Set or Clear concurrently with calls to any subprogram of package Environment_Variables, or to any instantiation of Iterate, results in erroneous execution. Making calls to the procedures Set or Clear in the actual subprogram corresponding to the Process parameter of Iterate results in erroneous execution.
So you cannot call either Set or Clear while using Iterate, at least not without risking a certain amount of undefined behavior.
Ada.Environment_Variables.Iterate Example Source

## See also
### Wikibook

* Ada Programming/Libraries/Ada

### External examples

* Search for examples of Ada.Environment_Variables in: Rosetta Code, GitHub (gists), any Alire crate or this Wikibook.
* Search for posts related to Ada.Environment_Variables in: Stack Overflow, comp.lang.ada or any Ada related page.

### Ada Reference Manual
#### Ada 2005

* A.17: The Package Environment_Variables

#### Ada 2012

* A.17: The Package Environment_Variables

### Open-Source Implementations

FSF GNAT
* Specification: a-envvar.ads
* Body: a-envvar.adb
drake
* Specification: environment/a-envvar.ads
* Body: environment/a-envvar.adb