Deep Into Pharo Alexandre Bergel, Damien Cassou, St´Ephaneducasse, Jannik Laval
Total Page:16
File Type:pdf, Size:1020Kb
Deep into Pharo Alexandre Bergel, Damien Cassou, St´ephaneDucasse, Jannik Laval To cite this version: Alexandre Bergel, Damien Cassou, St´ephaneDucasse, Jannik Laval. Deep into Pharo. Square Bracket Associates, pp.420, 2013, 978-3952334164. <hal-00858725> HAL Id: hal-00858725 https://hal.inria.fr/hal-00858725 Submitted on 5 Sep 2013 HAL is a multi-disciplinary open access L'archive ouverte pluridisciplinaire HAL, est archive for the deposit and dissemination of sci- destin´eeau d´ep^otet `ala diffusion de documents entific research documents, whether they are pub- scientifiques de niveau recherche, publi´esou non, lished or not. The documents may come from ´emanant des ´etablissements d'enseignement et de teaching and research institutions in France or recherche fran¸caisou ´etrangers,des laboratoires abroad, or from public or private research centers. publics ou priv´es. Distributed under a Creative Commons Attribution - ShareAlike 4.0 International License Deep into Pharo ESUG 2013 Edition Alexandre Bergel Damien Cassou Stéphane Ducasse Jannik Laval ii This book is available as a free download from http://rmod.lille.inria.fr/deepIntoPharo/. Copyright © 2013 by Alexandre Bergel, Damien Cassou, Stéphane Ducasse and Jannik Laval. The contents of this book are protected under Creative Commons Attribution-ShareAlike 3.0 Unported license. You are free: to Share — to copy, distribute and transmit the work to Remix — to adapt the work Under the following conditions: Attribution. You must attribute the work in the manner specified by the author or licensor (but not in any way that suggests that they endorse you or your use of the work). Share Alike. If you alter, transform, or build upon this work, you may distribute the resulting work only under the same, similar or a compatible license. • For any reuse or distribution, you must make clear to others the license terms of this work. The best way to do this is with a link to this web page: creativecommons.org/licenses/ by-sa/3.0/ • Any of the above conditions can be waived if you get permission from the copyright holder. • Nothing in this license impairs or restricts the author’s moral rights. Your fair dealing and other rights are in no way affected by the above. This is a human-readable summary of the Legal Code (the full license): creativecommons.org/licenses/by-sa/3.0/legalcode Published by Square Bracket Associates, Switzerland. http://SquareBracketAssociates.org ISBN 978-3-9523341-6-4 First Edition, August, 2013. Cover art by Jérôme Bergel ([email protected]). Contents 1 Preface 1 I Libraries 2 Zero Configuration Scripts and Command-Line Handlers 7 2.1 Getting the VM and the Image . 7 2.2 Getting the VM only . 9 2.3 Handling command line options . 9 2.4 Anatomy of a handler . 12 2.5 Using ZeroConf script with Jenkins . 14 2.6 Chapter summary. 14 3 Files with FileSystem 15 3.1 Getting started . 15 3.2 Navigating a file system . 16 3.3 Opening read and write Streams . 19 3.4 Renaming, copying and deleting Files and Directories. 20 3.5 The main entry point: FileReference . 21 3.6 Looking at FileSystem internals . 26 3.7 Chapter summary. 29 4 Sockets 31 4.1 Basic Concepts . 31 4.2 TCP Client . 33 4.3 TCP Server . 37 iv Contents 4.4 SocketStream . 43 4.5 Tips for Networking Experiments . 48 4.6 Chapter summary. 49 5 The Settings Framework 51 5.1 Settings architecture . 51 5.2 The Settings Browser. 53 5.3 Declaring a setting . 56 5.4 Organizing your settings . 61 5.5 Providing more precise value domain . 65 5.6 Launching a script . 68 5.7 Setting styles management . 69 5.8 Extending the Settings Browser . 70 5.9 Chapter summary. 75 6 Regular Expressions in Pharo 77 6.1 Tutorial example — generating a site map . 78 6.2 Regex syntax . 85 6.3 Regex API . 91 6.4 Implementation notes by Vassili Bykov . 96 6.5 Chapter summary. 97 II Source Management 7 Versioning Your Code with Monticello 101 7.1 Basic usage . 102 7.2 Exploring Monticello repositories . 113 7.3 Advanced topics . 115 7.4 Getting a change set from two versions . 120 7.5 Kinds of repositories . 121 7.6 The .mcz file format . 124 7.7 Chapter summary. 126 8 Gofer: Scripting Package Loading 129 8.1 Preamble: Package management system. 129 v 8.2 What is Gofer? . 132 8.3 Using Gofer . 133 8.4 Gofer actions . 135 8.5 Some useful scripts . 141 8.6 Chapter summary. 144 9 Managing Projects with Metacello 147 9.1 Introduction . 147 9.2 One tool for each job . 148 9.3 Metacello features. 149 9.4 A simple case study . 151 9.5 Loading a Metacello Configuration . 154 9.6 Managing dependencies between packages . 155 9.7 Baselines . 157 9.8 Groups. 160 9.9 Dependencies between projects . 163 9.10 About dependency granularity . 169 9.11 Executing code before and after installation . 171 9.12 Platform specific package . 172 9.13 Milestoning development: symbolic versions . 176 9.14 Load types . 181 9.15 Conditional loading . 183 9.16 Project version attributes . 185 9.17 Chapter summary. 186 III Frameworks 10 Glamour 191 10.1 Installation and first browser . 191 10.2 Presentation, Transmission and Ports . 194 10.3 Composing and Interaction . 200 10.4 Chapter summary. 207 11 Agile Visualization with Roassal 209 11.1 Installation and first visualization . 209 vi Contents 11.2 Roassal core model . 212 11.3 Detailing shapes . 218 11.4 Edges: linking elements . 220 11.5 Layouts . 224 11.6 Events and Callbacks . 230 11.7 The interaction hierarchy . 231 11.8 Understanding a View’s Camera . 235 11.9 Beyond Pharo . 238 11.10 Chapter summary. 239 12 Scripting Visualizations with Mondrian 243 12.1 Installation and first visualization . 243 12.2 Starting with Mondrian. 244 12.3 Visualizing the collection framework . 249 12.4 Reshaping nodes . 250 12.5 Multiple edges . 251 12.6 Colored shapes . 253 12.7 More on colors . 254 12.8 Popup view . 256 12.9 Subviews . 257 12.10 Forwarding events . 258 12.11 Events . 259 12.12 Interaction . 259 12.13 Chapter summary. 261 IV Language 13 Handling Exceptions 267 13.1 Introduction . 267 13.2 Ensuring execution . 268 13.3 Handling non-local returns . 269 13.4 Exception handlers . 270 13.5 Error codes — don’t do this! . 272 13.6 Specifying which exceptions will be handled . 273 vii 13.7 Signaling an exception . 275 13.8 Finding handlers . 277 13.9 Handling exceptions . 279 13.10 Comparing outer with pass . 285 13.11 Exceptions and ensure:/ifCurtailed: interaction . 286 13.12 Example: Deprecation . 288 13.13 Example: Halt implementation . 289 13.14 Specific exceptions . 290 13.15 When not to use exceptions . 292 13.16 Exceptions implementation . 293 13.17 Ensure:’s implementation . 297 13.18 Chapter summary. 303 14 Blocks: a Detailed Analysis 305 14.1 Basics . 305 14.2 Variables and blocks . 307 14.3 Variables can outlive their defining method . 314 14.4 Returning from inside a block . 316 14.5 Contexts: representing method execution . 322 14.6 Message execution . 325 14.7 Chapter conclusion . 328 15 Exploring Little Numbers 331 15.1 Power of 2 and Numbers . 331 15.2 Bit shifting is multiplying by 2 powers . 333 15.3 Bit manipulation and access . 335 15.4 Ten’s complement of a number . 337 15.5 Negative numbers . 338 15.6 Two’s complement of a number . 339 15.7 SmallIntegers in Pharo . 342 15.8 Hexadecimal . 344 15.9 Chapter summary. 345 viii Contents 16 Fun with Floats 347 16.1 Never test equality on floats . 347 16.2 Dissecting a Float . 349 16.3 With floats, printing is inexact . 353 16.4 Float rounding is also inexact . 354 16.5 Fun with inexact representations . 355 16.6 Chapter summary. 356 V Tools 17 Profiling Applications 359 17.1 What does profiling mean? . 359 17.2 A simple example . 360 17.3 Code profiling in Pharo. 361 17.4 Read and interpret the results . 364 17.5 Illustrative analysis . 369 17.6 Counting messages . 371 17.7 Memorized Fibonacci . 371 17.8 SpaceTally for memory consumption per Class . 373 17.9 Few advices . 374 17.10 How MessageTally is implemented? . 374 17.11 Chapter summary. 375 18 PetitParser: Building Modular Parsers 377 18.1 Writing parsers with PetitParser . 377 18.2 Composite grammars with PetitParser . 386 18.3 Testing a grammar . 391 18.4 Case Study: A JSON Parser . 393 18.5 PetitParser Browser . 398 18.6 Packrat Parsers . 410 18.7 Chapter summary. 411 Chapter 1 Preface Using a programming language is so far the most convenient way for a human to tell a computer what it should do. Pharo is an object-oriented programming language, highly influenced by Smalltalk. Pharo is more than a syntax and a bunch of semantics rules as most programming languages are. Pharo comes with an extensible and flexible programming environ- ment. Thanks to its numerous object-oriented libraries and frameworks, Pharo shines for modeling and visualizing data, scripting, networking and many other ranges of applications. The very light syntax and the malleable object model of Pharo are com- monly praised. Both early learners and experienced programmers enjoy the “everything is an object” paradigm. The simplicity and expressiveness of Pharo as well as a living environment empowers programmers with a won- derful and unique sensation. Deep into Pharo is the second volume of a book series initiated with Pharo by Example1. Deep into Pharo, the book you are reading, accompa- nies the reader for a fantastic journey into exciting parts of Pharo.