Steven Lu
Steven Lu

Reputation: 43467

perl -- check if file handle is stdin

I am using select with a TCP server. I want to add STDIN to the select filehandle set.

#!/usr/bin/perl

use IO::Select;
use IO::Socket::INET;
my $sock = IO::Socket::INET->new(LocalPort => $serv_listen_port, Proto => 'tcp', List    en=> 1);

my $s = IO::Select->new();
$s->add(\*STDIN); #want to be responsive to user input (allow me to type commands for example)
$s->add($sock);

@readytoread=$s->can_read(1); #timeout = 1sec
foreach $readable (@readytoread) {
  if ($readable==$sock) {
    #This was a listen request, I accept and add new client here
  }
  if ($readable == STDIN){ #what to do on this line?
    #This is user typing input into server on terminal
  }
}

Need help with 4th to last line in the code here.

Upvotes: 3

Views: 1454

Answers (2)

ikegami
ikegami

Reputation: 386396

can_read returns the exact value passed to add, so you can simply use

$readable == \*STDIN

Upvotes: 2

JB.
JB.

Reputation: 42134

$readable->fileno == fileno STDIN

Or, if you're comfortable with that, fileno STDIN is zero, which you can check directly.

Upvotes: 5

Related Questions