TEAM-ADA Archives

Team Ada: Ada Programming Language Advocacy


Options: Use Forum View

Use Monospaced Font
Show Text Part by Default
Condense Mail Headers

Message: [<< First] [< Prev] [Next >] [Last >>]
Topic: [<< First] [< Prev] [Next >] [Last >>]
Author: [<< First] [< Prev] [Next >] [Last >>]

Print Reply
"Team Ada: Ada Advocacy Issues (83 & 95)" <[log in to unmask]>
Matthew Heaney <[log in to unmask]>
Tue, 20 Apr 1999 07:53:15 -0500
"David C. Hoos, Sr." <[log in to unmask]>
text/plain; charset="iso-8859-1"
"David C. Hoos, Sr." <[log in to unmask]>
text/plain (82 lines)
----- Original Message -----
From: Matthew Heaney <[log in to unmask]>
To: <[log in to unmask]>
Sent: Tuesday, April 20, 1999 6:48 AM
Subject: Re: Environment Variables

> "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.
Well.. what's the "proper" way seems to me to depend on what one's
trying to do. If there is a default value which one wants to use in the
absence of the environment variable, then the use of the optional
Default parameter is certainly the proper way.

On the other hand, if the program absolutely requires that the variable
be defined, then the Is_Environment_Variable is the sure way to
insure this.

In addition, there are cases where the value of the variable is not
important at all -- just that it be defined -- e.g. an environment
variable named "DEBUG."

Unless one can guarantee that the user's environment _does not_
have a value identiacal to the default value, there is no way to
discriminate between the variable not being set, and it being
set to the default value.

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

DELAY_TIME is an example of where a default value is clearly acceptable
in the absence of the environment variable's definition.

I use this in many cases -- e.g. where the default port returned by
getservbyname can be used by a server, but where for testing purposes
one might want to be able to define an alternate port by means of an
environment variable.

But in many cases, there is no substitute for the variable's
definition in the execution environment.