cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Help us improve the PTC Community by taking this short Community Survey! X

ACL test for environment variable

ptc-953926
1-Visitor

ACL test for environment variable

Hi,

In my instance.acl script, I'm trying to test for the existence of an external variable and source the appropriate file depending on whether or not it's defined. This is how I'm trying to do it, but it doesn't seem to be working:

if ($main::ENV["XPS3_HOME"] != ") {
source $XPS3_HOME\XPSDocs\entities\commonStartup.acl
} else {
source $XPS_HOME\XPSDocs\entities\commonStartup.acl
};

That is, when "XPS3_HOME" is not defined, I'm getting an error during startup indicating that at line 2 it's trying to use an undefined variable. Anyone know the correct way of doing this test?

Thanks
Dave
Siemens
12 REPLIES 12

Hi Dave--

Try defined($main::ENV["XPS3_HOME"]).

--Clay

Clay Helberg
Senior Consultant
TerraXML

I think you'll want to use the defined function (this is off the cuff
without testing)...

Else if(!defined($main::ENV[XPS3_HOME])) {
Source ...
}

Clay types faster than I do 🙂

But to clarify now that I look at it, you'll want to put the defined
statement first, so that it doesn't error before getting to the else
if().

I'm not sure why I use this construction, but I assign the value of the
environment variable to an ACL variable before using it. Maybe the problem
you're having is what drove me there. Try:

local yourvar = $main::ENV["XPS3_HOME"]

if ($yourvar !=") ...



Hi Clay,

That looks like exactly what I need. So, I changed the ACL to:

if(!defined($main::ENV['XPS3_HOME'])) {
source $XPS_HOME\XPSDocs\entities\commonStartup.acl
} else {
source $XPS3_HOME\XPSDocs\entities\commonStartup.acl
};

It still drops into the second "source" and reports the undefined variable.

Doesn't seem like this should be that hard! Any other suggestions?

Dave

Well, this seems to work:

global xpshome = $main::ENV["XPS3_HOME"]
if($xpshome == ") {
source $XPS_HOME\XPSDocs\entities\commonStartup.acl
} else {
source $xpshome\XPSDocs\entities\commonStartup.acl
};

Unless someone knows of a less-confusing method, I'll go with this.

Thanks, Paul!

Dave

On Mon, Sep 26, 2011 at 1:49 PM, Hintz, David <-> wrote:
> Hi Clay,
>
> That looks like exactly what I need. So, I changed the ACL to:
>
> if(!defined($main::ENV['XPS3_HOME'])) {
> source $XPS_HOME\XPSDocs\entities\commonStartup.acl
> } else {
> source $XPS3_HOME\XPSDocs\entities\commonStartup.acl
> };
>
> It still drops into the second "source" and reports the undefined variable.
>
> Doesn't seem like this should be that hard! Any other suggestions?

DRY...

local xps_home = $main::ENV['XPS_HOME']; # or global..., if not in a function
if (defined($main::ENV['XPS3_HOME'])) { xps_home = $main::ENV['XPS3_HOME']; }
source $xps_home\XPSDocs\entities\commonStartup.acl;

-Brandon Smiley Happy

> Dave
> -----Original Message-----
> From: Clay Helberg [
>
> --Clay
>
> Clay Helberg
> Senior Consultant
> TerraXML
>
>
> -----Original Message-----
> From: Hintz, David [

> startup indicating that at line 2 it's trying to use an undefined
> variable. Anyone know the correct way of doing this test?
>
> Thanks
> Dave
> Siemens
>
> -----End Original Message-----
>
>
> -----End Original Message-----
>
> -----End Original Message-----
>
>

Thanks, Brandon. That looks pretty much like what I came up with.

Hi Dave--

After taking a closer look at your code, I think the problem is in your
source statement rather than in the test. I tried the following code:

if(!defined($main::ENV['XPS3_HOME'])) {
response("XPS3 not found");
} else {
response("XPS3 was found");
};

It gives the correct response under both conditions, i.e. if environment
variable "XPS3_HOME" is defined, it says it's found, otherwise it says
not found.

But in your code, you are concatenating $XPS3_HOME, which is not the
same as $main::ENV['XPS3_HOME']. Try changing your code to something
like this:

if(!defined($main::ENV['XPS3_HOME'])) {
source $main::ENV['XPS_HOME']\XPSDocs\entities\commonStartup.acl
} else {
source $main::ENV['XPS3_HOME']\XPSDocs\entities\commonStartup.acl
};

Clay Helberg
Senior Consultant
TerraXML

Reading your messages, I suddenly doubt.

We use an environment variable SIGMA witch is a network path, or does not exist.

In our scripts, we just use the very simple code :
If ($SIGMA=="){
do_something;
}else{
source $SIGMA/myscript.acl
}

Does it work only because we are not inside a function (we are in a script directly called by instance.acl)
Furthermore, defined($SIGMA) return 0, even if the environment variable has a value.
We use Epic 52, maybe we'll have bad surprise with newer versions...

Yves Deniard
MBDA
1 avenue Réaumur
92358 Le Plessis-Robinson cedex
France
tél.:01.71.54.27.39
yves.deniard@mbda-systems-com

-----Message d'origine-----
De : Clay Helberg [
} else {
response("XPS3 was found");
};

It gives the correct response under both conditions, i.e. if environment variable "XPS3_HOME" is defined, it says it's found, otherwise it says not found.

But in your code, you are concatenating $XPS3_HOME, which is not the same as $main::ENV['XPS3_HOME']. Try ch...











Hi Yves--

Very interesting. I tested it out and you are absolutely right. I stand corrected, and I apologize for leading Dave astray on that point. I hadn't seen that usage before, so thank you for teaching me something new.

One thing that seems to make a significant difference in how the variables are interpreted is whether or not the code is inside a function. If it's inside a function, then Dave's code using defined() should work. But if it's not inside a function, then the interpreter gets confused about the variables that don't exist and throws the error.

Paul Grosso suggested to me offline to use an execute wrapper to defer variable substitution until execution time. So you could solve the problem either by embedding the code in a function and calling the function, or by adding execute to the source commands, like so:

if(!defined($main::ENV['XPS3_HOME'])) {
execute source $XPS3_HOME\XPSDocs\entities\notfound.acl
} else {
execute source $XPS_HOME\XPSDocs\entities\found.acl
};

Hope that helps clarify things.

--Clay

Clay Helberg
Senior Consultant
TerraXML

Exact Clay, we also often use (without always be obliged to):

exec source $SIGMA/myscript.acl;

Thanks for all your answers.

Yves Deniard
MBDA
1 avenue Réaumur
92358 Le Plessis-Robinson cedex
France
tél.:01.71.54.27.39
yves.deniard@mbda-systems-com

-----Message d'origine-----
De : Clay Helberg [
Announcements

Top Tags