September 2017
Beginner
402 pages
9h 52m
English
To get a single character, use the getc method:
my $ch = $*IN.getc; say $ch;
The getc method blocks the program execution until a character appears in the stream. If there are no characters left in the stream, an empty value of Any is returned. In a Boolean context, it is False, so it can be used in a condition of a loop. Let us create a program that reads its input character by character and prints them each on a separate line.
while my $ch = $*IN.getc {
say $ch;
}
The getc method is quite smart when it deals with the Unicode characters. To demonstrate this behavior, let us create a text file, text.txt, and put a single u character in it. Then, pass the file to the program and read the character:
$ perl6 getc.pl < text.txt ...