
Programming, using and understanding Pike by Fredrik H¨ubinette 2 This book was written with the intention of making anybody with a little pro- gramming experience able to use Pike. It should also be possible to gain a deep understanding of how Pike works and to some extent why it works the way it does from this book. It will teach you how to write your own extensions to Pike. I have been trying for years to get someone else to write this book, but since it seems impossible without paying a fortune for it I will have to do it myself. A http://www.emit.com.pl/ian big thanks goes to Ian Carr-de Avelon* and Henrik Wallin <[email protected]> .html for helping me iron out some of the rough spots. The book assumes that you have programmed some other programming language before and that you have some experience of UNIX. Contents 0.1 Overview ............................... 4 0.2 The history of Pike .......................... 4 0.3 A comparison with other languages ................. 5 0.4 What is Pike ............................. 5 0.5 How to read this manual ....................... 6 1 Getting started 7 1.1 Your first Pike program ....................... 7 1.2 Improving hello world.pike ...................... 8 1.3 Further improvements ........................ 9 1.4 Control structures .......................... 11 1.5 Functions ............................... 11 1.6 True and false ............................. 12 1.7 Data Types .............................. 12 2 A more elaborate example 15 2.1 Taking care of input ......................... 17 2.1.1 add record() ......................... 17 2.1.2 main() ............................. 18 2.2 Communicating with files ...................... 19 2.2.1 save() ............................. 19 2.2.2 load() ............................. 20 2.2.3 main() revisited ........................ 21 2.3 Completing the program ....................... 21 2.3.1 delete() ............................ 21 2.3.2 search() ............................ 22 3 4 CONTENTS 2.3.3 main() again ......................... 22 2.4 Then what? .............................. 23 2.5 Simple exercises ............................ 23 3 Control Structures 25 3.1 Conditions ............................... 25 3.1.1 if ................................ 25 3.1.2 switch ............................. 26 3.2 Loops ................................. 27 3.2.1 while .............................. 27 3.2.2 for ............................... 28 3.2.3 do-while ............................ 28 3.2.4 foreach ............................ 29 3.3 Breaking out of loops ......................... 29 3.3.1 break ............................. 29 3.3.2 continue ............................ 30 3.3.3 return ............................. 30 3.4 Exercises ............................... 31 4 Data types 33 4.1 Basic types .............................. 33 4.1.1 int ............................... 33 4.1.2 float .............................. 34 4.1.3 string ............................. 35 4.2 Pointer types ............................. 38 4.2.1 array .............................. 38 4.2.2 mapping ............................ 40 4.2.3 multiset ............................ 42 4.2.4 program ............................ 43 4.2.5 object ............................. 45 4.2.6 function ............................ 48 4.3 Sharing data ............................. 49 4.4 Writing data types .......................... 50 CONTENTS 5 5 Operators 53 5.1 Arithmetic operators ......................... 53 5.2 Comparison operators ........................ 56 5.3 Logical operators ........................... 56 5.4 Bitwise/set operators ......................... 57 5.5 Indexing ................................ 58 5.6 The assignment operators ...................... 59 5.7 The rest of the operators ....................... 61 5.8 Operator precedence ......................... 62 5.9 Operator functions .......................... 63 6 Object orientation 65 6.1 Terminology .............................. 65 6.2 The approach ............................. 66 6.3 How does this help? ......................... 66 6.4 Pike and object orientation ..................... 66 6.5 Inherit ................................. 67 6.6 Multiple inherit ............................ 70 6.7 Pike inherit compared to other languages ............. 71 6.8 Modifiers ............................... 71 6.9 Operator overloading ......................... 72 6.10 Simple exercises ............................ 73 7 Miscellaneous functions 75 7.1 sscanf ................................. 75 7.2 catch &throw ............................. 76 7.3 gauge ................................. 77 7.4 typeof ................................. 77 8 Modules 79 8.1 How to use modules ......................... 81 8.2 Where do modules come from? ................... 81 8.3 The . operator ............................ 81 8.4 How to write a module ........................ 82 6 CONTENTS 8.5 Simple exercises ............................ 82 9 File I/O 83 Stdio ..................................... 83 9.1 File management - Stdio.File .................... 83 Stdio.File ............................... 83 9.2 Buffered file management - Stdio.FILE ............... 93 Stdio.FILE .............................. 93 9.3 Standard streams - Stdio.stdin, stdout and stderr ......... 94 9.4 Listening to sockets - Stdio.Port .................. 94 Stdio.Port ............................... 94 9.5 UDP socket and message management - Stdio.UDP ........ 96 Stdio.UDP ............................... 96 9.6 Terminal management - Stdio.Terminfo .............. 98 Terminfo ................................ 98 9.6.1 Stdio.Terminfo.Termcap ................... 99 9.6.2 Stdio.Terminfo.Terminfo ................... 100 9.7 Simple input-by-prompt - Stdio.Readline .............. 100 Stdio.Readline ............................ 100 9.8 Other Stdio functions ........................ 101 9.9 A simple example ........................... 105 9.10 A more complex example - a simple WWW server ........ 105 10 Threads 113 10.1 Starting a thread ........................... 113 10.2 Threads reference section ...................... 114 Thread.Mutex ............................. 114 Thread.Condition ........................... 116 Thread.Fifo .............................. 117 Thread.Queue ............................. 118 Thread.thread local ......................... 119 10.3 Threads example ........................... 120 CONTENTS 7 11 Modules for specific data types 123 11.1 String ................................. 123 String ................................. 123 11.2 Array ................................. 125 Array ................................. 125 12 Image 135 Image .................................... 135 12.1 Image.Image ............................. 137 Image.Image ............................. 137 12.2 Image.Colortable ........................... 171 Image.Colortable ........................... 171 12.3 Image.Layer .............................. 180 Image.Layer .............................. 180 12.4 Image.Font .............................. 184 Image.Font .............................. 184 12.5 Image.colortable ........................... 186 Image.colortable ........................... 186 12.6 Image.Poly .............................. 186 Image.Poly .............................. 186 12.7 Image.Color .............................. 186 Image.Color .............................. 186 12.7.1 Image.Color.Color ...................... 188 12.8 Image.X ................................ 192 Image.X ................................ 192 12.9 Image.ANY .............................. 193 Image.ANY .............................. 193 12.10Image.AVS .............................. 194 Image.AVS .............................. 194 12.11Image.BMP .............................. 194 Image.BMP .............................. 194 12.12Image.GD ............................... 196 Image.GD ............................... 196 12.13Image.GIF ............................... 197 8 CONTENTS Image.GIF ............................... 197 12.14Image.HRZ .............................. 204 Image.HRZ .............................. 204 12.15Image.ILBM .............................. 205 Image.ILBM .............................. 205 12.16Image.PCX .............................. 207 Image.PCX .............................. 207 12.17Image.PNG .............................. 208 Image.PNG .............................. 208 12.18Image.PNM .............................. 210 Image.PNM .............................. 210 12.19Image.PSD .............................. 212 Image.PSD .............................. 212 12.20Image.TGA .............................. 212 Image.TGA .............................. 212 12.21Image.XBM .............................. 213 Image.XBM .............................. 213 12.22Image.XCF .............................. 214 Image.XCF .............................. 214 12.23Image.XWD .............................. 215 Image.XWD .............................. 215 12.24Image.JPEG ............................. 216 Image.JPEG ............................. 216 12.25Image.TIFF .............................. 218 Image.TIFF .............................. 218 12.26Image.TTF .............................. 219 Image.TTF .............................. 219 12.26.1 Image.TTF.Face ....................... 220 12.26.2 Image.TTF.FaceInstance .................. 221 12.27Image.XFace ............................. 221 Image.XFace ............................. 221 CONTENTS 9 13 Protocols 223 Protocols .................................
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages473 Page
-
File Size-