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

i want to create a environment variable, say TESTDIR at run time and set it to some value , say /prj/script. i do not want to create it from outside, my script should create it. also this variable should be available to other script where this env variable is used .
  • Comment on create environment variable at run time

Replies are listed 'Best First'.
Re: create environment variable at run time
by Errto (Vicar) on Feb 03, 2005 at 06:18 UTC
    also this variable should be available to other script where this env variable is used .

    This isn't possible in a straightforward way, as brian_d_foy mentioned. The reason lies in the way environment variables work. Environment variables belong not to a user, or to a "login session," but to a process. Any child process of that process then inherits those variables. So if from your Perl program you call another program through system, qx``, or the piped form of open, then that program will inherit any environment variables that your program has set using %ENV. But any program not launched in that way will not be able to see them without substantial additional work. Also, the follwing code:

    open my $fh, "| someotherprog.pl" or die $!; $ENV{FOO} = 'bar';
    will not cause someotherprog.pl to see the change to the environment variable FOO.

    Win32 has a separate notion of "persistent" environment variables stored in the registry. I am not sure how to manipulate them with Perl, but I am fairly certain that modifying %ENV alone will not do it.

Re: create environment variable at run time
by brian_d_foy (Abbot) on Feb 03, 2005 at 05:36 UTC

    Environment variables live in the %ENV hash. You can create them as you like, and the process and its children see that value.

    $ENV{TESTDIR} = './t';

    You can't set this value from one process and see it in some other process without some sort of IPC trick, though. If at all possible, you should try to set it before you run the script according to whatever your shell allows.

    --
    brian d foy <bdfoy@cpan.org>
Re: create environment variable at run time
by eyepopslikeamosquito (Archbishop) on Feb 03, 2005 at 08:20 UTC

      Phil? Phil Connors?

Re: create environment variable at run time
by chanakya (Friar) on Feb 03, 2005 at 06:23 UTC
    I'm second to brain's comment "You can't set this value from one process and see it in some other ... ".

    You may take the following approaches for solving your problem

    1. If you have various scripts then, its better to export the variables on the command line and then execute your script(s). In this way any of your perl scripts can access the exported variables.

    OR

    2) Have a conf file within your application. When your application loads, read the conf file and export your variables to the ENV{}. In this way I suppose your other scripts can also access the variables.

    Good Luck !!
Re: create environment variable at run time
by grinder (Bishop) on Feb 03, 2005 at 10:10 UTC
    this variable should be available to other script

    If you are brave, and insist on this approach, you can do this with an ugly dot-and-carry method. Assume that your perl script is embedded a wrapper shell (bash-like assumed) script like so:

    cd /path/to/script ./myperlscript foo bar rat quux . ./newvar.sh ./myotherscript

    Where your perl script 'myperlscript' contains (amongst whatever else it needs to do) something like the following:

    #! /usr/local/bin/perl my %new_env = @ARGV; open OUT, '> newvar.sh' or die "cannot open newvar.sh for output: $!\n +"; print OUT "#! /bin/sh\n"; print OUT "export $_=$new_env{$_}\n" for keys %new_env; close OUT;

    That is, the perl script writes the shell script that is the next command to be executed in the outer shell script. By the time ./myotherscript runs, the environment will have been sourced by the script that the perl script wrote beforehand.

    But keep in mind that this is a maintenance nightmare, and those who come after you will not venerate your name.

    - another intruder with the mooring in the heart of the Perl

Re: create environment variable at run time
by blazar (Canon) on Feb 03, 2005 at 10:23 UTC
    Hint: perldoc -q environment
Re: create environment variable at run time
by deibyz (Hermit) on Feb 03, 2005 at 11:30 UTC
    You can set $ENV{var} in your process and then call the rest of the scripts with fork+exec.
    For example:

    Parent (your script):

    #!/usr/local/bin/perl use strict; use warnings; $ENV{foo}="bar"; if (fork == 0){ #child exec("./foo.pl"); } else{ print "I'm the parent $ENV{foo}\n"; }

    Child (other scripts):

    #!/usr/local/bin/perl print "I'm the child $ENV{foo}\n";

    This works because environment is propagated to childs (and filehandles and...).

    Search for fork+exec for more information.

Re: create environment variable at run time
by aquarium (Curate) on Feb 03, 2005 at 14:45 UTC
    this is actually possible, or at least used to be possible in older windows, even though "normally" the same environment forking model applied. it involved a routine in assembly language. i have actually compiled and ran it myself, and it worked. the routine accessed the system environment variable and changed it. this is not something perl can do. probably can't be done that easy now, now that operating systems have become so much tighter in controlling processes. it is possible though, as that's exactly what gets done when you adjust the enironment variables in the tab for these settings (right click "my computer", select "advanced" tab, and click "environment variables".
    years ago you could also run a compiled program and just poke around in memory into other programs etc. no can do now. now we have a perfectly stable OS thanks to Billy, for five minutes at least ;)
    the hardest line to type correctly is: stty erase ^H