Perl - check if file descriptor is stdin

I am using select with a TCP server. I want to add STDIN to the descriptor set of the selected file.

#!/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 from 4th to last line in code here.

+3
source share
2 answers
$readable->fileno == fileno STDIN

Or, if you like it, it fileno STDINis zero, which you can check directly.

+5
source

can_readreturns the exact value passed in add, so you can just use

$readable == \*STDIN
+2
source

Source: https://habr.com/ru/post/1796016/


All Articles