pritesh has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I am running Perl 5.8.9 (I know its old, but thats what installed on the server and I dont have access rights to upgrade) on a Windows 2003 server. I am trying to write a script to set a variable symcli_connect to a value so that other commands can use that value. the symcli value is used by EMC's Symmtrix array. Here's the script:
#!/usr/bin/perl use warnings; use strict; print "Choose The Datacenter\n"; print "1. \Primary\n"; print "2. \Secondary\n"; print "Enter 1 or 2:"; $ENV{"SYMCLI_CONNECT"} = ""; our $datacenter=<STDIN>; if ($datacenter == 1) { print "\t\t\t***Setting Environment for Primay***\n"; $ENV{"SYMCLI_CONNECT"} = "primary_server"; system ("symcfg list"); } elsif ($datacenter == 2) { print "\t\t\t\t***Setting Environment for Secondary***\n"; $ENV{"SYMCLI_CONNECT"} = "secodnary_server"; system ("symcfg list"); } else { print "Please enter only 1 or 2 \n"; }
Problem is, if I choose the Primary data center, it sets the symcli value correct. If, I run the script again, it outputs as if its set the secondary_server value, but when I try checking the value of symcli_connect, its still the first one. As a result its not working the way its supposed to. Looks like the value of $ENV variable is not being passed outside the if loop. I am a beginner....to Perl and programming as well. Can someone let me know where I am going wrong ?

Replies are listed 'Best First'.
Re: Passing value from IF loop
by jethro (Monsignor) on Sep 24, 2010 at 13:11 UTC
    Did you check inside your script or after the script was run on the command line?

    When you set environment variables, they only get inherited by sub-processes of the process that is setting them. At least that is how it works in unix and I assume also in windows.

Re: Passing value from IF loop
by Utilitarian (Vicar) on Sep 24, 2010 at 15:10 UTC
    Setting of the environment variable is not being passed outside the current environment.

    If you are asking what I think you are asking perhaps write the value to a file if you want persistence between runs.

    print "Good ",qw(night morning afternoon evening)[(localtime)[2]/6]," fellow monks."

      I agree with Utilitarian's comments about the current environment. To illustrate his point, you can do the following test:

      1. Open two command prompts.
      2. In the first command prompt, add/modify an environment variable.
      3. In the second command prompt, check on that environment variable. It won't see any changed values or any new variables.
      4. Open a third command prompt and check on the environment variable added/modified in step 2. This time, the addition/change should be detected.

      Personally, I am unaware of how to 'reset' the environment for command prompt or script, but that's not to say it can't be done. Short of doing that, I think Utilitarian's suggestion of writing to a file would be a good idea.

Re: Passing value from IF loop
by toolic (Bishop) on Sep 24, 2010 at 13:26 UTC
    It is unclear to me what you have tried. If you can edit the symcfg program, you should edit it and print all the environment variables. This will confirm if the variable is set properly. For example, if symcfg is Perl code, add:
    use Data::Dumper; print Dumper(\%ENV);
Re: Passing value from IF loop
by JavaFan (Canon) on Sep 24, 2010 at 15:04 UTC
    Looks like the value of $ENV variable is not being passed outside the if loop.
    Oh, it does. But it seems to be that you are thinking it should propagate back to the parent process. It doesn't. (Think about it, if it would, then the parent would propagate it back to *its* parents process, and that again to its parent process, all the way to the top level process (init(1) on UNIX systems). And then I guess they should propagate it down to their children - meaning the entire system would share all environment variables).
Re: Passing value from IF loop
by afoken (Chancellor) on Sep 25, 2010 at 04:53 UTC

    Just a small note:

    ... the if loop ...

    if is not a loop. It is typically followed by a block, like most loop constructs (for / foreach, while, repeat (not available in Perl)). But unlike loop constructs that repeatedly execute the code block that follows them (mathematically: zero or more times), if executes the code block either once or not at all, depending on the condition.

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)