(my $title = "Think Python") ~~ s/Python/Perl 6/ && $title.say;

I'm rereading Allen B. Downey's Think Python, but this time with an eye for writing the equivalent code in Perl 6. I am not sure how deep I'll dig into this, as I am limited by spare time and tools from the book like Swampy would have to be made accessible to Perl 6 somehow. BTW, Think Python is available under a CC-by-nc 3.0 license.

Just using this space as a public scratchpad at the moment. I've got $RAKUDO_HOME/install/bin on my path to simplify things.

$ perl6 --version
This is perl6 version 2012.12 built on parrot 4.10.0 revision 0

Chapter 1

Mostly an explanation of how computer programs work, and bits about the difference between high level and low level languages. Only one actual code sample.

Invoking perl6 without any arguments will start the REPL, shell, interactive mode (call it what you will).

$ perl6
>

Default presentation is a single chevron for user code, and no decoration for output:

> 1 + 1
2

The First Program

say covers the print-with-newline angle. We can use it with or without parentheses.

> say 'Hello, world!';
Hello, world!
> say('Hello, world!');
Hello, world!

But say is also a method attached to objects. Perl 6 literals are objects, too.

> 'Hello, world!'.say;
Hello, world!

Of course, this could lead you down the rabbit hole of explaining objects and literals when the chapter is mostly about "what the heck is a program?" and "how do I get perl6 to do stuff?"

Exercises

If I were actually writing Think Perl 6, the Chapter 1 Exercises would look something like this.

  1. Go to the Perl 6 website. This page contains information about Perl 6 and links to pages related to Perl 6. You can also browse a wealth of documentation about Perl 6. not so much on the searching, though
  2. It doesn't appear that the perl6 REPL includes a built-in help tool, and the POD does not yet have a perl6toc which ties all the installed documentation together. Best to suggest the doc index for now.
  3. Start the perl6 interpreter and use it as a calculator. The Perl 6 syntax for math operations is almost the same as standard mathematical notation

filler line inserted to make Markdown happy

  > 10 / 1.61 # Convert km to miles
  6.211180
  > (43 * 60) + 30 # Convert time to seconds
  2610
  > 2610 / 6.211180 # Average time per mile, in seconds
  420.2100084
  > 420.2100084 / 60 # Average time per mile, in minutes
  7.00350014
  > 60 / 7.00350014 # Miles per hour
  8.5671448277

All this to show that perl6 as a calculator works about the same as python as a calculator, though if you compare to one solution you'll see a difference in the number of significant digits.

I actually have notes for Chapter 2, but my compulsion to begin at the beginning means that this is all I have time for today.

Looks like I made it through Chapter 2

2 Comments

> You can also browse a wealth of documentation about Perl 6. not so much on the searching, though

And if you use vanilla google, you'll generally get a wealth of info that's out of date or non-technical advocacy or trolling.

One helper is this custom google search engine for generally high quality Perl 6 info.

(A second attempt to get a comment accepted on this blog post. Apologies if it ends up being a duplicate.)

If anyone reading along is thinking of installing Rakudo, make sure to install Rakudo Star (aka R*) if you can. R* includes doc, modules, the awesome debugger, and so on.

Search new or worthwhile Perl 6 resources. (For example, try searching for "calculator" or "rationals".)

Leave a comment

About Brian Wisti

user-pic I’m a geek in Seattle. I write code. I enjoy writing about code, regardless of whether anyone else reads it. I also knit.