Usage example
This section contains examples using the UNIXEnvironment class. Evaluate each of the expressions by highlighting them and selecting Display from the Edit menu.
The UNIXEnvironment class retains a copy of the environment that was initially available. This is useful when the current environment has been changed and access to the original environment is required.
"Retrieve the user's start up environment."
UNIXEnvironment startUpEnvironment
In most situations, processes created from within Smalltalk use the current environment. The current environment can be accessed using the current method.
"Retrieve the current environment."
UNIXEnvironment current
If a custom environment is desired, a new UNIXEnvironment can be created and defined.
"Create a new environment and set some key-value pairs."
| env |
env := UNIXEnvironment new.
env at: 'TEST' put: '123';
at: 'FRED' put: 'HELLO'.
env
Accessing the environment information is accomplished using the at: method.
"Query the value of a particular environment variable from the user's
start up environment."
UNIXEnvironment startUpEnvironment at: 'HOME'
The keys method answers all of the keys in an instance of UNIXEnvironment. This method is useful when checking a users configuration.
"Retrieve the names of all environment variables from the user's
start up environment."
UNIXEnvironment startUpEnvironment keys
If you are more familiar with the C language, you might want to use the getenv: message to access the current environment instead of querying the current environment and using the at: message. The messages getnev and at: are functionally equivalent for operations on the current environment.
"Query the Smalltalk AIX process environment with a specific key.
Equivalent to C function."
UNIXEnvironment getenv: 'HOME'
"Query an environment using an instance of UNIXEnvironment."
UNIXEnvironment current at: 'HOME'
Using the putenv: message is similar to the getenv: message except it is used to set a value instead of query it. The messages putenv and at:put: are functionally equivalent for operations on the current environment.
"Set a key-value pair in the Smalltalk UNIX process environment.
Equivalent to C function."
UNIXEnvironment putenv: 'TEST=1234'
"Set a key-value pair for an instance of UNIXEnvironment."
UNIXEnvironment current at: 'TEST' put: '1234'.
Last modified date: 01/29/2015