TEAM-ADA Archives

Team Ada: Ada Programming Language Advocacy

TEAM-ADA@LISTSERV.ACM.ORG

Options: Use Forum View

Use Monospaced Font
Show HTML Part by Default
Condense Mail Headers

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

Print Reply
X-cc:
Steffen Bretz <[log in to unmask]>
Sender:
"Team Ada: Ada Advocacy Issues (83 & 95)" <[log in to unmask]>
Subject:
From:
Matthew Heaney <[log in to unmask]>
Date:
Tue, 20 Apr 1999 04:48:59 -0700
In-Reply-To:
"David C. Hoos, Sr."'s message of "Tue, 20 Apr 1999 06:12:41 -0500"
X-To:
"David C. Hoos, Sr." <[log in to unmask]>
Reply-To:
Matthew Heaney <[log in to unmask]>
Parts/Attachments:
text/plain (46 lines)
"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.

ATOM RSS1 RSS2