Socket select in Perl

David Smith DavidSmith at byu.net
Thu Feb 17 17:15:20 MST 2005


<quote who="Hans Fugal">
> On Thu, 17 Feb 2005 at 16:58 -0700, David Smith wrote:
>> Perl's built-in select() function doesn't quite look like what I want. I
>> think IO::Select may be the trick, but it seems like overkill when I
>> just
>> have one socket (and not a set of handles to multiplex). Can anyone shed
>> light on this struggling Perlmonk wannabe?
>
> Isn't that the C way too though? Don't you have to set up the FDSETs and
> all that even if you're only selecting on one fd?

Okay, so I guess this would work. Can any Perl monks correct me here?

my $socket = ....
my $select =  = IO::Select->new();
$select->add( $socket );
if( $select->can_read( 0.5 ) ) {
    $response = <$socket>;
    print $response . "\n";
} else {
    print "No data on socket.\n";
}

Since can_read() returns an array, the if expression should return a count
of the array elements. If the count is > 0, the if expression is evaluated
as true, otherwise false. Is this right?

--Dave



More information about the PLUG mailing list