in reply to [SOLVED] File handle passing issue under *nix

Hi vsespb,

as I have to do penance for my sarcastic answer in another thread of today, here my points:

1) You subcommand2.pl test is IMHO a false positive. As soon as you read twice you also get this stuck behaviour. Try this:

open IN, "<&", $ARGV[0] or die $!; binmode IN; my $s = <IN>; print "line: $s"; print "Attempt to read twice:"; $s = <IN>; print "line: $s"; close IN;

So, it's the same as subcommand1.pl. You're just kidding yourself.

2) As soon as you modify your main.pl to the following, it works:

use strict; use warnings; use IO::Handle; $^F = 100; pipe READHANDLE,WRITEHANDLE; binmode READHANDLE; binmode WRITEHANDLE; WRITEHANDLE->autoflush; READHANDLE->autoflush; unless (fork()){ close WRITEHANDLE or die $!; exec $^X, $ARGV[0], fileno(READHANDLE); die; } close READHANDLE or die $!; print WRITEHANDLE "Test!\n"; close WRITEHANDLE; wait;

You see, that the unnecessary ends of the pipe are closed. See http://man7.org/linux/man-pages/man2/pipe.2.html for the description.

Best regards
McA

Replies are listed 'Best First'.
Re^2: File handle passing issue under *nix
by vsespb (Chaplain) on Sep 30, 2013 at 19:32 UTC
    1) You subcommand2.pl test is IMHO a false positive.
    Indeed, but my intention was to proof that it at least can read something and not hang (so, exec() call works like expected etc).
    2) As soon as you modify your main.pl to the following, it works
    Damn! You are right! I was trying version which closes READHANDLE, but forgot about WRITEHANDLE ! Thank you. I think it's solved now!

    p.s.
    I upvoted your sarcastic post, it looks acceptable to me.

      You're welcome. McA