我使用的是Perl 6模块Term::termios。
#!/usr/bin/env perl6
use v6;
use Term::termios;
my $saved_termios := Term::termios.new(fd => 1).getattr;
my $termios := Term::termios.new(fd => 1).getattr;
$termios.makeraw;
$termios.setattr(:DRAIN);
loop {
my $c = $*IN.getc;
print "got: " ~ $c.ord ~ "\r\n";
last if $c eq 'q';
}
$saved_termios.setattr(:DRAIN);
当我运行此脚本并按下向上箭头,向下箭头,向右箭头,左键arrow 和 q 这是输出:
#after arrow-up:
got: 27
got: 91
#after arrow-down:
got: 65
got: 27
got: 91
#after arrow-right:
got: 66
got: 27
got: 91
#after arrow-left:
got: 67
got: 27
got: 91
#after q:
got: 68
#after another q:
got: 113
但我原以为:
#after arrow-up:
got: 27
got: 91
got: 65
#after arrow-down:
got: 27
got: 91
got: 66
#after arrow-right:
got: 27
got: 91
got: 67
#after arrow-left:
got: 27
got: 91
got: 68
#after q:
got: 113
如何修改脚本以获得所需的输出?
答案 0 :(得分:2)
将var jsonBlob = []byte(`[
{"Name": "Platypus", "Purchase": "Monotremata"},
{"Name": "Quoll", "Purchase": "Dasyuromorphia"}
]`)
type Animal struct {
Name string
Order string `json:"Purchase"`
}
var animals []Animal
err := json.Unmarshal(jsonBlob, &animals)
if err != nil {
fmt.Println("error:", err)
}
fmt.Printf("%+v", animals)
替换为my $c = $*IN.getc;
并更改代码的其余部分以处理缓冲区而不是字符串。