"David C. Hoos, Sr." <[log in to unmask]> writes:

> Matthew Heaney's solution works if the environment variable exists,
> but raises an exception if it does not.

True, but that's a consequence of the decision to return an Ada string
as the return type.  Better is to return chars_ptr type, and let the
caller test the return value himself.

I illustrated how to do this in my follow-up post.


> This binding has the Boolean function
> POSIX.Process_Environment.Is_Environment_Variable
> which permits testing for existence before getting the value
> with a call to POSIX.Process_Environment.Environment_Value_Of.

This is the wrong way to use those functions, because you'd be
translating the env var twice (maybe - depends how it's implemented).

The proper way to handle this is to use the optional parameter (called
"Default", I think) of Env_Val_Of, to specify the value to return if the
env var isn't defined.

I use this technique all the time.  For example,

declare

  Env_Name : constant POSIX_String := To_POSIX_String ("DELAY_TIME");

  Default : constant POSIX_String := To_POSIX_String ("5.0");

  Env_Val_As_PString : constant POSIX_String :=
    Environment_Value_Of (Env_Name, Default);

  Env_Val : constant String := To_String (Env_Val_As_PString);

begin

  Delay_Time := Duration'Value (Env_Val);

end;


You do not need to call Is_Env_Var.