My biggest beef with Pike is that I can't get reliable Expect-like process control. Controlling things like yppasswd with create_process() and setsid does not work on both Solaris and Linux. I have spent hours reading the C code of both Pike and Expect trying to figure out what they do diffrently. So far without any luck.
I'm also starting to think that there could be a lower typing-overhead convienece version of Process.create_process(). I'm seeing a little bit to much of this in my scripts:
void timeout_handler(int signal) { write_errorlog("FATAL: operation timed out on SIGALRM. Aborting.\n"); exit(10); }
int main() { ...
alarm(180); /* 3 minutes should be enough for everyone... */
signal(signum("SIGALRM"), timeout_handler);
Stdio.File mystdout = Stdio.File(); Stdio.File mystderr = Stdio.File(); Stdio.File mystdin = Stdio.File(); object p=Process.create_process( ({ "/opt/timekeeper/settime", "-d", domain }), ([ "stdout" : mystdout->pipe(), "stderr" : mystderr->pipe(), "stdin" : mystdin->pipe(), ]) ); mystdin->write( time +"\n" ); int error = p->wait(); if(error) { write_errorlog("so: %O", mystdout->read(); write_errorlog("se: %O", mystderr->read(); fatalexit(11, "FATAL: Error %d for domain %s", error, domain ); } else { write_log("Domain %s worked out fine.", domain"); }
alarm(0);
... }
/ Peter Bortas
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)