Encoding Raku:如何使读取的标准数据成为原始数据?
我怎样才能用Raku编写这个Perl5代码Encoding Raku:如何使读取的标准数据成为原始数据?,encoding,stdin,raku,Encoding,Stdin,Raku,我怎样才能用Raku编写这个Perl5代码 my $return = binmode STDIN, ':raw'; if ( $return ) { print "\e[?1003h"; } 对库恩格姆的回答发表评论 我已经在使用阅读: my $termios := Term::termios.new(fd => 1).getattr; $termios.makeraw; $termios.setattr(:DRAIN); sub ReadKey {
my $return = binmode STDIN, ':raw';
if ( $return ) {
print "\e[?1003h";
}
对库恩格姆的回答发表评论 我已经在使用
阅读:
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时,我会得到大于127-32的$x
或$y
错误:
Malformed UTF-8 at ...
您可以使用从执行二进制读取:
#!/usr/local/bin/perl6
use v6;
my $result = $*IN.read(512);
$*OUT.write($result);
然后:
在Perl 6中不需要binmode,因为何时可以决定以二进制或文本形式读取数据取决于您使用的方法。utf-8是一种可变长度编码,它不是二进制安全的;根据您尝试执行的操作,可以使用.decode('latin1')
,或者只保留数值并将$c3 eq'M'
之类的内容替换为$c3=='M',您可以使用latin1来获取一个字符串,该字符串无论输入什么数据都不会失败。否则,我建议您只需在.read(1)[0]中返回$*IN,就可以只输出单字节值。我已经尝试了latin1
-解决方案。也许我会坚持下去。
$ printf '1\0a\n' | perl6 test.p6 | od -t x1
0000000 31 00 61 0a
0000004