Skip to Content
Perl 6 Deep Dive
book

Perl 6 Deep Dive

by Andrew Shitov
September 2017
Beginner
402 pages
9h 52m
English
Packt Publishing
Content preview from Perl 6 Deep Dive

Reading characters

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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Perl Hacks

Perl Hacks

Chromatic, Damian Conway, Curtis Ovid Poe, Curtis (Ovid) Poe
Learning Perl 6

Learning Perl 6

brian d foy
Think Perl 6

Think Perl 6

Laurent Rosenfeld, Allen B. Downey
Pro Perl

Pro Perl

Peter Wainwright

Publisher Resources

ISBN: 9781787282049Supplemental Content