Rebol a Programmer’S Guide
Total Page:16
File Type:pdf, Size:1020Kb
Rebol A programmer’s guide Copyright 2008, Olivier Auverlot and Peter W A Wood First edition – December 2008 The programs in this book are intended to illustrate the topics under discussion. There is no guarantee given that they will function once compiled, assembled or interpreted in the context of professional or commercial use. By visiting the site at www.auverlot.fr you can • Talk with the authors • Download the example code • Look for updates and additions All rights reserved. Any reproduction, even partial, of the content, the cover or illustrations, by any means whatsoever (electronic, photocopier, tape or otherwise) is prohibited without written permission of the authors. Preface For many years the answer to the question “Which is the best book to learn Rebol?” has been Olivier Auverlot’s “Programmation Rebol”. As you probably guessed from the title, it’s written in French. Soon after it was published late in 2001, work started on a translation. It was even listed with a publication date on Amazon.com. Sadly, it has yet to see the light of day. At the beginning of 2007, Olivier published “Rebol - Guide du programmeur”. I hoped for news of a translation; but none came. It seemed that the only option was going to be to read the book in French. So why not translate the book myself? Translating “Rebol - Guide du programmeur” has been most enjoyable. Olivier’s writing is eloquent and informative. His command of Rebol shines through. I learned a lot whilst translating the book, some Rebol and some French. I hope you will too, though perhaps not so much French as me. Rebol – A programmer’s guide Acknowledgements I would like to thank Olivier not only for writing such a good book but equally for all the help and support he’s provided as I’ve been working on the translation. There are many helpful folks in the Rebol community. In particular, Gregg Irwin and Sunanda have been a constant source of friendship and encouragement. Thank you. Last but certainly not least, I would like to thank Noriati, my wife, and Hannah Sarah, our daughter, for their love, their unquestioning support and their forbearance during the many hours I’ve sat staring at my computer. Peter W A Wood 2 Foreword What is the purpose of this book? For five years, Olivier had the pleasure of working for the French computer magazine Login and wrote many articles about REBOL. Sadly Login is no longer published and Olivier felt it both necessary and appropriate to consolidate his work so that the accumulated knowledge would still be available to the Rebol community. Understandably, he initially published his work as “Rebol – Guide de Programmeur”. This book is a translation of the original into English. The basic idea was simply to regroup articles according to specific subject, but the project turned out to be much larger than that. Many articles required a partial rewrite, others needed to be supplemented and finally, some previously unpublished elements were added. The "remix" has been much greater than anticipated, but it was necessary to ensure that the book has a coherent structure. In the end, the book turned out to be a highly practical guide to Rebol for programmers. Rebol – A Programmer’s Guide The diversity of topics makes the book more of a reference manual than a complete introduction. It elaborates on a number of points from Olivier’s previous book “Rebol Programmation” published by Eyrolles (ISBN: 2-212- 11017-0). Who is this book for? This book is primarily aimed at Rebol developers. It was conceived and designed with them in mind, giving the maximum of knowledge across a wide range of topics in a concise form. Most importantly, it can save them a lot of time through the numerous examples provided. It also meets the needs of developers and students wishing to learn Rebol. Each chapter explains the strong points of the language and then applies that knowledge. Additionally, it meets the needs of policy makers by outlining the capability of Rebol technology. Various case studies and product presentations are used to study the implementation of the language with the help of examples. Finally, it is intended for system administrators interested in using Rebol to automate certain tasks (Unix server administration, managing grid computing, etc.). Many parts of this book are devoted to such topics. How is it structured? The book consists of seven chapters each built around a theme: • Chapter 1 is an overview of Rebol by example, • Chapter 2 deals with the basics of the language, • Chapter 3 covers graphical interfaces, graphics and sound, • Chapter 4 describes Rebol’s advance network programming features, vi Foreword • Chapter 5 is aimed at professional users of Rebol. It presents the use of Rebol in the context of e-business, • Chapter 6 provides advanced information for Rebol developers, • Chapter 7 is a series of workshops designed to facilitate learning the language through practice. vii Contents Introduction Chapter 1 – Discover Rebol in an hour Chapter 2 – The Rebol langauge Chapter 3 – GUI, graphics and sound Chapter 4 – Networking and the Internet Chapter 5 – Rebol for pros Chapter 6 – Rebol for geeks Chapter 7 – Practical applications Table of Contents Introduction 1 Join the REBOLution ! 1 Programmer, word builder 2 A virtual machine 3 A family of products 3 Rebol/Core 4 Rebol/View 4 Rebol/Command 5 Rebol/SDK 6 Rebol/IOS 6 A network programming language 6 Manipulating information 7 Object programming 7 The main applications written in Rebol 8 Protocols and dialects 8 Rebol and the web 8 Utilities as if it raining! 9 Multimedia and games 10 Downloading and installing 11 Using the console 14 Rebol – A programmer's guide Establishing a working environment 15 Chapter 1 - Discover Rebol in an hour 17 The robotFTP project 17 Project introduction 18 Technical considerations 18 The execution environment 18 The security manager 19 Writing the header 20 The principle datatypes 21 And now, the code! 22 Declaring a function 23 Manipulating URLs 24 Executing FTP commands 25 Defining and using dialects 25 The robotFTP dialect 26 Managing errors 27 Conditional expressions 28 Managing files 28 Completing and testing robotFTP 29 Adding a graphic layer 30 Managing a progress bar 30 Opening a window 31 Integration with the RobotFTP script 31 Summary 32 Chapter 2 – The Rebol language 33 Survival guide 33 Let's write a program 34 Running your script 35 Variables and datatypes 36 Declaring a variable 36 Finding the type of a variable 37 Using Constructors 37 Simple datatypes 38 Complex datatypes 38 Blocks 39 xii Table of Content Handling lists 40 Arrays 40 Navigating within a series 41 Accessing an element 42 Adding and removing elements 42 Modifying a series 43 Searching and sorting series 43 Copying and clearing series 43 Control structures and loops 44 Tests in Rebol 44 And else? 45 Multiple choices 46 Loop 46 The for loop family 47 Repeat, until and while 48 A simple game in Rebol 49 Functions and objects 50 Using functions and objects 50 Defining functions 51 Creating objects 53 Parsing and dialects 55 The art of handling character strings 55 Rebol parsing 56 Parsing using a dialect 56 A little parsing 57 Defining a dialect 58 Summary 60 Chapter 3 – GUI, graphics and sound 61 GCS and VID 62 Basic concepts 62 Styles 64 Attributes 64 Style layout 65 A dollar-euro converter 65 Image processing with VID 66 Using images 67 xiii Rebol – A programmer's guide Displaying an image 67 Modifying images 69 Applying special effects 70 The DRAW dialect 72 Let's draw a line 72 Other draw functions 74 Adding text 75 Manipulating images 76 Generating image files 77 Using DRAW dynamically 77 Generating DRAW instructions 77 A little animation 79 Handling events with VID 80 Event-driven programming 80 Default behaviours 80 Tracking events 81 The event object 83 Controlling Windows 84 Managing styles 85 Defining a style 85 Applying a style sheet 86 Modifying style aspects 87 Defining a style's behaviour 89 Rebol and sound 90 Opening and closing a sound port 90 Loading and handling sound samples 91 Playing Samples 93 Graphic display of a sound sample 94 Summary 96 Chapter 4 – Networking and the Internet 97 Using TCP/IP protocols 97 Protocols in Rebol 98 Network configuration 98 Sending and receiving email 99 Accessing web resources 101 And the other protocols? 101 xiv Table of Content Clients and servers 102 Creating network protocols in Rebol 103 The standard protocols 104 The root protocol 105 Properties of the root-protocol object 106 Methods of the root-protocol object 107 Implementing Echo 108 Developing a gopher protocol 110 Rebol and CGI scripts 113 The overall picture 114 CGI overview 114 How to write CGI scripts in Rebol? 115 Reading parameters 116 Producing dynamics web documents 119 How Magic! Works 120 Installation 120 When the static becomes dynamic 121 Magic! Functionality 123 Library functions 123 Controlling embedded Rebol code 124 Handling MIME types 124 Managing cookies 126 Managing sessions 126 Handling XML documents 128 The problem of interoperability 129 Rebol's integrated parser 129 How to use the data? 130 The guts of the parser 132 A better XML parser 133 Generating XML 135 Using Web Services 136 Introducing XML-RPC 136 Using XML-RPC 136 The Rebol/View plugin 137 Small yet powerful 137 Inserting the plugin in an HTML page 139 Configuration Parameters 140 xv Rebol – A programmer's guide Cache, proxy and compression 141 Interacting with the Browser 144 Summary 147 Chapter 5 – Rebol for pros 149 Rebol/Command 149 Concentrated power 150 Database access 150