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

Hi Monks, Within my perl program, I want to setenv PEOPLE 5033 and "source" a directory to run these tools. I noticed that when I finish running the program and look back at the terminal window. It does not recognize echo $PEOPLE in the terminal.
`setenv PEOPLE 5033`
how come this doesn't work? what can i do to get this to work?

Replies are listed 'Best First'.
Re: Problems with Setenv within Perl
by Fletch (Bishop) on Jul 21, 2004 at 15:50 UTC

    If you mean you want to modify the parent shell's environment it's because you can't (directly) do that; see perldoc -q environment. The work around is to output what the parent should set (e.g. print "setenv PEOPLE 5033\nsetenv FOO bar\n") and then have the parent shell eval that output instead.

Re: Problems with Setenv within Perl
by pbeckingham (Parson) on Jul 21, 2004 at 15:49 UTC

    Your code

    `setenv PEOPLE 5033`
    launches a shell, sets the environment variable in that shell, then the shell quits, and the var is gone. To preserve the value, I believe your only option is to set the var, then exec, but that's hardly a solution for what (little) you describe.

      It's not necessary to use the shell to set a variable. Just:
      $ENV{PEOPLE}=5033;
      should suffice.
        Yes, setting the value of %ENV will automatically set the enverioment variable outside the process, and after the process die it will restore the previous values.

        Note that some keys can't be set, for example, on Win2K we can't set wrong keys like PATH, PATHEXT e HOMEPATH. But if you are just setting a new key it will work just fine.

        Graciliano M. P.
        "Creativity is the expression of the liberty".

Re: Problems with Setenv within Perl
by thor (Priest) on Jul 21, 2004 at 18:17 UTC
    Different shells have different semantics for setting environment variables. Consider:
    #csh/tcsh
    setenv PEOPLE 5033
    
    #bash/ksh
    export PEOPLE=5033
    
    #sh
    PEOPLE=5033
    export PEOPLE
    
    #perl
    $ENV{PEOPLE} = 5033
    
    As I allude to above, environment variables in perl are stored in a hash called %ENV. You are free to modify this hash as you see fit.

    thor