loads the file and compiles it into a program. Then it instansiate an
.^ s
Anyway, I'm really glad to hear these plans!
/ Peter Lundqvist (disjunkt)
Previous text:
2003-04-15 20:30: Subject: Pike Cookbook
Johan Sundström and I are about to make some attempts to make a Pike Cookbook. Regardless if we have the strength to make it all the way into a book or not, I think this is a good project to spend some time on. While we can get some ideas what this cookbook should contain by looking at e.g. the Perl and Python cookbooks, much of what is in them deals with how to solve problems that are inherit in the langauges themselves. As a consequence we would like to know what problems you have with Pike and how you solve them (if you have). Johan and I will set up some sort of database or repository and collect the responses as well as inserting our own stuff. We will probably aim at getting the same basic layout as in the present cookbooks in the database, e.g.:
----------------------8<-------------------- Title: Hello World Contributor: Martin Nilsson Problem: Make a program that prints Hello World! and exits.
Solution: int main() { write("Hello World!\n"); }
Discussion:
The above code is probably the smallest possible program, counting tokens, that solves the problem. Without unessecary white spaces this is 36 characters. A more "proper" solution would however be:
int main(int argc, array(string) args, array(strings) env) { write("Hello World!\n"); return 0; }
Here all the passed arguments are received in fairly typed variables, which you don't have to do. I say fairly typed, because there is always at least one argument passed to the program, the path of the program itself, so argc could be declared as int(1..). After the interesting code has been executed 0 is returned, as result code of the program. There is an implicit return 0; at the end of every function, which we relied on in the first code. Since the return value is always 0, the main function could be declared to return int(0..0).
Since the Pike master only feeds positive return values from main into an exit call, return 0; can be replaced with exit(0);. While this doesn't really matter here it does matter if we chose another aproach for this program all together. When Pike executes a script it first loads the file and compiles it into a program. Then it instansiate an object from the program and call its main function. Thus we can put our code in the object constructor and when finished exit Pike, so it doesn't find out that the main function is missing.
void create() { write("Hello World!\n"); exit(0); } ----------------------8<--------------------
So please give us your thoughts, problems and solutions, either here or as private messages.
/ Martin Nilsson (har bott i google)