The Process Dictionary
Each process in Erlang has its own private data store called the process dictionary. The process dictionary is an assciative array (in other languages this might be called a map, hashmap, or hash table) coposed of a collection of keys and values. Each key has only one value.
The dictionary can be manipulated using the following BIFs:
@spec put(Key, Value) -> OldValue.
Add a Key, Value association to the process dictionary. The value of put is OldValue, which is the previous value associated with Key. If there was no previous value, the atom undefined is returned.
@spec get(Key) -> Value.
Look up the value of Key. If there is an association Key, Value association in the dictionary, return Value; Otherwise, return the atom undefined.
@spec get() -> [{Key, Value}].
Return the entire dictionary as a list of {Key, Value} tuples.
@spec get_keys(Value) -> [Key].
Return a list of keys that have the values Value in the dictionary.
@spec erase(Key) -> Value.
Return the value associated with Key or the atom undefined if there is no value associated with Key. Finally, erase the value associated with Key.
@spec erase() -> [{Key, Value}].
Erase the entire process dictionary. The return value is a list of {Key, Value} tuples representing the state of the dictionary before it was erased.
If you use the process dictionary, your code will no longer be side effect free, and all the benefits of using nondestructive variables.
Using the process dictionary can introduce subtle bugs into your program and make it difficult to debug. One form of usage that I do approve of is to use the processes dictionary to store “write-once” variables. If a key acquires a value exactly once and does not change the value, then storing it in the process dictionary is sometimes acceptable.