English 中文(简体)
PerlTerm:Read 等待新路线
原标题:Perl Term::ReadKey don t wait for newline
  • 时间:2010-06-16 15:30:16
  •  标签:
  • perl

我只字不提,试图接受投入,不阻挡,不重复所接触的特性(文字是产出,我想有骑手改变行为)。

我远未使用。

use Term::ReadKey;
ReadMode( "cbreak", STDIN );
if($input = ReadKey($pause_time, STDIN)){
    #process input
}

但是,一旦用户类型发生变化,文字就停止使用,直到新线进入。 我喜欢将处理的意见,而不等待新路线。

问题回答

这是一项小型方案,我认为你希望:

#!/usr/bin/perl

use strict;
use warnings;

use Term::ReadKey;

ReadMode 4;
END { ReadMode 0 }

print <<EOS;
q to quit
b to print in binary
o to print in octal
d to print in decimal
x to print in hexadecimal
EOS

my $control = "d";
my $i       = 0;
while (1) {
    #use "if" if you want to have a buffer of commands
    #that will be processed one per second  
    while (defined (my $key = ReadKey(-1))) {
        exit 0          if $key eq  q ;
        $control = $key if $key =~ /^[bodx]$/;
    }
    printf "%$control
", $i++;
    sleep 1;
}

我将把这一意见留给你自己回答,但决定我需要更多的余地。

破碎相当于原产物,但这种破碎的异构体的阻截控制序列,如 c、 c等。 两者同时收集特性。 这两种方式之间的行为差别,是你问题的根源。 如果Chas的解决方案与你打算做些什么一样,那么问题就更可能与你在<代码>上重复做的任何事情有关。 我已经评论过,如果我以某种精干的udi来填补,那么你原来的文字就会被罚款,这样我就可以看到它的工作。 例如,小范围插手:

use strict;
use warnings;
use Term::ReadKey;

my ($char, $input, $pause_time);
ReadMode("cbreak");

# Collect all characters typed into $input
# and quit when  #  is typed.

$input =   ;
while ($char = ReadKey($pause_time)) {
    last if $char eq  # ;
    $input .= $char;
}

print "$input
";

我无需打到底部,这样做是徒劳无益的(除了将运输回落到<条码>(投放到<条码>>/条码>之外,还绕过了航架)。





相关问题
Why does my chdir to a filehandle not work in Perl?

When I try a "chdir" with a filehandle as argument, "chdir" returns 0 and a pwd returns still the same directory. Should that be so? I tried this, because in the documentation to chdir I found: "...

How do I use GetOptions to get the default argument?

I ve read the doc for GetOptions but I can t seem to find what I need... (maybe I am blind) What I want to do is to parse command line like this myperlscript.pl -mode [sth] [inputfile] I can use ...

Object-Oriented Perl constructor syntax and named parameters

I m a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I ...

Where can I find object-oriented Perl tutorials? [closed]

A Google search yields a number of results - but which ones are the best? The Perl site appears to contain two - perlboot and perltoot. I m reading these now, but what else is out there? Note: I ve ...

热门标签