Last night I reached chapter 4 of Think Python, which is full of good old fashioned turtle graphics stuff.

It’s just - well - there’s a problem. I don’t think there is a port of the swampy library to Perl 6. I could write one, but that would probably require a Tk interface with Perl 6. Even just writing a Perl 5 Tk version of Swampy is beyond my spare time attention span.

I know what I’ll do. I’ll cheat and have perl6 call python.

First, I install Swampy. It is available on PyPi;

$ tar xfvz swampy-2.1.1.tar.gz
$ cd swampy
$ python setup.py build
$ sudo python setup.py install

If you didn’t break your pip like I did the other day, you can probably just pip install swampy.

Right now I’m just shooting for enough to make the first example work. fd and lt need to be implemented, and nothing else.

use v6;

my $turtle = 'turtle';
my @directives;

sub fd($distance) {
    @directives.push("fd($turtle, $distance)");

sub lt() {

sub swampy() {
    my $code = @directives.join("\n");

    my $full-code = qq:to/END/;
from swampy.TurtleWorld import *

world = TurtleWorld()
$turtle = Turtle()



    my $python-file = save-code $full-code;
    say "Python code is in $python-file";
    run-python-with $python-file;

sub save-code($code, $name='default.py') {
    my $handle = open $name, :w;

    return $name;

sub run-python-with($script) {
    my $python = qx{which python}.chomp;
    my $result = qqx{$python $script}.chomp;

    if $result {
        say "$python output:";
        say $result;


Total cheat. I do one-for-one generation of Python Swampy directives from Perl 6, and when everything is generated I create a Python file and hand that off to python. Despite my good intentions of looking for the default Python interpreter, the odds are good that this will only work on my machine.

Even so, I need to make this a module. I don’t want to copy and paste all this every time I want to work through a Swampy example in the book. I’m sticking with a module interface rather than a class so that I can keep the invocation style used in this chapter of Think Python. Also, I’m not entirely certain that I am ready for Perl 6 classes quite yet. Maybe later.

Rather than bore you with details, I’ll send the patient reader to perl6-swampy. This is a simple stupid Perl 6 module that can be downloaded, and then installed with Panda. It probably won’t stay simple for long, but let’s hope it stays stupid. I’m no good with code that’s smarter than I am.

And then the starter code for Chapter 4?

use v6;

use Swampy;



Yes, I have grand ideas for Swampy.pm6, including crazy stuff like documention. And actual functionality. But hey. It works. I can draw a line and turn left, but in Perl 6. Yay me!

All I had to do was cheat.


I'm having fun watching you have fun with these blog posts. :)

You might want to check out the Turtle class in the Spiral and Zig-zag matrix entries from this search for turtle on Rosetta code.

Also, Create and Distribute Modules. :)

Why would you do all kinds of difficult work, when you can easily cheat?
( Like using the elevator instead of the stairs )

Perl has stolen many ideas from other languages.
Why not cheat when you are writing answers (in Perl) to questions from their programming books.

Hi Brian,

Not sure why, but a bunch of my comments on your posts have been held back for moderation. (I think I've added at least one comment to each four of the posts you've done so far in this Perl 6 series.)

I just tried again. I think it sees the links in my comments and decides they need to be approved by you: https://plus.google.com/photos/115289448037897603962/albums/5847878595708182609/5847878599956185890

Thanks. I hope you have time to read the comments I added; they took a while to create and provide what I hope is useful info addressing particular points you raised or queried in your posts.

If you do nothing else, make sure you read an intro to the debugger (which jnthn wrote in a few days!). It's bundled with Rakudo Star; just type per6-debug.


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.