#!/usr/bin/env perl use warnings; use strict; use IO::Select; use IO::Handle; use Inline 'C'; my $select = IO::Select->new( \*STDIN ); disable_canon_mode() if @ARGV; # Experiment by commenting out the above line and giving command-line # arguments. while ( 1 ) { $select->can_read; # block until new input warn "CAN_READ STDIN\n"; my $in; if ( @ARGV ) { sysread STDIN, $in, 1; print "Char read: $in\n"; } else { $in = ; print "Line read: $in\n"; } } __END__ __C__ #include #define STDIN 0 void disable_canon_mode () { struct termios tio; if ( isatty( STDIN ) == 0 ) { return; } /* Turn off canonical mode, meaning read one char at a time. Set timer to 0, to wait forever. Minimum chars is 1. */ tcgetattr( STDIN, &tio ); tio.c_lflag &= ~ICANON; tio.c_cc[ VTIME ] = 0; tio.c_cc[ VMIN ] = 1; tcsetattr( STDIN, TCSANOW, &tio ); }