Camelia

Getting started with Rakudo Perl 6 in a Docker container

Docker is a container of a light-weight Linux system that can run on Linux, MS Windows, and Mac OSX. In this article you'll see how to use it to start writing Perl 6 code.

Note! This site is about Perl 6.
If you are looking for a solution for Perl 5, please check out the Perl 5 tutorial.

Download and Install Docker

Launch the Docker daemon

Run

docker run -it rakudo-star

This might take some time as it needs to download the Rakudo Docker image. The output will look something like this:

Unable to find image 'rakudo-star:latest' locally
latest: Pulling from library/rakudo-star
693502eb7dfb: Already exists
081cd4bfd521: Pull complete
5d2dc01312f3: Pull complete
e090aa29a3ec: Pull complete
c3439586dbe8: Pull complete
Digest: sha256:eac1ce2634c62857ee7e5e3f23b2156206edef779da47c08c4ac508f905ca60d
Status: Downloaded newer image for rakudo-star:latest
To exit type 'exit' or '^D'
>

Then you can type in your Perl 6 code:

> say 'Hello Rakudo'
Hello Rakudo

This Rakudo runs on a Linux box. Let's look around:

The Current Working Directory can be accessed via the $*CWD variable:

> say "$*CWD"
/

We can also list all the directories:

> for dir() -> $f { say "$f" }
usr
media
etc
root
mnt
boot
proc
lib
sys
home
run
lib64
tmp
bin
dev
opt
var
srv
sbin
.dockerenv

We can check out the version of Perl:

> $*PERL
Perl 6 (6.c)

the version of Rakudo:

> $*VM
moar (2017.01)

and the location of the current Perl 6 executable:

> $*EXECUTABLE
"/usr/bin/perl6".IO

We can also check the Linux distribution used for this Docker image and the version of the Linux kernel:

> $*DISTRO
debian (8.jessie)
> $*KERNEL
linux (4.9.27.moby)

If we type exit we will leave the Perl 6 interactive shell and with that we'll leave the Docker Image and get back to the prompt of our host operating system.

Perl 6 from terminal

If we run the following Docker command:

docker run -it --name rakudo -v $(pwd):/opt rakudo-star bash

(On Windows, you will need to replace $(pwd) the path to the current directory.)

it will launch a Docker container called "rakudo" based on the "rakudo-star" image. It will map the current working directory of the host operating system to /opt inside the container, and finally, instead of launching the interactive shell, it will launch a regular shell running bash.

You'll see a prompt like this one:

root@1fa71fb0cfcb:/#

You can execute subset of the regular Unix/Linux Bash commands such as "pwd" and "ls". If you type "ls -l opt/" you'll see the content of the directory on the host OS.

Install security fixes

If you need other commands, you can install them using apt-get. In order for that to work properly first we need to update the package list and if we have done that we can also install the recent security fixes of the Linux distribution used.

root@7d03b913aeca:/# apt-get update

0% [Working]
Get:1 http://security.debian.org jessie/updates InRelease [63.1 kB]
Ign http://deb.debian.org jessie InRelease
Get:2 http://deb.debian.org jessie-updates InRelease [145 kB]
Get:3 http://deb.debian.org jessie Release.gpg [2373 B]
Get:4 http://deb.debian.org jessie Release [148 kB]
Get:5 http://security.debian.org jessie/updates/main amd64 Packages [453 kB]
Get:6 http://deb.debian.org jessie-updates/main amd64 Packages [17.6 kB]
Get:7 http://deb.debian.org jessie/main amd64 Packages [9049 kB]
Fetched 9878 kB in 15s (644 kB/s)
Reading package lists... Done

root@7d03b913aeca:/# apt-get -y upgrade

Install less, make, and gcc

The Linux-box is so bare-bones that less is not installed and we'll need it to use p6doc. In addition we'll need make and gcc for some of the additional installation.

In order to check what is the name of the package that contains the less command type in:

# apt-cache search less | grep ^less

less - pager program similar to more

Then to install it type in:

# apt-get install -y less

You can verify that the installation succeeded by typing

# which less

/usr/bin/less

In order to install make and gcc type in:

# apt-get -y install make gcc

First Perl 6 script

Using your tools on the host Operating system, create a file called hello_world.pl in the directory where you launched the Docker container, and add the following two lines:

examples/hello_world.pl

use v6;
say "Hello from Perl 6";

Now you can run the script by typing in the following:

perl6 /opt/hello_world.pl

@1fa71fb0cfcb:/# perl6 /opt/hello_world.pl
Hello from Perl 6

Getting Help with p6doc

p6doc is the command-line tool to read documentation of Perl 6. Try it!

# p6doc
# p6doc -l
# p6doc io

Alternatively, visit The Perl 6 documentation.

Installing modules

The CPAN of Perl 6 is more limited than the CPAN of Perl 5, but a lot of things that are implemented in Perl 5 modules are already available in the core of Perl 6.

In any case you can find 3rd-party modules for Perl 6.

You can install modules using zef. For example we can install Terminal::Readsecret in order to read passwords on the command-line without echoing back to the screen:

# zef install Terminal::Readsecret

===> Searching for: Terminal::Readsecret
===> Building: Terminal::Readsecret:ver('0.0.2')
===> Building [OK] for Terminal::Readsecret:ver('0.0.2')
===> Testing: Terminal::Readsecret:ver('0.0.2')
t/01-basic.t ..... ok
passwordXXXX
t/02-password.t .. ok
All tests successful.
Files=2, Tests=2,  3 wallclock secs
Result: PASS
===> Testing [OK] for Terminal::Readsecret:ver('0.0.2')
===> Installing: Terminal::Readsecret:ver('0.0.2')

Using Terminal::Readsecret

Create a small script copying on the example from the README of Terminal::Readsecret.

Save this content as read_secret.pl in the directory where you launched the Docker container:

examples/read_secret.pl

use v6;
use Terminal::Readsecret;
my $password = getsecret("password:" );
say "your password is: " ~ $password;

Then run

perl6 /opt/read_secret.pl

password:
your password is: Hello World

It is supposed to prompt for a password. As you type in the text it should not show anything. Then when you press enter it should display the password.

Exit and restart the container

Now you can type exit (this is the Unix exit command) which will leave and close the Docker container.

Running the container again

If you try to run the same container again using the same command:

# docker run -it --name rakudo -v $(pwd):/opt rakudo-star bash

it will fail with a message like this:

docker: Error response from daemon: Conflict. The container name "/rakudo" is already in use by container 7d03b913aeca4c45e386b2e73d060428d7e3b07ab0b7e56a8c6f8eb0b2eb582a. You have to remove (or rename) that container to be able to reuse that name..
See 'docker run --help'.

That's because we've already created a container called "rakudo". We could remove it or we could create a new container with another name, but then we would need to start the installations again. Instead of that we could use the existing container:

# docker start rakudo && docker attach rakudo

It will print "rakudo" and then it will seem to hang without showing a prompt. You have to press ENTER again in order to get to the prompt.

With this you are back to your same prompt, with all the installed stuff. With the mounting of your directory to /opt/ and even with your Bash History.

Enjoy Perl 6 in your Docker!


The Perl 6 Tricks and Treats newsletter has been around for a while. If you are interested to get special notification when there is new content on this site, it is the best way to keep track:
Email:
Full name:
This is a newsletter temporarily running on my personal site (szabgab.com) using Mailman, till I implement an alternative system in Perl 6.
Gabor Szabo
Written by Gabor Szabo

Published on 2017-03-25



Comments

In the comments, please wrap your code snippets within <pre> </pre> tags and use spaces for indentation.
comments powered by Disqus
Suggest a change
Elapsed time: 2.3443375

Perl 6 Tricks and Treats newsletter

Register to the free newsletter now, and get updates and news.
Email:
Name: