perl6 How to re-open $*IN for input?

65 views Asked by At

I have been trying to read characters from $*IN, including control characters. However, after Control-D has been entered, it seems that $*IN becomes non-functional. How can I re-open $*IN for reading?

To exit type 'exit' or '^D'
> my $x=$*IN.getc             # entered "e"
e
e
> say ($x=$*IN.getc).ord      # entered Ctrl-D
10
> $x=$*IN.getc                # nothing can be entered now
(Any)
> my $y=$*IN.getc
(Any)
> 

Thanks for any help.

1

There are 1 answers

0
smls On BEST ANSWER

Automatically closing the stdin stream when the user presses Ctrl-D, is not something that Perl 6 does on the receiving end of the stream – it's something the Unix terminal driver does on its end of the stream. You'll thus have the same issue in every other programming language.

I don't think you can ask the terminal to re-open the stdin stream once it has closed it.

However, you can instruct the terminal to temporarily disable its default behavior of automatically handling control characters like Ctrl+D (so-called "Cooked mode"), and instead use "Raw mode" in which the EOT character will be passed on to your program as-is, among other things. Your program can then decide how to interpret that character.

One way to instruct the terminal to enter raw mode, is the termios API, the Perl 6 bindings for which are provided by the Term::termios module. (Using the .makeraw method demonstrated in the module's README.)