Skip to main content
1-Visitor
September 26, 2011
Question

ACL test for environment variable

  • September 26, 2011
  • 12 replies
  • 2298 views
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

    18-Opal
    September 26, 2011
    Hi Dave--

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

    --Clay

    Clay Helberg
    Senior Consultant
    TerraXML

    September 26, 2011
    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 ...
    }
    September 26, 2011
    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().
    1-Visitor
    September 26, 2011
    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 !=") ...



    1-Visitor
    September 26, 2011
    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
    1-Visitor
    September 26, 2011
    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
    1-Visitor
    September 26, 2011
    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-----
    >
    >
    1-Visitor
    September 26, 2011
    Thanks, Brandon. That looks pretty much like what I came up with.
    18-Opal
    September 26, 2011
    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

    1-Visitor
    September 27, 2011
    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 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