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

I need to know if there is a solution to exec() acting differently on windows vs unix. Best way to see the difference is using 3 little perl scripts (I'm displaying them from unix with the "more" command, but they will run as-is on windows also)
-> more testexec* :::::::::::::: testexec.pl :::::::::::::: #!/usr/bin/perl system("testexec2.pl"); print "Done $0\n"; :::::::::::::: testexec2.pl :::::::::::::: #!/usr/bin/perl exec($^X,"testexec3.pl") or print "exec failed - $!\n"; print "Done $0\n"; :::::::::::::: testexec3.pl :::::::::::::: #!/usr/bin/perl `sleep 3`; print "Done $0\n";
On unix, things act as expected: testexec.pl waits for testexec2.pl to finish (with testexec2.pl being replaced with testexec3.pl). But on windows, testexec.pl does NOT wait for testexec2.pl/testexec3.pl to finish. Is there a way to get Windows to act the same as unix? The windows behaviour does not seem correct...

FYI: I'm trying to avoid using system() instead of exec() because then I open a can of worms regarding the quoting of arguements to the programs being exec()'d (the arguements themselves may contain quotes and/or spaces, and going through the shell on the two different OS's is a whole lot of extra code I can avoid if exec() would just act right :-)

Aaron

Replies are listed 'Best First'.
Re: windows vs unix: different behaviour with exec()
by almut (Canon) on Nov 19, 2008 at 20:12 UTC
    the arguments themselves may contain quotes and/or spaces

    You could use the list variant of system, in which case the arguments won't undergo shell interpolation.

    system("perl", "testexec.pl", "arg 1", '"arg2"');
Re: windows vs unix: different behaviour with exec()
by zentara (Cardinal) on Nov 19, 2008 at 20:10 UTC
      you may need the weird windows "system(1,prog.pl)"

      Is this an undocumented feature of Perl? Because system doesn't mention it, although it works (as I found by trying it out).

      -- 
      Ronald Fischer <ynnor@mm.st>
        It's just one of those tips that floats around Perlmonks.... I couldn't even find it on google.

        I'm not really a human, but I play one on earth Remember How Lucky You Are
Re: windows vs unix: different behaviour with exec()
by wol (Hermit) on Nov 20, 2008 at 15:03 UTC
    IIRC, the problem would be the way that exec() is emulated on Windows - it spawns a new process to run the command you specify and exits the current one (it does this because there's no way to re-use the existing process to run the new command).

    Most of the time, this implementation is indistinguishable from the UNIX-like exec(), but as you show, not always.

    As noted by almut, I think your best bet may be to use system() with a list.

    --
    .sig : File not found.