Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clarification regarding IO::Select

Tags:

perl

I am using IO::Select.

I didn't expect this code to terminate only when I manually kill the forked process. Is this correct 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";
like image 728
mpapec Avatar asked Mar 12 '23 14:03

mpapec


1 Answers

From open,

Closing any piped filehandle causes the parent process to wait for the child to finish, then returns the status value in $? and ${^CHILD_ERROR_NATIVE}.

So closing the file handle in $ph (which is done when $ph goes out of scope) waits for the child to complete.

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

like image 129
ikegami Avatar answered Mar 16 '23 00:03

ikegami