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

Hi,

I'd like to work with creating an environment variable in perl and wait.

Then I would then allow an external process to change that environment variable.

The perl code will basically loop and wait until environment variable is set at say 0 then continue.

The following code does not work. changing the environment variable externally does not propogate to perl's environment variable setting even if I use setx or set. Looks like perl only sees the environment variables of it's own session.

Help.

`setx nas_scratch 0 -m`; (@queueitems)=split(/\s+/,$lineitem); foreach (@queueitems) { my $tapeid = $_; $ENV{NAS_SCRATCH}=$tapeid; ## shell out to Junction command using oprcmd? while ($ENV{NAS_SCRATCH} != 0) {## wait until external command finishes sleep(5); ## hold 5 secs and check again }

Replies are listed 'Best First'.
Re: working with enivornment variables in win32
by duff (Parson) on May 11, 2004 at 15:41 UTC
    It sounds like you really want a semaphore rather than an environment variable. The behavior you see has nothing to do with perl and everything to do with the operating system. Either write to a file (rather than an environment variable) or use Win32::Semaphore or something
Re: working with enivornment variables in win32
by itub (Priest) on May 11, 2004 at 15:41 UTC
    The very nature of environment variables is that they "belong" to a process. If you change them in one process, they won't change in the others. You should consider other methods of interprocess comunication. Perhaps a temporary file could be enough for your needs?
      yea I thought I'd try with the environment variables since I didn't really want the overhead of opening and closing the file every 5 secs. I thought about forking and using semaphores but didn't want to make the code too complex either.
Re: working with enivornment variables in win32
by bengmau (Beadle) on May 11, 2004 at 15:39 UTC

    woops didn't login

    Hi, I'd like to work with creating an environment variable in perl and wait. Then I would then allow an external process to change that environment variable. The perl code will basically loop and wait until environment variable is set at say 0 then continue. The following code does not work. changing the environment variable externally does not propogate to perl's environment variable setting even if I use setx or set. Looks like perl only sees the environment variables of it's own session. Help.

    `setx nas_scratch 0 -m`; (@queueitems)=split(/\s+/,$lineitem); foreach (@queueitems) { my $tapeid = $_; $ENV{NAS_SCRATCH}=$tapeid; ## shell out to Junction command using oprcmd? while ($ENV{NAS_SCRATCH} != 0) {## wait until external command finishes sleep(5); ## hold 5 secs and check again }
Re: working with enivornment variables in win32
by hawtin (Prior) on May 12, 2004 at 09:23 UTC

    Another simple way to do what you want is to have a "flag file". That is a file in a well known directory that is created when the external process is complete. Then your control process just has to check for the existence of the file rather than opening it.

    Once the controller has noticed the file exists it can just delete it, the file doesn't need to actually contain anything (unless you want it to).