sid_com
sid_com

Reputation: 25117

Raku: How to make read STDIN raw data?

How could I write this Perl5 code in Raku?

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

Comment to cuonglm's 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 ...

Upvotes: 8

Views: 569

Answers (1)

cuonglm
cuonglm

Reputation: 2816

You can use method read() from class IO::Handle to perform 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 don't need binmode in Perl 6, since when you can decide to read data as binary or as text, depend on what methods you use.

Upvotes: 5

Related Questions