This version of the book is based on the previous version authored by Andrew P. Black, Stéphane Ducasse, Oscar Nierstrasz, Damien Pollet, Damien Cassou and Marcus Denker called, Pharo by Example. It also builds on the version that was issued for Pharo 50, Updated Pharo by Example, authored by Stéphane Ducasse and edited by Dmitri Zagidulin, Nicolai Hess, and Dimitris Chloupis.
Many aspects of Pharo's tooling have changed since the last edition, and we have worked hard to cover these changes:
- We have reorganized the beginning of the book to get shorter chapters.
- We have introduced a new 'first contact' chapter with a simple counter example. This lets the reader see the most important operations to define a class, its tests, and save its code.
- We briefly cover the new system browser, Calypso, as well as the Pharo Launcher.
- We have introduced a new chapter covering Iceberg and package management. A larger book on Pharo and git is available at http://books.pharo.org.
- We added a new chapter on Traits.
- We have simplified the SUnit chapter, since a companion book is now available at http://books.pharo.org/
- We have revised parts of the book that were wrong in previous editions, such as the Morphic chapter; the current version is a considerable improvement over Pharo by Example 5.
Pharo by Example has received many edits, modifications and updates to bring it into line with the current version of Pharo. It does not magically update itself; improvements are the result of many people's hard work; one does not simply change a '5.0' to a '9'. This is why I decided, as one of the original and main authors of the book over the years, to invite Sebastijan, Gordana and Quentin to be listed as authors of Pharo by Example 9.
Pharo is a modern, open-source, dynamically-typed language that supports live coding and is inspired by Smalltalk. Pharo and its ecosystem are composed of six fundamental elements:
- A dynamically-typed language with a minimalist syntax, similar to natural language writing, that can fit on a postcard and be read by people who are unfamiliar with it.
- A live coding environment that allows the programmer to seamlessly modify their code as it executes.
- A powerful IDE providing tools to help manage complex code and promote good design.
- A rich library that creates an environment so powerful that it can be viewed as a virtual OS, including a very fast JITing VM and full access to OS libraries and features via its FFI.
- A culture where changes and improvements are encouraged and highly valued.
- A community that welcomes coders from any corner of the world, of any skill level or experience, in any programming language.
Pharo strives to offer a lean, open platform for professional software development, as well as a robust and stable platform for research and development into dynamic languages and environments. Pharo serves as the reference implementation for the Seaside web development framework, available at http://www.seaside.st.
Pharo's core contains only code that has been contributed under the MIT license. The Pharo project started in March 2008 as a fork of Squeak (a modern implementation of Smalltalk-80), and the first 1.0 beta version was released on July 31, 2009. Since then, Pharo has reached a new version every year or year and a half. The current version is Pharo 9.0, released in July 2021.
Pharo is highly portable. Pharo can run on OS X, Windows, Linux, Android, iOS, and Raspberry Pi. Its virtual machine is written entirely in a subset of Pharo, making it easy to simulate, debug, analyze, and change from within Pharo itself. Pharo is the vehicle for a wide range of innovative projects, from multimedia applications and educational platforms to commercial web development environments.
There is an important principle behind Pharo: Pharo does not just copy the past, it reinvents the essence of Smalltalk. However, we realize that Big Bang style approaches that start from scratch rarely succeed. Pharo instead favors evolutionary and incremental changes. Rather than leaping for the perfect solution in one big step, a multitude of small changes keeps even the bleeding edge relatively stable while allowing us to experiment with important new features and libraries. This facilitates contributions and rapid feedback from the community, which Pharo relies on for its success. Finally, Pharo is not read-only: changes from the community are integrated daily. Pharo has around 100 contributors, based all over the world. You can have an impact on Pharo too! Just take a look at http://github.com/pharo-project/pharo.
This book will not teach you how to program. The reader should have some familiarity with programming languages. Some background in object-oriented programming would also be helpful.
The current book will introduce the Pharo programming environment, the language, and the associated tools. You will be exposed to common idioms and practices, but the focus is on the technology, not on object-oriented design. We will show you good, illustrative examples as often as possible.
An excellent MOOC (Massive open online course) for Pharo is freely available at http://mooc.pharo.org. The MOOC makes for a good introduction to Pharo and object-oriented programming, and complements this book well.
This book will not teach you everything you need, or want, to learn about Pharo. Here is a short commented list of other books that you can find at http://books.pharo.org:
- Learning Object-Oriented Programming, Design and TDD with Pharo. This book teaches the key aspects of object-oriented design and test-driven development. A good book to learn about object-oriented programming.
- Pharo with Style. This book is a must read. It discusses how to write good and readable Pharo code. In just one hour, you will boost the standard of your code.
- The Spec UI framework. This book will show you how to develop standard user interface applications in Pharo.
More technical books are:
- Managing your code with Iceberg. This book covers how to manage your code with git in further detail.
- Enterprise Pharo. This book contains different chapters relating to the web, converters, reporting, and documentation that you need for delivering applications.
- Deep into Pharo. This book covers more advanced topics than Pharo by Example.
And there are also books to expand your mind:
- A simple reflective object kernel revisits all the fundamental points of "objects all the way down" by taking you on a little journey to build a reflective language core. It is truly excellent.
In addition, there are numerous other books on Smalltalk freely available at http://stephane.ducasse.free.fr/FreeBooks.html.
Do not be frustrated by parts of Pharo that you do not immediately understand: You do not have to know everything. Alan Knight expresses this as follows:
Try not to care. Beginning Pharo programmers often have trouble because they think they need to understand all the details of how a thing works before they can use it. This means it takes quite a while before they can master Transcript show: 'Hello World'
. One of the great leaps in OO [object-oriented programming] is to be able to answer the question "How does this work?" with "I don't care".
When you do not understand something, simple or complex, do not hesitate to ask us on our mailing lists ([email protected] or [email protected]), IRC and Discord. We love questions and we welcome people of any skill.
This book is an open book in the following senses:
-
The content of this book is released under the Creative Commons Attribution-ShareAlike (by-sa) license. In short, you are allowed to freely share and adapt this book, as long as you respect the conditions of the license available at the following URL http://creativecommons.org/licenses/by-sa/3.0/.
-
This book just describes the core of Pharo. We encourage others to contribute chapters on the parts of Pharo that we have not described. If you would like to participate in this effort, please contact us. We would like to see more books about Pharo!
-
It is also possible to contribute directly to this book via GitHub. Just follow the instructions there and ask any questions you have on the mailing list, IRC or Discord. You can find the GitHub repo at https://github.com/SquareBracketAssociates/PharoByExample90
The Pharo community is friendly and active. Here is a short list of resources that you may find useful:
- http://www.pharo.org is the main web site of Pharo.
- http://www.github.com/pharo-project/pharo is the main GitHub account for Pharo.
- Pharo has an active on Discord server - a platform for chat based on IRC, just ask for an invitation on Pharo's website http://pharo.org/community, in the Discord section. Everybody is welcome.
- Pharoers started a wiki on Pharo: https://github.com/pharo-open-documentation/pharo-wiki
- An Awesome catalog is maintained with projects: https://github.com/pharo-open-documentation/awesome-pharo
- If you hear about SmalltalkHub, http://www.smalltalkhub.com/ was the equivalent of SourceForge/GitHub for Pharo projects for about 10 years. Many extra packages and projects for Pharo lived there. Now the community is mainly using git repositories such as GitHub, GitLab, and Bitbucket.
We have tried to provide as many examples as possible. In particular, there are examples that show a fragment of code which can be evaluated. We use a long arrow to indicate the result you obtain when you select an expression and from its context menu and choose print it:
3 + 4
>>> 7 "if you select 3+4 and 'print it', you will see 7"
In case you want to play with these code snippets in Pharo, you can download a plain text file with all the example code from the Resources sidebar of the original book's web site: http://books.pharo.org/pharo-by-example.
We always prefix method source code with the class of the method. This way you can also know of which class the method is.
For example, the book shows a method as:
MyExampleSetTest >> testIncludes
| full empty |
full := Set with: 5 with: 6.
empty := Set new.
self assert: (full includes: 5).
self assert: (full includes: 6).
self assert: (empty includes: 5) not
And if you want to type it into Pharo you should type the following in the corresponding class.
testIncludes
| full empty |
full := Set with: 5 with: 6.
empty := Set new.
self assert: (full includes: 5).
self assert: (full includes: 6).
self assert: (empty includes: 5) not
We would like to thank Alan Kay, Dan Ingalls and their team for making Squeak, an amazing Smalltalk development environment, that became the open-source project from which Pharo took root. Pharo also would not be possible without the incredible work of the Squeak developers.
We would also like to thank Hilaire Fernandes and Serge Stinckwich who allowed us to translate parts of their columns on Smalltalk, and Damien Cassou for contributing the chapter on Streams. We especially thank Alexandre Bergel, Orla Greevy, Fabrizio Perin, Lukas Renggli, Jorge Ressia and Erwann Wernli for their detailed reviews. We thank the University of Bern, Switzerland, for graciously supporting this open-source project and for hosting the web site of this book for some years.
We also thank the Pharo community for their enthusiastic support of this book project, as well as for all the translations of the first edition of Pharo by Example.
We want to thank the original authors of this book! Without the initial version it would have been difficult to make this one. Pharo by Example is a central book for Pharo to welcome newcomers, it has immense value.
Special thank to David Wickes for his massive copy-edit pass.
Thanks to Adrian Sampaleanu, Manfred Kröhnert, Markus Schlager, Werner Kassens, Michael OKeefe, Aryeh Hoffman, Paul MacIntosh, Gaurav Singh, Jigyasa Grover, Craig Allen, Serge Stinckwich, avh-on1, Yuriy Timchuk, zio-pietro, Vivien Moreau, Liwei Chou for the typos and feedback. Special thanks to Damien Cassou and Cyril Ferlicot for their great help in the book update.
Finally we want to thank Inria for its steady and important financial support, and the RMoD team members for the constant energy pushing Pharo forward. We want to thank also the Pharo consortium members.
Super special thanks to Damien Pollet for this great book template and Joseph Guégan for giving us the right to use his great picture of the Eckmuhl lighthouse' staircase.
S. Ducasse, S. Kaplar, Gordana Rakic, and Q. Ducasse