Clarification regarding IO :: Select

I use IO::Select.

I did not expect this code to end only when I manually kill the forked process. Is this the right behavior?

use strict;
use warnings;

STDOUT->autoflush();

use IO::Select;

open(my $ph, "-|", "perl -e 'sleep 1 while 1'");

my $sel = IO::Select->new();

$sel->add($ph);

$sel->can_read(2) or warn "timeout\n";

print "woohoo?\n";
+4
source share
1 answer

by open,

Closing any file descriptor with pipes causes the parent process to wait for the child process to complete, then returns the status value to $?and ${^CHILD_ERROR_NATIVE}.

Thus, closing the file descriptor in $ph(which is performed when it $phgoes out of scope) waits for the child to complete the work.

(This has nothing to do with IO :: Select or select.)

+7
source

All Articles