This is an advance chapter of Learning Perl 6 by Randal L. Schwartz and brian d foy. It is incomplete and may contain merely notes or an outline for future work. It may also contain sections of their writing from other sources, although these are noted where possible.
This work is copyrighted under a contract between O'Reilly Media and the authors, and you cannot repost it or distribute it without permission.
We've already showed you a some input and output so you could see what's going on with your programs, and now we're going to cover what you'll need for most of your small programs. This chapter covers taking input from the keyboard or files, and sending output to the screen or files.
For now, just think of standard input as that which a person types during a run of your program. It can come from other
sources, and we'll show those in a minute. So far, you've read a line of input from the user using the standard input, $IN. The = in front of the variable tells [% P6 %] to read a line from
$IN. It automatically strips the newline for you.
[% INCLUDE_CODE nice_to_meet_you.1.p6 %]
If the user simply hits return without typing anything else, nothing ends up in $name. That
nothing is really just the empty string, which is still a defined value. In this case, I can use the // operator to give $name it's default value.
[% INCLUDE_CODE nice_to_meet_you.default.p6 %]
This also works for the case where the user ends input (perhaps by using Control-D on Unix or
Control-Z on Windows) without hitting return. In that case, =$IN
returns undef, and $name still gets the default value.
There's a bit of a problem though. What if the user's name just happens to be 0? We can't
imagine that anyone would be that cruel to their child, but what do we know? If the user enters that name, our program still uses
the default.
% perl6 ...
What's your name? 0
Hello Perl Programmer! Nice to meet you!
# reading in a loop
So far we've read a single line and done something with it. We can wrap that in a while loop to
keep reading lines. We have to check the value for defined-ness so we don't accidently end on a line that has a false value such as
0, which might be perfectly good input:
while( defined( my $line = =$*IN ) ) {
say "Read the line: $line";
}
That's a bit much to type every time we want to get some lines of input though. Instead of using a while loop, we can use a for loop to do it for us. The read line goes
into $_ by default and Perl automatically checks it for definedness:
for =$*IN {
say "Read the line: $_";
}
We can easily read input from files we specify on the command line. Those arguments show up in the special array @*ARGS.
XXX
Since @*ARGS is just an array, we can populate it ourselves before we use $*ARGS.
XXX
We've already used say, which sends it's string to standard output and adds a newline to it.
The print method does the same thing except that it doesn't add a newline.
# interpolating arrays
# replacement for cat
* @ARGV is now @ARGS
* auto-chomp
* no more while( <> )
* names
* open returns a filehandle
* Perl 5 print with no args in now Perl 6 .print
* can't printf @items;
See Section A.[% this_chapter %] for answers to these exercises: