in reply to Re^14: Using STDIN after exec() with ActiveState Perl
in thread Using STDIN after exec() with ActiveState Perl

On Linux it works, but not on Windows (I re-checked right now)

I don't believe that :)

You must be loading the module before you modify %ENV

Don't do that, only load the module after you've set %ENV

  • Comment on Re^15: Using STDIN after exec() with ActiveState Perl

Replies are listed 'Best First'.
Re^16: Using STDIN after exec() with ActiveState Perl
by Yaribz (Beadle) on Jun 21, 2015 at 22:04 UTC
    Here is the code for test.pl:
    use warnings; use strict; use Module::Load; if(! $ENV{SPRING_WRITEDIR}) { $ENV{SPRING_WRITEDIR}='E:\\test'; exec {$^X} ($^X,$0); } $ENV{PATH}="E:\\springrts;$ENV{PATH}"; load("PerlUnitSync"); PerlUnitSync::Init(0,0); my $writeDir=PerlUnitSync::GetWritableDataDirectory(); print "writeDir=$writeDir\n";
    Here is the output:
    E:\test>perl test.pl E:\test>writeDir=E:\test\
    And now the output if I comment the exec line:
    E:\test>perl test.pl writeDir=D:\Documents\My Games\Spring\
    As you can see the SPRING_WRITEDIR environment variable is not taken into account if I don't use exec.

      Update: Ok, so can't use a BEGIN block...

      On Windows, exec is simulated in a strange way. Apparently, it's creating a new process and exiting the caller before the new process exits.

      (Update 2: On Linux, exec does not start a new process, it loads a new executable into the calling process. From the command shell's point of view, the process it launched is still running, so it's not trying to read STDIN.)

      Your problem with system is that you need to exit after system returns:

      use warnings; use strict; use Module::Load; if(! $ENV{SPRING_WRITEDIR}) { $ENV{SPRING_WRITEDIR}='E:\\test'; system {$^X} ($^X,$0); exit; }

      Yes, you will have 2 Perl processes where you had 1, but one of those will be idle while waiting on the other to exit.

      Please try:

      use warnings; use strict; BEGIN { $ENV{SPRING_WRITEDIR}='E:\\test'; $ENV{PATH}="E:\\springrts;$ENV{PATH}"; } use Module::Load; load("PerlUnitSync"); PerlUnitSync::Init(0,0); my $writeDir=PerlUnitSync::GetWritableDataDirectory(); print "writeDir=$writeDir\n";

      This will set the environment before Module::Load is loaded.

        Thanks for looking into this. The problem is that my script must be able to modify the environment variables as many times as needed during its execution, which would lead to a growing number of idle perl processes with this solution.