3

Here's my test program:

use Readline;

shell 'clear';
my $r = Readline.new;

loop {
  my $a = $r.readline("> ");
  {say ''; last} if not defined $a;
  $r.add-history( $a );
  say $a;
}

After I enter any string, it exits with the following message:

> abc
Internal error: unhandled encoding
  in method CALL-ME at /opt/rakudo-pkg/share/perl6/sources/24DD121B5B4774C04A7084827BFAD92199756E03 (NativeCall) line 587
  in method readline at /home/evb/.perl6/sources/D8BAC826F02BBAA2CCDEFC8B60D90C2AF8713C3F (Readline) line 1391
  in block <unit> at abc.p6 line 7

If I comment the line shell 'clear';, everything is OK.

Eugene Barsky
  • 5,780
  • 3
  • 17
  • 40

1 Answers1

5

This is a bit of a guess, but I think when you tell your shell to clear the screen, it's sending a control character or control sequence as input to the terminal emulator. Readline is reading from that same stream, and those characters end up at the beginning of your "line" when you try to read a line. Those characters aren't valid UTF-8 (the default encoding) and so can't be interpreted as a string. You'll know more if you open the text files in the stack trace and look at the relevant line numbers.

You can try calling reset-terminal or reset-line-state to see if you can get rid of that character. What I would do in a low level programming language is to do a nonblocking read of the input (without converting it into a string), but I can't find the API for that in the Perl 6 library.

piojo
  • 6,351
  • 1
  • 26
  • 36
  • 2
    Thanks! I can't find `reset-terminal` or `reset-line-state`, but simply adding `shell 'reset'` solves the problem. – Eugene Barsky Jan 03 '18 at 10:45
  • There was no such problem with Linenoise, but Readline is so much better... – Eugene Barsky Jan 03 '18 at 11:29
  • 1
    @EugeneBarsky I'm glad that worked! I found those functions in the Readline.pm code, but haven't actually tried running it. https://github.com/drforr/perl6-readline/blob/master/lib/Readline.pm – piojo Jan 03 '18 at 11:50
  • And you are right, bash `shell 'printf "\033c"';` does the same job as `shell 'clear'` (and disables Readline as well). – Eugene Barsky Jan 03 '18 at 12:46