Raku:如何读取 STDIN 原始数据?
Raku: How to make read STDIN raw data?
如何在 Raku 中编写此 Perl5 代码?
my $return = binmode STDIN, ':raw';
if ( $return ) {
print "\e[?1003h";
}
评论cuonglm的回答。
我已经在使用 read
:
my $termios := Term::termios.new(fd => 1).getattr;
$termios.makeraw;
$termios.setattr(:DRAIN);
sub ReadKey {
return $*IN.read( 1 ).decode();
}
sub mouse_action {
my $c1 = ReadKey();
return if ! $c1.defined;
if $c1 eq "\e" {
my $c2 = ReadKey();
return if ! $c2.defined;
if $c2 eq '[' {
my $c3 = ReadKey();
if $c3 eq 'M' {
my $event_type = ReadKey().ord - 32;
my $x = ReadKey().ord - 32;
my $y = ReadKey().ord - 32;
return [ $event_type, $x, $y ];
}
}
}
}
但是当 STDIN 设置为 UTF-8 时,我得到 $x
或 $y
大于 127 - 32 的错误:
Malformed UTF-8 at ...
可以使用method read() from class IO::Handle进行二进制读取:
#!/usr/local/bin/perl6
use v6;
my $result = $*IN.read(512);
$*OUT.write($result);
然后:
$ printf '1[=11=]a\n' | perl6 test.p6 | od -t x1
0000000 31 00 61 0a
0000004
在 Perl 6 中您不需要 binmode,因为您何时可以决定以二进制或文本形式读取数据,这取决于您使用的方法。
如何在 Raku 中编写此 Perl5 代码?
my $return = binmode STDIN, ':raw';
if ( $return ) {
print "\e[?1003h";
}
评论cuonglm的回答。
我已经在使用 read
:
my $termios := Term::termios.new(fd => 1).getattr;
$termios.makeraw;
$termios.setattr(:DRAIN);
sub ReadKey {
return $*IN.read( 1 ).decode();
}
sub mouse_action {
my $c1 = ReadKey();
return if ! $c1.defined;
if $c1 eq "\e" {
my $c2 = ReadKey();
return if ! $c2.defined;
if $c2 eq '[' {
my $c3 = ReadKey();
if $c3 eq 'M' {
my $event_type = ReadKey().ord - 32;
my $x = ReadKey().ord - 32;
my $y = ReadKey().ord - 32;
return [ $event_type, $x, $y ];
}
}
}
}
但是当 STDIN 设置为 UTF-8 时,我得到 $x
或 $y
大于 127 - 32 的错误:
Malformed UTF-8 at ...
可以使用method read() from class IO::Handle进行二进制读取:
#!/usr/local/bin/perl6
use v6;
my $result = $*IN.read(512);
$*OUT.write($result);
然后:
$ printf '1[=11=]a\n' | perl6 test.p6 | od -t x1
0000000 31 00 61 0a
0000004
在 Perl 6 中您不需要 binmode,因为您何时可以决定以二进制或文本形式读取数据,这取决于您使用的方法。