Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!swrinde!cs.utexas.edu!uunet!convex!usenet From: tchrist@convex.COM (Tom Christiansen) Newsgroups: comp.lang.perl Subject: Re: grabchars in perl? Message-ID: <1991Jun05.103001.3365@convex.com> Date: 5 Jun 91 10:30:01 GMT References: <1991Jun5.052344.22684@newshost.anu.edu.au> Sender: usenet@convex.com (news access account) Reply-To: tchrist@convex.COM (Tom Christiansen) Organization: CONVEX Software Development, Richardson, TX Lines: 42 Nntp-Posting-Host: pixel.convex.com From the keyboard of rjm@vulcan.anu.edu.au (Robert J. McArthur): :Has anyone implemented the grabchars package in perl? : :For those who don't know, grabchars is a PD program from comp.sources.misc :that implements timed reads from ttys and non-CR-requiring input. eg. you :can just type one letter in answer to a y/n question without needing the :CR. Use "system stty" or ioctl to get yourself in the moral equivalent of cbreak mode (per FAQ #16). Use an ioctl if you're going to be doing a lot of these. Use system if you're only running it once. Then set a timer with alarm (or SYS_setitimer if you have it and want that granularity). If your reads don't continue after interruptions, then just return from your interrupt routine. If they do, you need a setjmp/longjmp, so do the getc in an eval, and have your interrupt routine die, effectively longjumping out of the eval. sub BANG { die "alarm went off"; } $SIG{'ALRM'} = 'BANG'; system "stty cbreak /dev/tty 2>&1"; print "go\n"; alarm 2; $key = eval "getc(STDIN)"; alarm 0; if (!defined($key)) { die $@ if $@ && $@ !~ /alarm/; # not my exception? print "timeout!\n"; } else { printf "\nkey was %s (0x%x)\n", $key, ord($key); } system "stty cooked /dev/tty 2>&1"; I believe that this code needs 4.003 to work correctly. Again, you poor folks (SysV, POSIX) with non-restarting reads don't need to worry about the eval. I wish perl gave me more control here, but I'm at the mercy of my C library, and getting at the SV_INTERRUPT bit is non-trivial. --tom -- Tom Christiansen tchrist@convex.com convex!tchrist "Perl is to sed as C is to assembly language." -me