Perl6: How to read STDIN source data?

How can I write this Perl5 code in Perl6?

my $return = binmode STDIN, ':raw'; if ( $return ) { print "\e[?1003h"; } 

Comment on cuonglm answer.

I am already using 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 ]; } } } } 

But with STDIN set to UTF-8, I get errors with $x or $y greater than 127 - 32:

 Malformed UTF-8 at ... 
+5
source share
1 answer

You can use the read () method from the IO :: Handle class to do binary reading:

 #!/usr/local/bin/perl6 use v6; my $result = $*IN.read(512); $*OUT.write($result); 

Then:

 $ printf '1\0a\n' | perl6 test.p6 | od -t x1 0000000 31 00 61 0a 0000004 

You do not need binmode in Perl 6, because when you can decide to read data as binary or text, it depends on what methods you use.

+5
source

Source: https://habr.com/ru/post/1240989/


All Articles