Wednesday, January 07, 2009

My very first perl6 program

I know, I know, I promised to do some perl6 hacking over the holidays, but as it turned out, I didn't have the time to do it. Having 2 kids in the family and tons of activities lined up over the holidays didn't really gave me much room to doing other things like hacking with my computer. Anyways, another thing I did over the holidays was... tada I got a G1. And I really, really love it! Absolutely amazing piece of work. The downside is, it's going to hurt on our monthly utility bills. So my wife and I were thinking cutting down our cost. We are thinking of giving up our landline, since we both have wireless anyway. But of course wireless minutes aren't cheap either, so we gotta figure out who are actually calling us and who do we call from our landline.

Well, that can be done very easily. Since we are subscribed to comcast, incoming and outgoing phone calls are easily downloadable from their website. And so, this is a good opportunity for me to use the perfect tool for the job. Perl. But this time, I want to use Perl6. And here's what I came up with.

#!/usr/bin/perl6

my %number_of;

my $file = "CallRecords.txt";
if (my $fh = open $file, :r) {
    for =$fh -> $line {
        next if $line !~~ /Answered/ or $line ~~ /Private/;
        my ($name, $phone) = $line.split("\t")<2, 3>;
        %number_of{$name.uc} = $phone;
    }
else {
    say "Could not open $file";
}

for %number_of.sort.kv -> $name, $phone {
    say $name ~ ' ' ~ $phone;
}

Here's the output:

0 ABXXON ROLLY  (408) 386-XXXX
1 AGUXXAR R & J (408) 251-XXXX
2 AGUXXAR RONNIE        (408) 768-XXXX
3 ALMXXEN FAMILY        (408) 997-XXXX
4 XXER FRATERNAL        (408) 984-XXXX
5 AQXXNO GREG   (408) 297-XXXX
6 AQXXNO GREG R (408) 297-XXXX
7 ARGONCXXLO S  (408) 937-XXXX
8 ARGONCXXLO SUSA       (408) 937-XXXX
9 ASXXAS DAISY  (831) 240-XXXX
10 BALIXXIT VENTUR      (408) 251-XXXX
11 XXNK OF AMERICA      (866) 512-XXXX
12 BEXXER LIVING I      (408) 561-XXXX
13 BOUXXER      CO      (303) 786-XXXX
14 CALIFOXXIA EAR       (650) 494-XXXX
15 CAMXXHO GEMMA        (408) 849-XXXX
...

(I replaced some letters and numbers with X's in this post for security purposes.)

Oh my goodness! What an elegant piece of code that is. And it didn't take me a while to figure it out. I simply read tutorials from Gabor and Jonathan. I tell you, I can't believe the expressiveness of the latest and greatest perl. I have barely scratched the surface, it's got tons of other amazing stuffs! Try it for yourself.

0 comments: