In a recent thread about starting detached processes on Windows using the special form system(1,...), I got a recommendation to use Win32::Job instead of system(1,...), as this would be safer to use and easy too. So I started to play around with Win32::Job, basically trying this:
The purpose of watch() is to abort the spawned program and all child processes it might have been created, prematurely if the file killfile.txt gets created.... my $j=Win32::Job->new; $j->spawn('perl.exe', "perl.exe myprog.pl", { new_console=>0, new_group=>1, stderr=> $out, stdout=> $out }) unlink 'killfile.txt'; $j->watch(sub { -e 'killfile.txt' ? 1 : 0 }, 3); print "go on\n"; ...
This works indeed, but there is one big difference compared to when I implement the same using system(1,...):
With system(1,...), I get a separate process, i.e. my Perl program continues to run. With Win32::Job, the watch call blocks, i.e. "go on" is printed only after "perl.exe myprog.pl" has (forcefully or gracefully) finished; so Win32::Job doesn't seem to be an alternative for system(1,...), but simply something different.
Of course I could combine the two approaches: Write a helper script 'spawn_and_watch.pl', which I would start with system(1,...), and which does the spawn and watch calls; but I have somehow the feeling that there should be an easier solution. Did I miss something here?In reply to Win32::Job::watch vs. system(1,...) by rovf
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |