#!/usr/bin/env perl
use warnings;
use strict;
use Fcntl qw(F_GETFD F_SETFD FD_CLOEXEC);
open my $fh,">&2";
unless (@ARGV && $ARGV[0] eq 'noset') {
fcntl($fh, F_SETFD, ~FD_CLOEXEC) or die "Can't set flags: $!\n";
print "Set no close on exec\n";
}
$ENV{myfd}=fileno($fh);
system 'print_to_3.pl';
print $fh "This is to fd $ENV{myfd} from the parent\n";
####
#!/usr/bin/env perl
use warnings;
use strict;
open OUT,">&=$ENV{myfd}" or die "cannot dup $ENV{myfd}: $!\n";
print "Child opened FD $ENV{myfd}\n";
my $p=print OUT "***\nHere is data going to fd $ENV{myfd}\n***\n";
warn "return value of print=$p ($!)\n";
##
##
#!perl -w
use strict;
use Fcntl qw(F_GETFD F_SETFD FD_CLOEXEC O_TEXT);
use Win32API::File qw(:Func :HANDLE_FLAG_);
use Win32::Process;
open my $fh,">&2";
$^F = 3;
my $os_handle=GetOsFHandle($fh) or die "Can't GetOSFHandle: $^E,\nstopped";
#$ENV{myfd}=my $ivFD = OsFHandleOpenFd($os_handle,O_TEXT);
GetHandleInformation($os_handle, my $flags);
print "HANDLE_FLAG_INHERIT=",HANDLE_FLAG_INHERIT,"\n";
print "HANDLE_FLAG_PROTECT_FROM_CLOSE=",HANDLE_FLAG_PROTECT_FROM_CLOSE,"\n";
print "flags was=$flags\n";
$flags |= HANDLE_FLAG_INHERIT|HANDLE_FLAG_PROTECT_FROM_CLOSE;
print "Trying for flags=$flags\n";
SetHandleInformation($os_handle, 0x2, 0x2) or die "Can't SetHandleInformation: $^E,\nstopped";
GetHandleInformation($os_handle, my $newflags);
print "flags now=$newflags\n";
$ENV{myfd}=fileno($fh);
my $proc;
#Win32::Process::Create($proc, $ENV{COMSPEC},"/c print_to_3.pl",1,0, "." )
# or die "Can't run: $^E\n";
system 'print_to_3.pl';
print $fh "This is to fd $ENV{myfd} from the parent\n";
##
##
open OUT,">&=$ENV{myfd}" or die "cannot dup $ENV{myfd}: $!\n";
print "Opened FD $ENV{myfd}\n";
$x=print OUT "***\nHere is data to FD $ENV{myfd}\n***\n";
warn "print=$x ($!)\n";