Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

comment on

( [id://3333] : superdoc . print w/replies, xml ) Need Help??

If I add logic to the main program to exit the while loop before the subprogram has finished, the subprogram does not get terminated early.

use strict; use warnings; #1176332 my $cmd_1 = "perl"; # (renamed from OP's open (CMD, "$cmd_1 2>&1|") or warn ("!!! Can't run program: $!\n"); my $start = time(); printf "Started at %s\n",$start; while (my $line = <CMD>) { print "->" . $line; if (time() > $start + 2) { printf "Stopping at %d!\n",time(); last; } if ($line =~ m/^xyz/) { print "\n Pretending to do some string manipulation\n"; } else { print "(from $0): Nothing yet | "; } } close CMD; printf "Actually stopped at %d\n", time();
H:\perl>perl Started at 1479848254 ->Hello World from 1176332 PART 2 (from Nothing yet | ->Sleeping 10 seconds (from Nothing yet | ->. (from Nothing yet | ->. (from Nothing yet | ->. Stopping at 1479848257! Actually stopped at 1479848264

The main program does not return control to the command line until 7 seconds later, not until the subprogram has terminated on its own.

But God demonstrates His own love toward us, in that while we were yet sinners, Christ died for us. Romans 5:8 (NASB)

In reply to Re^2: Run and kill external programm after x seconds by GotToBTru
in thread Run and kill external programm after x seconds by demichi

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

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.