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:

... 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"; ...
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.

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?
-- 
Ronald Fischer <ynnor@mm.st>

In reply to Win32::Job::watch vs. system(1,...) by rovf

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.