Slash Boxes
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

JonathanWorthington (6049)

  (email not shown publicly)
Monday March 31, 2008
05:03 PM

Rakudo gets some IO

[ #36013 ]

Reading through the transcript of the Perl 6 call last week, I noticed that I/O was mentioned as something that Rakudo was lacking and that people were really missing. I flew to the UK on Friday, was at a wedding on the Saturday weekend and meeting up with a friend in London on the Sunday and flew back today. However, I managed to grab a few spare hours on the train on the way there, and on the Sunday evening while Normal People slept and the morning before my flight on the Monday to get some Rakudo hacking done. As a result, we now have the beginings of IO in Rakudo.

Here's a quickly hacked up random number game.

my $answer = int(rand(100)) + 1;
my $guesses = 0;

say "Guess the number (between 1 and 100)";

for =$*IN -> $guess {
    if $guess == $answer {
        say "You got it right in $guesses guesses!";
    elsif $guess < $answer {
        say "Too low";
    else {
        say "Too high";

Here's an example run of the game.

C:\Hacking\parrot\languages\perl6>..\..\parrot perl6.pbc guess.p6
Guess the number (between 1 and 100)
Too low
Too low
Too low
Too high
Too low
Too high
You got it right in 7 guesses!

You'll note that here I'm using $*IN. This is an instance of the IO class, so you can do:

say $*IN.WHAT;

And it will report "IO". Writing =$*IN means "get me the iterator to read from $*IN", which reads a line at a time. $*OUT and $*ERR are also available, so we can output to STDERR now by doing:


So we have those three file handles, but what about getting one to a file itself? That's what open is for. Here's a program that takes a file name and then prints each line in that file with the line number at the start of it.

my $fh = open(@*ARGS[0], :r); # :r = read mode
my $line = 1;
for =$fh {
    say $line++ ~ " $_";

I ran it on the readme; I'll just quote the top bit of the output.

C:\Hacking\parrot\languages\perl6>..\..\parrot perl6.pbc ln.p6 README
1 ## $Id: README 25155 2008-01-22 19:25:25Z pmichaud $
3 =head1 Rakudo Perl 6
5 This is the Perl 6 compiler for Parrot, called "Rakudo Perl 6,"

You'll notice the use of the new pair syntax for specifying the mode to open the file in, which I talked about in my last post. So already we're building stuff on top of that syntax. :-)

I'm not sure how busy the coming week will be, but I do know that next weekend and Monday are set aside for doing Perl 6 related things: I will be joining the Oslo QA Hackathon. So I look forward to seeing any of you that will be attending there, and hopefully bringing us another few steps closer to Perl 6 on Parrot.

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
More | Login | Reply
Loading... please wait.