Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

202
Visualizações
Using given/when with IO::Select can_read

I am concocting a DIY sleep study filter that will log events from input from an audio stream (2K bytes/sec), an accelerometer (10 readings/sec) and console terminal keystrokes.

I have put together this chunk of code:

$SELECT=IO::Select->new or die "muffed create SELECT: $!\n" ;

my $i ; foreach ($AREC,$IMU,$STDIN) {
        $i++ ;
        $SELECT->add($_) or die "muffed SELECT $i: $!\n" ;
}

while (TRUE) {
    foreach ($SELECT->can_read ) {
        given ($_) {
            when ($AREC)  {}# handle audio from pipe
            when ($IMU)   {}# handle acceleromter from UDP socket}
            when ($STDIN) {}# handle keystrokes }
        }
    }           
}

  1. All of the given/when examples use string values or regular expressions. Will this work with filehandles?
  2. I recall that slower devices should have greater priority to avoid getting drowned in the flood of data input from faster devices. How should this be handled using IO::Select add and can_read?

I am using perl 5.30.0 on an Ubuntu 20.04 desktop. Appreciate any advice.

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

Smart matching is experimental and considered a failure. It should be avoided!

while (1) {
   for ($SELECT->can_read ) {
      if    ( $_ == $AREC  ) { }
      elsif ( $_ == $IMU   ) { }
      elsif ( $_ == $STDIN ) { }
   }
}
over 4 years ago · Santiago Trujillo Relatório

0

I like dispatch tables for these sorts of things, using the expected values as hash keys and the subroutine to run as the hash values. Some Perl pseudocode:

my $subs = (
    SOME_AREC_VALUE  => sub { ... },
    SOME_IMU_VALUE   => sub { ... },
    SOME_STDIN_VALUE => sub { ... },
    );

$subs->{$_}->();

I write quite a bit about replacing syntax structure with data structure in Mastering Perl and Effective Perl Programming.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda