Advertisement
HwapX

ReadLine Async

Dec 13th, 2012
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 0.57 KB | None | 0 0
  1. #!usr/bin/perl -w
  2.  
  3. use warnings;
  4. use strict;
  5. use feature qw(state);
  6. use Term::ReadKey qw(ReadKey);
  7.  
  8. sub readLine {
  9.     my $c;
  10.     state $buffer = "";
  11.     while($c = ReadKey(-1)) {
  12.         print $c;
  13.         if($c eq chr(8) && length($buffer)) {
  14.             $buffer = substr($buffer, 0, length($buffer) -1);
  15.             next;
  16.         }
  17.         $buffer .= $c;
  18.         if($c eq "\r") {
  19.             my $b = $buffer;
  20.             $buffer = "";
  21.             return $b;
  22.         }
  23.     }
  24.     return undef;
  25. }
  26.  
  27. while(1) {
  28.     my $i = readLine();
  29.     print $i if($i);
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement