Let’s get you going with Pharo programming as quickly and as easily as possible. Starting from nothing, we shall write our very first Pharo program (“Hello World”) within 10 minutes.
From http://pharo.org/web/download, you can find downloads for Windows, macOS, and Linux. What you are actually downloading is the Pharo Launcher, a powerful tool that allows you to create a variety of Pharo images. You can then launch any of those images as you like.
For Linux, you need to change directory to where the Pharo Launcher was downloaded to and execute the script:
You can put this into a shell script (I called mine start) and run it as a program by setting the permission (see the properties of the script file).
Launching the Pharo image
When you start up the Pharo Launcher, you will see two sections: Templates and Existing images (which is initially empty). From the Templates side, we’ll pick on the latest stable official distribution (Pharo 6.1 at the time of writing). Right-click and choose “Create image”.
To launch any of the Existing images, right-click and choose “Launch”. Easy-peasy.
At this point, we could actually write “Hello World” immediately. Using the Playground, which is a scratchpad area where we can execute code snippets for exploratory reasons, we can type the following line:
Transcript show: 'Hello World'
Select the text, right-click on it and choose “Do it”. This will print “Hello World” to the Transcript window, which is like a system console for logging messages. To open the Transcript, left-click on open space or the Pharo background to bring up the World menu, select Tools, and then select Transcript.
To open the Playground, just left-click on open space to bring up the World menu, then select Playground.
This was easy but we’d like to package our code into a proper program. The way to do this is by using the System Browser.
Getting acquainted with the Pharo IDE
Pharo’s System Browser is the main interface to the Pharo programming system. It’s much like today’s IDEs such as Eclipse and IntelliJ IDEA, only much simpler and more elegant. To open the System Browser, left-click on open space to bring up the World menu, then select System Browser.
The System Browser consists of several panes:
- the Packages pane is where you see all the logical groupings of classes
- the Classes pane is where you see all the classes for a particular package
- the Protocols pane is where you see the logical groupings of methods for a particular class
- the Methods pane is where you see the methods for a particular protocol
- the Edit pane is where you can view, edit, or create new methods and classes
- the Quality Assistant pane is where you can see helpful hints and tips for whatever you are currently editing
(For now, don’t worry about OOP parlance like classes or methods. You’ll get around to them soon enough.)
You can create classes or methods in the Edit pane simply by editing what is there or clearing everything and typing from scratch. Make sure the class or method pane gets the focus first before you create a class or method, respectively. To save your edits, press Ctrl-S (or Cmd-S under macOS).
Creating a package and class for our Hello World program
For our Hello World program, we need to create a package and class for it. We shall name the class #Greeter and the package ‘HelloWorld’. Edit or type the following in the Edit pane:
Object subclass: #Greeter
Don’t forget to save (Ctrl-S or Cmd-S).
Creating a method for our Hello World program
Now we need to create a method or function for our Greeter class. The standard first method is ‘initialize’ which is invoked when the Greeter class is instantiated (this is OOP parlance). For simplicity, we shall skip this.
We create a method called ‘sayIt’ and print “Hello World” in this method.
Select “no messages” in the Protocols pane so that the IDE knows we are working with methods. Edit or type the following in the Edit pane:
Transcript show: 'Hello World'
Executing our Hello World Program
Our Hello World program prints to the Transcript. To open the Transcript, left-click on open space to bring up the World menu, select Tools, and then select Transcript.
To run our program, we need to open the Playground. To open the Playground, left-click on open space to bring up the World menu, then select Playground. In Playground, enter the following text:
Greeter new sayIt
Select the text, right-click and choose “Do it”. You should immediate see “Hello World” in the Transcript window.
Congratulations! We’ve created our first Pharo program. To save the program — indeed, to save the entire state of the Pharo image — select “Save” or “Save and quit” from the World menu, or say Yes to “Save changes before quitting?” when you close the Pharo window.
If you enjoyed this Quick Start guide, look at the sequel: Pharo: The Next Ten Minutes.