Your answer is not only at least a little bit right, it's exactly on target!  Excellent analysis! ++

I read your response yesterday, and was fairly sure that you had hit the nail on the head.  But I wanted to do it justice by testing further, and as I expected, the program succeeds when another reference is created to the filehandle.

First, I made sure $b_use_global_fh was zero, and changed system_command() to return both $psyscmd and the filehandle $fh, so I could create a reference to $fh to keep it open:

my ($psyscmd, $fh) = system_command("$bld_cmd 2>&1"); $global_fh = $fh;
That worked exactly as you predicted.  It also worked when I simply had the parent process return the filehandle:
if ($pid) { # Parent print STDERR "\e[101m(1a) Parent about to return\e[m\n"; return $fh; }
but only, of course, when the reference was preserved after the return from monitor_build:
my $unused_fh = monitor_build("./fakebuild");

So I thank you for a very enlightening solution.  It taught me something important about closures; namely, that they close their open filehandles which go out of scope, just the way a program does when it exits.  Nicely written!

Update:  I just realized (and just tested as correct) that another way to keep the filehandle from attempting to close is for the parent to return the reference to the closure:

my $psyscmd = system_command("$bld_cmd 2>&1"); # ... if ($pid) { # Parent print STDERR "\e[101m(1a) Parent about to return\e[m\n"; return $psyscmd; }
Then, as long as it is saved in the main program, this technique works as well.  It also avoids having to fuss with the filehandle directly.

@ARGV=split//,"/:L"; map{print substr crypt($_,ord pop),2,3}qw"PerlyouC READPIPE provides"

In reply to Re^2: Why is a deeply-bound lexical in a forked closure blocking the parent process? by liverpole
in thread Why is a deeply-bound lexical in a forked closure blocking the parent process? by liverpole

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.