Ada Programming/Libraries/Ada.Environment Variables

is a unit of the Predefined Language Environment since Ada 2005.

Introduction
The 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.

Using Ada.Environment_Variables
Environment variables are simple key/value pairs. Both the key and the value are strings. With the  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  line. So, let's move on and see how we can read an environment value using Ada.

Ada.Environment_Variables.Value
The specification for  looks like this:

Reading an environment variable is done using the  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:

Environment_Variables test /usr/local/bin:/usr/bin:/bin:/usr/games:/usr/lib/java/bin:/usr/lib/java/jre/bin:/usr/lib/qt/bin:.:/usr/local/texlive/2008/bin/i386-linux/ No such environment variable.

As you can see, a  is raised if the given   parameter does not match any existing environment variable. If the notion of environment variables isn't supported in the target operating system, a  is raised instead.

Ada.Environment_Variables.Exists
The specification for  looks like this:

The  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, otherwise it returns boolean.

The resulting output on my computer is:

Environment_Variables test EXISTS! DOES NOT EXIST!

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  is raised, as with Value.

Ada.Environment_Variables.Set
The specification for  looks like this:

With  we can define new and alter existing environment variables:

The output of this is:

Environment_Variables test DOES NOT EXIST! FooBar FooBar again

If setting or altering a given environment variable is prohibited by the operating environment, a  is raised. As with Value, a  is raised if the execution environment has no notion of environment variables.

Ada.Environment_Variables.Clear
The specification for  looks like this:

Deleting environment variables is done using one of the  procedures. If a  parameter is given,   will try to delete all environment variables with that name. If no  parameter is given,   will try to delete all existing environment variables.

First lets see how  works when given a   parameter:

And the output:

Environment_Variables test This is Foo Environment variable Foo does not exist

Now lets see  in action with no   parameter:

And the output is:

Environment_Variables test This is Foo This is Bar Foo and Bar are both gone!

Exactly as expected.

Ada.Environment_Variables.Iterate
The specification for  looks like this:

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  parameters and then give   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:

JAVA_HOME=/usr/lib/java HOME=/home/thomas SHELL=/bin/bash

There's a very important note about  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  or   while using , at least not without risking a certain amount of undefined behavior.

Specification
Preelaborate Environment_Variables Value Name  String  String Exists Name  String  Boolean Set Name   String Value  String Clear Name  String Clear Iterate Process Name   String Value  String AdaEnvironment_Variables

Wikibook

 * Ada Programming/Libraries/Ada