Learning Parrot with the Parrot Shell

| | TrackBacks (0)
I recently have been working a lot on Parrot (I am the release manager for 1.7) and learning by the seat of my pants. The first thing that I yearned for was something REPL-like, for rapid prototyping and just hacking out code with immediate feedback. Enter the Parrot Shell. To start up the shell, go to the root of the Parrot source code and type:

perl tools/dev/parrot_shell.pl

This should be after you have configured and compiled Parrot with

perl Configure.pl && make

You should see something like:

Welcome to the Parrot Shell, it's experimental!
Type h or help for some basic help
Type q or quit to flee the madness

parrot_shell 0>

The Parrot Shell reads input until it sees a single period (".") on a line (ignoring whitespace), then executes the code that you gave it. That is it. As a simple first example, let's print something:

parrot_shell 0> say "This Parrot talks!"
Time: 0.062809 wallclock secs
This Parrot talks!

But you may ask, there is no ".sub main" followed by a ".end" at the end of the code, so that is not valid PIR. You would be correct. The Parrot Shell wraps the commands you give it in ".sub main" and ".end" if the first line of your input does not start with ".sub". So it allows one to be lazy for short pieces of code, but allows specifying the name of the main subroutine for when that is necessary. This shows how to do that:

parrot_shell 2> .sub main
.sub foo
        say "foo"  
Time: 0.0438812 wallclock secs

Some other things to know about the Parrot Shell are that you can type "h" or "help" to get a quick reminder of how Parrot Shell works and that each numbered Parrot Shell session is run in its own interpreter, so no registers or variables are shared/leaked between them. To leave the Parrot Shell, just type "q" or "quit" . 

parrot_shell 1> quit
Thanks for visiting the Parrot Shell, come back soon

Here is one more example which shows basic interaction with Parrot registers via the Parrot Shell

parrot_shell 0>
$I0 = 42
$N1 = sqrt $I0
say $N1
Time: 0.0245831 wallclock secs

The Parrot Shell is great for pasting code snippets from Trac tickets or from people on IRC asking "does this code do X on your system?" I also find it is very helpful to have a Parrot Shell open while reading Parrot documentation (like the PIR book), so that you can test things as you learn them. In addition to all the docs that Parrot comes with, there are some really good online guides popping up, like Brian Wisti's Parrot Baby Steps.

If you are trying to write tests for a certain Parrot feature, the Parrot Shell can often help you figure out how to write it in the least amount of time.

I hope that the Parrot Shell gives you an easy on-ramp to start playing with Parrot, please let me know how you use it and if you have any suggestions for improvement. As always, patches welcome!

0 TrackBacks

Listed below are links to blogs that reference this entry: Learning Parrot with the Parrot Shell.

TrackBack URL for this entry: http://leto.net/mt/mt-tb.cgi/160

About this Entry

This page contains a single entry by Jonathan Leto published on September 30, 2009 8:26 PM.

Blizkost is passing 102 tests! was the previous entry in this blog.

Parrot Hacktivity Report is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.

Clicky Web Analytics 42