La lecture en ligne est gratuite
Le téléchargement nécessite un accès à la bibliothèque YouScribe
Tout savoir sur nos offres
Télécharger Lire

pike-7.0-tutorial

63 pages
Roxen WebServer 2.2Administrator Manual1Web Site Creator Manual2Tutorials3Programmer4Pike Tutorial5The inside of InternetTable of ContentsTable of ContentsIntroduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .5What Does Pike Look Like? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5Pike and Some Other Languages. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5Reading this Tutorial . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6First Steps. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7Your Very First Pike Program. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7I Want My Greeting in a Window! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8Interactive Pike . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8Fundamental Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .11Terminology / Glossary. . . ...
Voir plus Voir moins

Vous aimerez aussi

Roxen WebServer 2.2
1
Admi
2
nistrator Manual
Web Site Creator Manual
3
Tutorials
4
Programmer
5
P
The inside of Internet
kie Tutorial
Table of Contents
Table of Contents
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .5 What Does Pike Look Like? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Pike and Some Other Languages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Reading this Tutorial . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 First Steps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7 Your Very First Pike Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 I Want My Greeting in a Window! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 Interactive Pike . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 Fundamental Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .11 Terminology / Glossary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 Your Second Pike Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .13 The Bare Bones . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 Running the Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 Command-line Arguments and if . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 Methods and Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 Magic Web Stuff . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 Syntactic Sugar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 Showing the Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 Some Notes About WWW . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 An Introduction to Data Types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .19 What is a data type? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 Basic Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 Container Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 Object, Program and Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 Variable Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 Basic Types and Reference Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .23 Methods: an Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 Calling a method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 More Advanced Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 Object-Oriented Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .25 Object Orientation in General . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 Object Orientation in Pike . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 Creating and Using Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 How to Create a Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 Classes as Record Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 Programs are Classes and Vice Versa . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 Multiple Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 Access control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .31 Choosing between Alternatives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 Repetition (or "Loops") . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 The Other Statement Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 More About Data Types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .35 The Basic Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 Container Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 The Other Reference Types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 Working with Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .41
iii
Table of Contents
Operators on Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 Built-in Functions for Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 Composing Strings with sprintf. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 Analyzing Strings with sscanf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 Wide Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 Expressions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .45 Some Terminology . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 Arithmetical Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 Operations on Complex Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 Comparison . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 Logical Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 Bitwise Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 Operations on Sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 Indexing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 Assignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 Type Conversions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 The Comma Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 Call and Splice . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 Operator Precedence and Associativity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 The Preprocessor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .55 Preprocessor Directives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .57 Some Examples of Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 How Do You Use a Module? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 How Do You Create a Module? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 How Does Pike Find the Modules? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 Errors and Error Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .59 Error Messages from Pike . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 Error Handling: the Concept . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 Detecting an Error . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 Handling the Error . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 Error Codes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 catch and throw . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 Not Covered Here . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .63 Uncharted Terrain . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
iv
10/24/2002 Introduction
Pike Tutorial Version 1.0, February 29, 2000, for Pike 7.0. Written by Thomas Padron-McCarthy, with thanks to Fre-drik Hbinette, who wrote the original tutorial. Maintained by Johan Sundstrˆm. What is Pike? Pike is an interpreted, object-oriented programming lan-guage. It looks a bit like C and C++, but it is much easier to learn and use. It can be used for small scripts as well as for large programs. Pike is • high-level and powerful, which means that even very complex things are easy to do in Pike, • object-oriented, which means that you can use modern programming techniques to divide a large program into small pieces, which are much easier to write than it would be to write the entire program at once, • interpreted, which means that you don't have to wait for a program to compile and link when you want to run it, • garbage-collected, which makes programming much simpler, and also removes the risk for memory leaks and other memory-related bugs, • easy to extend, which means that you can create plug- ins, written in Pike as well as in C or C++, and integrate them with the rest of Pike. Pike can be used to write small and simple scripts, and also for very large programs: the World Wide Web servers Roxen WebServer and Roxen Platform from Roxen Inter-net Software are written in Pike. Pike's advanced data types and built-in support for sockets makes it ideal for use in Internet applications. Pike is free software, distributed under the GNU Gen-eral Public License. Pike is available for many operating systems, among them Linux, Solaris and Windows NT. What Does Pike Look Like? Here is a small Pike program: int main()
{
What Does Pike Look Like?
write("Hi there! What's your name?\n"); string name = Stdio.stdin->gets(); write("Nice to meet you, " + name + "!\n"); return 0; } Programmers with some experience from programming languages such as C, C++, and Java, will not have much trouble understanding what this program does. Pike looks a lot like those languages, and for example uses the "curly brackets" "{" and "}" to organize program code into blocks. An excerpt from a Pike program usually does the same thing that a similar-looking program fragment in C, C++ and Java would do. Exactlyhowit is done can be very different, though. Even if you have never seen any code in C, C++ or Java before, perhaps it doesn't come as a surprise if I tell you that the program writes Hi there! What's your name? on the screen, then waits for you to type your name, and finally tells you that it is nice to meet you? All those backslashes (that is, "\") and semicolons (";") can be a bit confusing at the start. Getting comfortable with the syntax (that is, how a program looks on the sur-face) is often the most difficult part about learning Pike. On the other hand, experience shows that this is the syntax that most people feel is easy and productive when they have become used to it, and that is the reason why we keep it as it is.
Pike and Some Other Languages C++ and C Pike looks a lot like C++ on the surface, but is easier and safer to use. Since it is interpreted, it may be slower for some applications. Pike is more flexible than C++, and allows for a somewhat less rigid programming style than what is necessary in C++. Another difference is that there are many different C and C++ compilers, while there is only one implementation of Pike. Java Pike looks a bit like Java on the surface. Like Java, Pike is translated to an intermediate format, which is then exe-cuted by an interpreter. Java programs are usually distrib-uted to the user in this intermediate format, but with Pike we use the source code. This is feasible since compilation time, i. e. the time it takes to translate the program to the intermediate format, is almost zero in Pike. Perl Perl started on Unix systems as a system administration tool. Both Pike and Perl are good at handling strings, and
5
Introduction
both can be used to add functionality to web servers. Perl is much more widely used than Pike. Some programmers feel that the syntax of Perl is obscure. Python Programs written in Python look very different from Pike programs, but Python and Pike are similar when it comes to ideas and use. Python is more widely used and has more libraries available. Pike on the other hand is faster, has a more advanced type system, and has better support for object-oriented programming. Pike's more C++-like syntax makes it easier to get started with for programmers who know C++, C or Java.
Reading this Tutorial This tutorial gives an introduction to the language Pike. It is not a complete reference manual. It does not document the modules and libraries that come with the Pike distribu-tion, except for what is necessary to explain the workings of the language itself. We also assume that Pike has already been installed on your computer. How to install Pike is explained elsewhere. The tutorial is mainly intended for people with at least some experience of programming, for example from writ-ing some JavaScript on a web page, or trying to write CGI scripts on a web server in some language. We assume that you now have a need for, or just an interest in, using Pike. We also assume that you are willing to spend a few hours on reading this introduction. We recommend that you try to run the examples as you read them, since this will help your learning greatly. Even total beginners at programming will be able to understand much of what is said. If you have never pro-grammed before it is nevertheless a good idea to read a book or tutorial intended for beginners. There are some general skills and ways of thinking that are needed to write good programs, and a language tutorial such as this one will not cover those in any depth. We have tried to make this tutorial platform-indepen-dent, meaning that it will not matter if you are using Pike under Linux, Solaris, Windows NT, or some other operat-ing system. The following conventions for type faces are used: Boldfaceis used for terms that have a special meaning. Example: Avariablecan be seen as a sort of box where you can store a value. It can also be used to show values, such as7.3. Fixed sizeis used for something that is copied verba-tim from the computer. It can be parts of a program, or something printed by the computer, or something typed by the user. Example: In Pike, the datatype integer is spelledint. Italicsbe used for emphasis, but also as a place-can holder for other things. Example: In Pike, you can define a variable with "datatype-name;", where you replacenamewith the name of the variable, anddatatypewith its type. • A bordered section of fixed-size text on a blue back-ground represents an interactive session, where user
6
10/24/2002
input is marked in dark blue italics. The rest of the ses-sion's text is program output: Hello, professor. I see you brought the keys to the ferarri. > run project 21 Warming up particle accelerator... • Widthin fixed-width blocks of pike code in pike exam-ples, changes are marked in dark red bold text, as in: void example() { write("This line was changed from the previous exam ple.\n"); }
10/24/2002 First Steps
We show what a "hello world" program looks in Pike. There is also a graphical toolkit available for Pike, so you can write a window-based "hello world". Pike can also be run interactively.
Your Very First Pike Program It is traditional to start a book or tutorial about a program-ming language with a very simple example: a program that just writes the text "Hello world!" on the screen. Here it is in Pike: int main() { write("Hello world!\n"); return 0; } To run this program, you will write it in a text file, for example called "hello.pike", and then run it. If you are using a text-based interface, such as a Unix command shell or the Command Prompt Window in Windows NT, you can run the program by typing the command "pike hello.pike". If you are using a graphical interface where the file is shown as an icon, such as the Windows Explorer or a graphical file manager in Unix, you may be able to run the program by dragging its icon and dropping it on the Pike interpreter, or by double-clicking on the program icon. All this assumes that Pike has been installed on your computer. When you run the program, it will (surprise, surprise) print output: Hello world! on the screen. (In a graphical environment, this text may pop up in a separate window.) Explaining the "hello world" program If we start in the middle of this program, the line write("Hello world!\n"); is the central part of the program. We are using the built-in functionwrite, which prints text to the so-calledstandard output. The standard output is usually the computer screen. Between the parentheses afterwritewe have put the argumentsto the function. In this case, therethat we send is only one argument, the text"Hello world!\n". The dou-ble quotes (") signify that it is astring, i. e. a sequence of characters. The combination\nis translated to anewline
Your Very First Pike Program
character, so the next thing that is printed on the screen after the greeting will come on a different line. We cannot let a line like this stand by itself in a pro-gram. It must be contained in a larger construct called a functionormethod. We therefore enclose it in the method main: int main() { write("Hello world!\n"); } This (as yet unfinished) program means that there exists a method calledmain, and that this method will print a greet-ing to the standard output as explained before. Theint beforemainthat (besides doing the printing) themeans method will also return a value, an integer, to whatever it is that uses it. But we have not specified which value it will return, so we add a line that does this, finally yielding the complete program: int main() { write("Hello world!\n"); return 0; } When Pike runs (or "executes") a file like this one, it always starts by callingmain. Whenmainis finished, the program stops executing, and the returned value is used to indicate whether the program succeeded to do what it was supposed to do. Returning the value 0 frommainmeans success. A semi-colon (;) signifies the end of a so-calledstate-ment. This program contains two statements: the use of write, and the returning of a value. In the good old days, statements would have been called "program lines", since you had to have a statement on each line, but nowadays most programming languages allow you to write in "free format", dividing your program into lines in any way that you feel makes it easy to read. There are still some con-straints: you can't split words, and you can't start a new line in the middle of a quoted string. This is the same pro-gram as before, but rearranged: int main ( ) {write ( "Hello world!\n"); return 0; } Ok, those changes probably didn't make the program eas-ier to read and understand, but there are many quite legiti-mate variations in how programmers choose to write. For example, this is a common style: int main()
7
First Steps
{ write("Hello world!\n"); return 0; }
I Want My Greeting in a Window! Pike has support for graphical user interfaces. If you have the GTK Pike module installed on your computer, you can use a slightly modified program to print "Hello world!" in its own window: int main() { _ GTK.setup gtk(); GTK.Alert("Hello world!"); return -1; } The statement "GTK.setup_gtk();" is a call to a method, similar to the call towritein our first example. The differ-ence here is thatsetup gtkis found in a module calledGTK, _ so we must prefix it with "GTK." to let Pike know where to look for it. The next statement, "GTK.Alert("Hello world!");", creates a small window, an "alert window", with the text "Hello world!" in it. The window will look something like this:
The window that pops up when the GTK hello-world pro-gram is run When you click on the "OK" button, the window disap-pears. The last statement inmainis "return -1;". In Pike, a negative return value frommainmeans that the program doesn't stop executing whenmainis finished. This is neces-sary here, since otherwise the program would finish as soon
8
10/24/2002
as it had created the window, and the window would dis-appear at once. But there is a problem with this program. The program doesn't stop executing whenmainis finished, so when does it stop? Never. We may close the window, but the program is still running. It doesn't actually do anything, but it is there. We can fix the problem like this: int main() { GTK.setup gtk(); _ GTK.Alert("Hello world!") -> signal connect("destroy", exit, 0); _ return -1; } To explain this, think that GTK.Alert("Hello world!") creates the Alert window. The Alert window is a thing, or an "object". It not only pops up on your computer screen, but you can also let your program do things with this object. For example, you can tell the window that when it is destroyed, it should order the entire program to exit. This is what the rest of of the statement does (even if we don't explain the exact details here): -> signal connect("destroy", exit, 0); _ Interactive Pike You can use Pike interactively, which means that you type a line at a time, letting Pike execute it immediately. Just start Pike by giving the commandpike, without any arguments. Then type a statement, for example: # pike Pike v7.0 release 37 running Hilfe v2.0 (Incrementa l Pike Frontend) > write("hello!\n"); Pike will then do what you told it to do, i. e. print "hello!" on a line: # pike Pike v7.0 release 37 running Hilfe v2.0 (Incrementa l Pike Frontend) > write("hello!\n"); hello! Similar tomain, the built-in functionwritereturns a value, which happens to be the number of characters it has writ-ten. Interactive Pike will also show you this return value: # pike Pike v7.0 release 37 running Hilfe v2.0 (Incrementa l Pike Frontend) > write("hello!\n"); hello! Result: 7 > (Seven characters:h,e,l,l,o, the exclamation mark, and the newline character.) Running Pike interactively like this can be very useful when testing things, for example when you are following this tutorial. It can also be used as a very advanced calcula-
10/24/2002
tor.But beware! Some things don't work the same way in interactive mode as they do when you run a Pike program from a file!For example, type checking is turned off.
Interactive
Pike
9
First Steps
10
10/24/2002
10/24/2002
Terminology / Glossary
Fundamental Concepts
This tutorial assumes that you are familiar with some fun-damental concepts that are used in all programming envi-ronments. Look through the list, and if you don't immediately know what one of the terms means, you should read the explanation.
Terminology / Glossary Program A program consists of a set of instructions that tells the computer how to do something, plus some data that the program can work with. Programs can be organized in dif-ferent ways, and are often divided into several smaller parts. Data The things that your program works with, such as integers (for example5and-3) and character strings (such as "hello" and "Hi there, John!") are data. Value One piece of data (for example the integer5) is sometimes called avalue, and sometimes adata itemordata object. (But the wordobjectalso has a more specialized meaning in connection with object-oriented programming. More about that later.) Variable A variable is a sort of box that can be used to store a value. The variable usually has a name and also a type. The type determines which values you can put in the variable. Constant A constant is in a way the opposite of avariable, but also similar. A constant is like a variable, in the sense that it is a sort of box that can be used to store a value, and that it has a name. But once you have defined it, it can not be changed. Constants are often used to give names to certain values in order to make a program easier to understand: minimum_income_taxin a program is easier tosomewhere understand than just20000. Sometimes the term constant is used to includeliteralstoo. Literal A literal is a value of some kind, written in the program. "Hello"is a string literal,-678is an integer literal, and({ 7, 8, 9 })is an array literal. Sometimes the termconstant is used to include literals too. Identifier A sequence of characters that can be used in a program as a name, of a variable or of something else. In Pike an identi-
fier must start with an alphabetical letter or an underscore character ("_"). The rest of the characters can be alphabet-ical letters, underscores, and digits. Some valid identifiers in Pike aren,number_of_9s, andDoNotFeedTheMonkey. Data type Values and variables havedata types. The value5has the data typeinteger(spelledintin Pike), and5.14has the type real number (which is spelledfloatin Pike). You can only put values in a variable if the types of the value and the variable are compatible. Type checking Pike has type checking, which means that Pike keeps track of the data types of variables and values. If your program tries to put one type of value in a variable which was designed to hold another type of value, Pike may detect this, and tell you about the problem. Expression An expression is a piece of a program that gives a value when it is executed by the computer. An example of an expression in Pike is5 * x + 7, which means "take the value of the variablexand multiply it with5, and then add 7". An expression also has a type, and in this case the type isint. Statement A statement is a command that is part of a program, and that the computer can interpret and execute. Control structure The instructions in a program must be executed in the right order, and this order is expressed using control structures. Examples of control structures are selection (such as theif statement) and loops (such as thewhileloop). More about those later. True and false Control structures such asifdo one thing or another, depending on a condition. If the condition is true, it does one thing, and if it is false, it does another thing. Some lan-guages have special data values, called "true" and "false", that are used for this. In Pike, we use the simpler conven-tion that the value zero (0) is interpreted as false, and everything else is interpreted as true. Truth value True and false are sometimes calledtruth values. Block Ablockorcompound statementis a statement that consists of several other statements. In Pike, we use the curly brack-ets "{" and "}" to group statements into blocks. Some other languages use the words "begin" and "end".
11
Un pour Un
Permettre à tous d'accéder à la lecture
Pour chaque accès à la bibliothèque, YouScribe donne un accès à une personne dans le besoin